[英]Reload tableView section without reloading header section - Swift
我正在尝试重新加载 tableView 部分而不是重新加载整个 tableview,因为我在标题部分有一个文本字段,当我调用self.tableView.reloadData()
我的键盘会关闭。
我已经尝试了下面的代码,但Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete row 5 from section 2 which only contains 4 rows before the update'
我收到此错误Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete row 5 from section 2 which only contains 4 rows before the update'
那么请问我的问题在哪里?
let newCount = self.placeArray.count
var newIndexPaths = NSMutableArray(capacity: newCount)
for var i = 0 ; i < newCount ; i++ {
var indexPath = NSIndexPath(forRow: i, inSection: 2)
newIndexPaths.addObject(indexPath)
}
self.tableView.beginUpdates()
self.tableView.reloadRowsAtIndexPaths(newIndexPaths as [AnyObject], withRowAnimation: UITableViewRowAnimation.None)
self.tableView.endUpdates()
您可以改用UITableView
的reloadSections
方法。
tableView.reloadSections(IndexSet(integer: 2), with: .none)
斯威夫特 3.0.1:
let sectionIndex = IndexSet(integer: 0)
tableView.reloadSections(sectionIndex, with: .none) // or fade, right, left, top, bottom, none, middle, automatic
Swift 3.1 Xcode 8.3.3 答案:)
问题是 - 当然,每个部分都包含标题,所以部分是:
答案是 - 直接通过 IndexPath 重新加载行。 例子:
假设您的表中有1 个部分和用作表数据源的“数组”:
let indexes = (0..<array.count).map { IndexPath(row: $0, section: 0) }
现在我们有了要重新加载的单元格索引数组,所以:
tableView.reloadRows(at: indexes, with: .fade)
笔记:
UITableView.reloadData()
将重新加载所有表格。 关于 reloaddata 的文档
重新加载用于构建表格的所有数据,包括单元格、节页眉和页脚、索引数组等。 为了效率,表视图只重新显示那些可见的行。 如果表因重新加载而缩小,它会调整偏移量。 当表视图的委托或数据源希望表视图完全重新加载其数据时,它会调用此方法。
UITableView.reloadSections(_ sections: IndexSet, with animation: UITableViewRowAnimation)
将重新加载特定部分(包括部分页眉和部分页脚)。 关于重新加载部分的文档
UITableView.reloadRows(at indexPaths: [IndexPath], with animation: UITableViewRowAnimation)
将重新加载指定的行。 关于 reloadRows 的文档
所以这里reloadSections
在这里reloadSections
。 如果您的标题部分数据和状态没有改变,调用此方法将没有区别。
解决方法:使用reloadRows
加载所有节或特定节的行。 注意:在这里你可以只加载可见行,当你滚动查看时会加载其他行。 更多解决方案也可以参考Get all indexPaths in a section
var indexPaths = self.tableView.indexPathsForVisibleRows
/* just roload visible rows of specified section
indexPaths = indexPaths.filter({ (indexPath) -> Bool in
return indexPath.section == SECTION_INDEX_NEED_TO_RELOAD
})
*/
self.tableView.reloadRows(at: indexPaths!, with: .none)
斯威夫特 5
您可以重新加载部分,如下所示:
tableView.reloadSections([0], with: .none)
或者
tableView.reloadSections(IndexSet(integer: 0), with: .none)
这里零代表它将重新加载的索引。
重新加载多个部分:
tableView.reloadSections([0, 1, 3], with: .none)
这是放置静态索引重新加载,它是第一个索引,您可以根据需要进行更改。
self.tblView.reloadSections(IndexSet(index: 1), with: .none)
[斯威夫特 5]
您想重新加载一个部分中的所有行。 所以你可以这样做:
let rowIndexes: [IndexPath] = Array(0...YOUR_ARRAY-1).map({
return IndexPath(row: $0, section: YOUR_SECTION)
})
获取此部分的所有可能的行索引。 然后你只需要为这些索引重新加载行:
tableView.reloadRows(at: rowsIndexes, with: .fade)
该部分不会重新加载。
我可能会迟到,但这里有一个 2D 数组的解决方案(如果您的 tableView 有部分并且每个部分都有一些项目)。 这将重新加载每个部分中的每个单元格,但不会触及该部分的页眉/页脚。
let indexPaths = (0..<array.count).map { section in
(0..<array[section].nestedArray.count).map { IndexPath(row: $0, section: section) }
}.flatMap { $0 }
tableView.reloadRows(at: indexPaths, with: .fade)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.