【问题标题】:How to create indexpaths for all rows and all sections for uitableview?如何为uitableview的所有行和所有部分创建索引路径?
【发布时间】:2012-05-23 07:51:16
【问题描述】:

我有一张有 n 个部分的表格。每个部分包含一行。如何为表创建索引路径? 有一种方法可以为所有可见行创建索引路径[self.tableView indexPathsForVisibleRows] 我需要类似indexPathsForAllRows的东西@

我需要所有这些来仅更新表中的数据,因为方法[self.tableView reloadData]; 使用页眉和页脚更新了所有表。这就是为什么我必须使用reloadRowsAtIndexPaths

【问题讨论】:

  • 为什么需要更新屏幕外的行?它们将在 cellForRowAtIndexPath 中更新:只要您的模型更新
  • 你的意思是只要它们可见就会更新?

标签: ios xcode uitableview nsindexpath


【解决方案1】:

您不需要重新加载所有行。您只需要重新加载可见单元格(这就是indexPathsForVisibleRows 存在的原因)。

屏幕外的单元格一旦变得可见,就会在cellForRowAtIndexPath: 中获取新数据。

【讨论】:

  • 我从来没有想过以这种方式使用 indexPathForVisibleRows。
【解决方案2】:

这是 Swift 3 中的解决方案

func getAllIndexPaths() -> [IndexPath] {
    var indexPaths: [IndexPath] = []

    // Assuming that tableView is your self.tableView defined somewhere
    for i in 0..<tableView.numberOfSections {
        for j in 0..<tableView.numberOfRows(inSection: i) {
            indexPaths.append(IndexPath(row: j, section: i))
        }
    }
    return indexPaths
}

【讨论】:

    【解决方案3】:

    我根据@Vakas 的回答做了一个UITableView 扩展。此外,必须检查部分和行是否有 &gt; 0,以防止空 UITableViews 崩溃:

    extension UITableView{
        func getAllIndexes() -> [NSIndexPath] {
            var indices = [NSIndexPath]()
            let sections = self.numberOfSections
            if sections > 0{
                for s in 0...sections - 1 {
                    let rows = self.numberOfRowsInSection(s)
                    if rows > 0{
                        for r in 0...rows - 1{
                            let index = NSIndexPath(forRow: r, inSection: s)
                            indices.append(index)
                        }
                    }
                }
            }
            return indices
        }
    }
    

    【讨论】:

      【解决方案4】:

      此代码将为您提供完整的索引:

      extension UITableView {
          func allIndexes() -> [IndexPath] {
              var allIndexes: [IndexPath] = [IndexPath]()
      
              let sections = self.sectionCount() ?? 0
              if sections > 1 {
                  for section in 0...sections-1 {
                      let rows = self.rowCount(section: section) ?? 0
                      if rows > 1 {
                          for row in 0...rows-1 {
                              let index = IndexPath(row: row, section: section)
                              allIndexes.append(index)
                          }
                      } else if rows == 1 {
                          let index = IndexPath(row: 0, section: section)
                          allIndexes.append(index)
                      }
                  }
              } else if sections == 1 {
                  let rows = self.rowCount(section: 0) ?? 0
                  if rows > 1 {
                      for row in 0...rows-1 {
                          let index = IndexPath(row: row, section: 0)
                          allIndexes.append(index)
                      }
                  } else if rows == 1 {
                      let index = IndexPath(row: 0, section: 0)
                      allIndexes.append(index)
                  }
              }
              return allIndexes
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-28
        • 2012-05-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-25
        • 2021-06-23
        相关资源
        最近更新 更多