ChatGPT解决这个技术问题 Extra ChatGPT

如何为 TableView 创建 NSIndexPath

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

以 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];

B
Blazej SLEBODA

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

编辑:在 Swift 3 中:

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

斯威夫特 5

IndexPath(row: 0, section: 0)

P
Paras Joshi

indexPathForRow 是一个类方法!

代码应为:

NSIndexPath *myIP = [NSIndexPath indexPathForRow:0 inSection:0] ;

J
JohnVanDijk

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

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


S
Sergey Kalinichenko

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