0

我已经实现了每次返回 0 的代码。我正在尝试从可变数组中删除一个字符串,并在按下按钮后选择行值。

相关代码:

- (IBAction)remove:(id)sender {
    NSIndexPath *selectedIndexPath = [_tableView2 indexPathForSelectedRow];
    [names removeObject:[NSString stringWithFormat:@"%i",selectedIndexPath.row]];
    testLabel.text=[NSString stringWithFormat:@"%i",selectedIndexPath.row];
    [_tableView2 reloadData];
}

每次按下按钮时,测试标签都会显示 0,无论在哪里选择 tableview。

如果您希望我发布其他相关代码(例如 tableView),请告诉我。

4

2 回答 2

1

代替:

[names removeObject:[NSString stringWithFormat:@"%i",selectedIndexPath.row]];

尝试:

[name removeObjectAtIndex:selectedIndexPath.row];

另外,不要使用@"%i",而是尝试使用这里提到的@"%d"

希望这可以帮助!

于 2013-06-11T20:10:19.867 回答
1

对于 UITableView,选定行概念仅在用户触摸行时有效。因此,indexPathForSelectedRow记录为返回“标识所选行的行和节索引的索引路径,如果索引路径无效,则返回 nil。”</p>

我的观点是您正在获得一个nil结果,然后调用该row方法nil导致您看到的零是您的名字。

该解决方案取决于您的数据源实现的其余部分,但可能会涉及存储分接索引didSelectRowAtIndexPath:以便稍后在您的删除方法中使用它。

(我认为您没有使用该allowsMultipleSelectionDuringEditing选项,也没有编辑表格)。

于 2013-06-11T20:32:12.753 回答