【问题标题】:indexPath returns null for Custom UISwitch in tableView cell?indexPath 为 tableView 单元格中的自定义 UISwitch 返回 null?
【发布时间】:2013-06-03 03:47:27
【问题描述】:

所以从技术上讲,它不是 UISwitch,而是具有自定义类的 UIView,其行为类似于 UISwitch (https://github.com/domesticcatsoftware/DCRoundSwitch)

我在表格的每一行都使用这个 DCRoundSwitch。我想获取 DCRoundSwitch 所在行的 indexPath,但它一直返回 null。

以下是 DCRoundSwitch 的配置方式(使用 Value Changed):

这是我的 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:”日志消息之前返回方法。

【问题讨论】:

    标签: ios uitableview uiswitch


    【解决方案1】:

    确保 switch1.s​​uperview 实际上像您在此处假设的那样返回单元格。

    有时你会得到你没想到的奇怪的父视图层。

    【讨论】:

    • 好主意。什么是检查返回内容的好/快速方法?
    • 非虚拟机。明白了——你是对的!比预期少一个孩子。
    猜你喜欢
    • 2015-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-27
    • 1970-01-01
    • 2016-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多