简体   繁体   English

如何为 TableView 创建 NSIndexPath

[英]How to create NSIndexPath for TableView

I need delete row 1 of a table in a function I have defined.我需要在我定义的函数中删除表的第 1 行。 In order to use deleteRowAtIndexPath you must use an IndexPath with a section and row defined.为了使用deleteRowAtIndexPath您必须使用已定义节和行的IndexPath How can I create an indexpath like this?如何创建这样的索引路径?

An array with the int {1} as its only member will crash;以 int {1} 作为唯一成员的数组会崩溃; the NSLog message states that the section needs to be defined as well. NSLog 消息指出该部分也需要定义。

*Edit -> Code relating to cell delete: *编辑 -> 与单元格删除相关的代码:

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

Use [NSIndexPath indexPathForRow:inSection:] to quickly create an index path.使用[NSIndexPath indexPathForRow:inSection:]快速创建索引路径。

Edit: In Swift 3:编辑:在 Swift 3 中:

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

Swift 5斯威夫特 5

IndexPath(row: 0, section: 0)

indexPathForRow is a class method! indexPathForRow是一个类方法!

The code should read:代码应为:

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

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

You will notice that NSIndexPath.indexPathForRow(row, inSection: section) is not available in swift and you must use the first method to construct the indexPath.您会注意到NSIndexPath.indexPathForRow(row, inSection: section)在 swift 中不可用,您必须使用第一种方法来构造 indexPath。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM