2012-05-23 18 views
8

我有一個表有n個部分。每個部分包含一行。如何創建表的索引路徑? 有一種方法,它允許創建索引路徑對所有可見的行[self.tableView indexPathsForVisibleRows]我需要類似的東西像indexPathsForAllRows如何爲uitableview的所有行和所有部分創建索引路徑?

我需要這一切只更新表中的數據,因爲方法[self.tableView reloadData];更新所有的表的頁眉和頁腳。這就是爲什麼我必須使用reloadRowsAtIndexPaths

+0

爲什麼你需要的所有行? –

+0

我更新了問題 – Alex

+1

爲什麼需要更新屏幕外的行?他們將在cellForRowAtIndexPath中得到更新:只要您的模型已更新 –

回答

10

您不需要重新加載全部的行。您只需重新加載可見單元格(這就是爲什麼indexPathsForVisibleRows存在)。

屏幕外的單元格將在cellForRowAtIndexPath:可見時獲取新數據。

+0

我從來沒有想過以這種方式使用indexPathForVisibleRows。 – Mark

3

這裏是斯威夫特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擴展。另外,部分和行必須檢查> 0防止崩潰空UITableView S:

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 
    } 
} 
相關問題