从UITableView didSelectRowAtIndexPath中检测UISegmentedControl中的选择更改

时间:2016-01-02 22:59:24

标签: ios uitableview uisegmentedcontrol 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];
    });



}
}



}

1 个答案:

答案 0 :(得分:0)

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