【问题标题】:How to create NSIndexPath for TableView如何为 TableView 创建 NSIndexPath
【发布时间】:2011-01-10 04:58:27
【问题描述】:

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

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

*Edit -> 删除单元格相关代码:

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

【问题讨论】:

    标签: ios iphone uitableview nsindexpath


    【解决方案1】:

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

    编辑:在 Swift 3 中:

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

    斯威夫特 5

    IndexPath(row: 0, section: 0)
    

    【讨论】:

      【解决方案2】:

      indexPathForRow 是一个类方法!

      代码应为:

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

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          【解决方案4】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-09-21
            • 2018-04-03
            • 1970-01-01
            • 2020-05-01
            相关资源
            最近更新 更多