Ios indexPath为tableView单元格中的自定义UISwitch返回null?

Ios indexPath为tableView单元格中的自定义UISwitch返回null?,ios,uitableview,uiswitch,Ios,Uitableview,Uiswitch,所以从技术上讲,它不是一个UISwitch,而是一个具有自定义类的UIView,其行为就像一个UISwitch() 我在一张桌子的每一行都使用这个DCRoundSwitch。我想获取DCRoundSwitch所在行的indexPath,但它始终返回null 以下是DCRoundSwitch的配置方式(使用更改的值): 以下是我的togglePublic方法: -(void)togglePublic:(UIControl *)button withEvent:(UIEvent *)event {

所以从技术上讲,它不是一个UISwitch,而是一个具有自定义类的UIView,其行为就像一个UISwitch()

我在一张桌子的每一行都使用这个DCRoundSwitch。我想获取DCRoundSwitch所在行的indexPath,但它始终返回null

以下是DCRoundSwitch的配置方式(使用更改的值):

以下是我的togglePublic方法:

-(void)togglePublic:(UIControl *)button withEvent:(UIEvent *)event {


    DCRoundSwitch *switch1 = (DCRoundSwitch *)button;
    UITableViewCell *cell = (UITableViewCell *)switch1.superview;
    NSIndexPath *indexPath = [self.tableview indexPathForCell:cell];

    NSLog(@"PUBLICSWITCH PRESSED %@", indexPath);


    //NSIndexPath * indexPath = [self.tableview indexPathForRowAtPoint:[[[event touchesForView: button] anyObject] locationInView: self.tableview]];


    if ( indexPath == nil ){

        return;

    }


    AHBContact *contact = [self.contacts[indexPath.section] allValues][0][indexPath.row];

    NSLog(@"Public switch toggled for: %@", contact);


}

如您所见,indexPath返回nil,因为if语句总是在进入“Public switch toggled for:”日志消息之前返回方法。

确保switch1.superview实际返回的单元格与您在这里假设的一样


有时,您会得到意想不到的奇数层父视图

确保switch1.superview实际返回的单元格与此处假设的相同


有时,您会得到意想不到的奇数层父视图

好主意。什么是检查返回内容的好/快方法?nvm。明白了,你是对的!比预期低了一个孩子。好主意。什么是检查返回内容的好/快方法?nvm。明白了,你是对的!比预期低了一个孩子。