在模拟器上调用accessoryButtonTappedForRow但不是Device

时间:2015-01-06 06:22:30

标签: ios uitableview

我有一个自定义的tableView设置,它不会对单元格进行dequeing。我没有在storyboard中使用静态单元格,因为我使用的是包含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 {
        //
    }
}

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

FYI所有tableView委托和数据源都正确设置到文件所有者。就像我说的一切正常,直到我添加第二个NSIndexPath,这让我相信问题所在。但如果是这种情况,为什么它在模拟器中完美运行,甚至区分两个不同的按钮索引?我想我在这里错过了一些简单的东西。怎么麻烦拍?

编辑 记录indexPaths:

SIMULATOR RETURNS

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

2 个答案:

答案 0 :(得分: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");
    }
}

希望这有帮助。

答案 1 :(得分:1)

==用于指针对等的对象测试,而不是"这个索引路径与那个"具有相同的行和部分。

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

 if ([indexPath isEqual:maintenanceInfo])
相关问题