首页 文章

如何为TableView创建NSIndexPath

提问于
浏览
235

我需要在我定义的函数中删除表的第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];

4 回答

  • 115

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

  • 17

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

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

  • 18

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

    Edit: 在Swift 3中:

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

    indexPathForRow 是一种类方法!

    代码应为:

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

相关问题