从 UITableView didSelectRowAtIndexPath 检测 UISegmentedControl 中的选择更改
Posted
技术标签:
【中文标题】从 UITableView didSelectRowAtIndexPath 检测 UISegmentedControl 中的选择更改【英文标题】:Detect select change in UISegmentedControl From UITableView didSelectRowAtIndexPath 【发布时间】:2016-01-02 22:59:23 【问题描述】:我正在尝试检测 UISegmentedControl 选择是否更改为执行操作,但是当我更改选择时没有任何反应,我从单元格中的标签获得 UISegmentedControl
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UISegmentedControl *doing = (UISegmentedControl *)[cell viewWithTag:512];
NSString *done = [NSString stringWithFormat:@"%@", [[_myarraytask objectAtIndex:indexPath.row]valueForKey:@"done"]];
if ([done isEqualToString:@"NO"])
doing.userInteractionEnabled = YES;
if(doing.selectedSegmentIndex==0)
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Task finish ?" message:@"Please confirm" preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"Confirm" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
]];
[alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
[self closeAlertview:(cell)];
]];
dispatch_async(dispatch_get_main_queue(), ^
[self presentViewController:alertController animated:YES completion:nil];
);
【问题讨论】:
【参考方案1】:UISegmentedControl
不是这样工作的,您需要为其分配一个选择器方法事件 (UIControlEventValueChanged
)。这样它就会监听索引的变化然后调用那个函数。`
所以在你的牢房里
UISegmentedControl *doing = (UISegmentedControl *)[cell viewWithTag:512];
[doing addTarget:self
action:@selector(action:)
forControlEvents:UIControlEventValueChanged];
然后将该代码从委托中移出到它自己的函数中:
-(void)action(id)sender
UISegmentedControl* doing = (UISegmentedControl*)sender;
if(doing.selectedSegmentIndex==0)
//...
else
//...
【讨论】:
以上是关于从 UITableView didSelectRowAtIndexPath 检测 UISegmentedControl 中的选择更改的主要内容,如果未能解决你的问题,请参考以下文章