245

我需要在我定义的函数中删除表的第 1 行。为了使用deleteRowAtIndexPath,您必须使用IndexPath定义了节和行的 an。如何创建这样的索引路径?

以 int {1} 作为唯一成员的数组将崩溃;NSLog 消息指出该部分也需要定义。

*编辑->与单元格删除相关的代码:

    NSIndexPath *myIP = [[NSIndexPath alloc] indexPathForRow:0 inSection:0];
    NSArray *myArray = [[NSArray alloc] initWithObjects:myIP, nil];
//  [self.tableView beginUpdates];
    [self.tableView deleteRowsAtIndexPaths:myArray withRowAnimation:UITableViewRowAnimationFade];
//  [self.tableView endUpdates];
4

4 回答 4

647

用于[NSIndexPath indexPathForRow:inSection:]快速创建索引路径。

编辑:在 Swift 3 中:

let indexPath = IndexPath(row: rowIndex, section: sectionIndex)

斯威夫特 5

IndexPath(row: 0, section: 0)
于 2010-01-27T23:40:59.817 回答
123

indexPathForRow是一个类方法!

代码应为:

NSIndexPath *myIP = [NSIndexPath indexPathForRow:0 inSection:0] ;
于 2011-02-07T05:35:33.297 回答
18

Swift 中的强制性答案:NSIndexPath(forRow:row, inSection: section)

您会注意到,这NSIndexPath.indexPathForRow(row, inSection: section)在 swift 中不可用,您必须使用第一种方法来构造 indexPath。

于 2015-11-08T07:41:42.657 回答
17

对于 Swift 3,它现在是:IndexPath(row: rowIndex, section: sectionIndex)

于 2016-09-29T14:00:37.980 回答