【问题标题】:accessoryButtonTappedForRow called on Simulator but not Device在模拟器上调用了附件ButtonTappedForRow,但在设备上没有调用
【发布时间】:2015-01-06 06:22:30
【问题描述】:

我有一个自定义的 tableView 设置,它不会使单元格出队。我没有在情节提要中使用静态单元格,因为我使用的是包含 tableView 的 UIViewController。不管设置如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    // Array > Dictionary > cell.textLabel.text info here

    switch (indexPath.section) {
        case 0:
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            switch (indexPath.row) {
                case 4:
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                    cell.accessoryType = UITableViewCellAccessoryDetailButton;
                //etc etc

单元格的设置与上述代码显示的相同。

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
    NSIndexPath *pubdisclaimer = [NSIndexPath indexPathForRow:2 inSection:2];
    NSIndexPath *maintenanceInfo = [NSIndexPath indexPathForRow:4 inSection:0];

    if (indexPath == pubdisclaimer) {
        NSLog(@"Disclaimer Tapped");
        //Show UIAlertController
    } else if (indexPath == maintenanceInfo) {
    NSLog(@"Maintenance tapped");
        //Show Different UIAlertController
    } else {
        //
    }
}

在模拟器中检测到附件ButtonTapped(并且相应地填充不同的 UIAlertController)但是,当在与模拟器相同的部署目标的设备上运行时,除了按钮突出显示之外什么都没有发生。这适用于一个 indexPath,但是当我包含 else if 语句时,它不会在设备上调用。

仅供参考,所有 tableView 委托和数据源都已正确设置为文件所有者。就像我说的,在我添加第二个 NSIndexPath 之前一切正常,这让我相信这就是问题所在。但如果是这样的话,为什么它在模拟器中可以完美运行,甚至区分两个不同的按钮索引?我想我在这里遗漏了一些简单的东西。如何解决问题?

编辑记录索引路径:

模拟器返回

2015-01-05 21:42:54.666 [38440:9202972] didSelectRowAtIndexPath section: 0, row: 4
2015-01-05 21:42:57.993 [38440:9202972] accessoryButtonTapped section: 0, row: 4
2015-01-05 21:43:04.765 [38440:9202972] didSelectRowAtIndexPath section: 2, row: 2
2015-01-05 21:43:08.185 [38440:9202972] accessoryButtonTapped section: 2, row: 2 

设备退货

2015-01-05 21:45:17.564 [4072:1128326] didSelectRowAtIndexPath section: 0, row: 4
2015-01-05 21:45:19.763 [4072:1128326] Nothing tapped : else
2015-01-05 21:45:28.613 [4072:1128326] didSelectRowAtIndexPath section: 2, row: 2
2015-01-05 21:45:29.313 [4072:1128326] Nothing tapped : else

【问题讨论】:

  • else 块中添加一个警报,并检查您的情况是否会出现。
  • @Mrunal 测试,结果如您所料。两个附件按钮 NSLog 到 else 语句。这证实了两个 indexPaths 都是正确的。但是,仍然很困惑为什么它可以在模拟器上运行。你有什么建议
  • NSLog(@"section: %d, row: %d", indexPath.section, indexPath.row);在模拟器和设备上。然后比较为什么它对两者都不同。在这里你需要深入研究代码。
  • @Mrunal 感谢更新问题

标签: ios uitableview


【解决方案1】:

更改if 条件并将其分别分解为rowsection

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {

// PubDisclaimer
    if ((indexPath.row == 2 && indexPath.section == 2)) {
        NSLog(@"Disclaimer Tapped");
        //Show UIAlertController
    } 
// MaintenanceInfo
    else if ((indexPath.row == 4 && indexPath.section == 0)) {
        NSLog(@"Maintenance tapped");
        //Show Different UIAlertController
    } 
    else {
        NSLog(@"Nothing tapped");
    }
}

希望这会有所帮助。

【讨论】:

  • 很遗憾,这行得通。我说伤心不是因为我伤心,因为我不明白哈。感谢您修复我,但是您能否提供有关为什么必须以这种方式设置它的知识,特别是因为索引是明确设置的,以及为什么它在模拟器上而不是设备上工作的任何理论?我是来学习的,不仅仅是获取代码 :) 谢谢
  • 我通常以这种方式进行比较。很高兴知道这对您有帮助。您能否为答案投票。抱歉打错了,我直接在答案框中写了,没有用XCode检查。
【解决方案2】:

== 用于对象测试指针是否相等,而不是“此索引路径是否具有与该索引路径相同的行和节”。

如果它在模拟器上工作,那是运气,不应该依赖,可能一些低编号的索引路径被缓存,就像一些低编号的 NSNumbers 是为了提高效率。您应该将对象与isEqual: 进行比较:

 if ([indexPath isEqual:maintenanceInfo])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多