xcode 如何为 uitableview 的所有行和所有部分创建索引路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10715655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create indexpaths for all rows and all sections for uitableview?
提问by Alexey
I have a table with n sections. Each section contains one row. How to create the index path for the table?
There is a method that allows to create index path for all visible rows [self.tableView indexPathsForVisibleRows]
I need something similar like indexPathsForAllRows
我有一张有 n 个部分的表格。每个部分包含一行。如何为表创建索引路径?有一种方法可以为所有可见行创建索引路径,[self.tableView indexPathsForVisibleRows]
我需要类似的东西indexPathsForAllRows
I need all this to update only the data in the table, because method [self.tableView reloadData];
updates all the table with headers and footers. That's why i have to use reloadRowsAtIndexPaths
我需要所有这些来仅更新表中的数据,因为方法[self.tableView reloadData];
更新所有带有页眉和页脚的表。这就是为什么我必须使用reloadRowsAtIndexPaths
回答by David R?nnqvist
You don't need reload allthe rows. You only need to reload the visible cells (that is why indexPathsForVisibleRows
exists).
您不需要重新加载所有行。您只需要重新加载可见单元格(这就是indexPathsForVisibleRows
存在的原因)。
The cells that are off-screen will get their new data in cellForRowAtIndexPath:
once they become visible.
cellForRowAtIndexPath:
一旦它们变得可见,屏幕外的单元格将获得它们的新数据。
回答by Vakas
Here is the solution in Swift 3
这是 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
}
回答by Besi
I made a UITableView
extension based on @Vakas answer. Also the sections and rows have to be checked for > 0
to prevent crashes for empty UITableView
s:
我UITableView
根据@Vakas 的回答做了一个扩展。还必须检查部分和行> 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
}
}
回答by Siddharth Kavthekar
This code will give you complete indices:
此代码将为您提供完整的索引:
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
}
}