从 UITableView didSelectRowAtIndexPath 检测 UISegmentedControl 中的 select 变化

Detect select change in UISegmentedControl From UITableView didSelectRowAtIndexPath

我正在尝试检测 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];
    });



}
}



}

这不是 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{
     //...
    }
}