CAN*_*CAN 25 uitableview ios 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'所以请问我的问题在哪里?
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()
Run Code Online (Sandbox Code Playgroud)
hen*_*nes 37
你可以使用UITableView's reloadSections方法代替.
tableView.reloadSections(NSIndexSet(index: 2), withRowAnimation: .None)
Run Code Online (Sandbox Code Playgroud)
Dan*_*lea 21
Swift 3.0.1:
let sectionIndex = IndexSet(integer: 0)
tableView.reloadSections(sectionIndex, with: .none) // or fade, right, left, top, bottom, none, middle, automatic
Run Code Online (Sandbox Code Playgroud)
Gen*_*nko 11
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)
雨燕5
您可以重新加载部分,如下所示:
tableView.reloadSections([0], with: .none)
Run Code Online (Sandbox Code Playgroud)
或者
tableView.reloadSections(IndexSet(integer: 0), with: .none)
Run Code Online (Sandbox Code Playgroud)
这里零代表它将重新加载的索引。
重新加载多个部分:
tableView.reloadSections([0, 1, 3], with: .none)
Run Code Online (Sandbox Code Playgroud)
我不知道为什么错误答案会得到如此多的批评。
注意:
UITableView.reloadData()将重新加载所有表格。 有关reloaddata的文档
重新加载用于构造表的所有数据,包括单元格,节的页眉和页脚,索引数组等。为了提高效率,表视图仅重新显示那些可见的行。如果表由于重新加载而缩小,则会调整偏移量。当表视图的委托或数据源希望表视图完全重新加载其数据时,将调用此方法。
UITableView.reloadSections(_ sections: IndexSet, with animation: UITableViewRowAnimation)将重新加载特定的部分(包括部分标题和部分页脚)。有关reloadsections的文档
UITableView.reloadRows(at indexPaths: [IndexPath], with animation: UITableViewRowAnimation)将重新加载指定的行。有关reloadRows的文档
所以这里在这里reloadSections行不通。如果标题部分的数据和状态未更改,则调用此方法不会造成任何差异。
解决方案:用于reloadRows加载所有部分或特定部分的行。注意:这里您可以只加载可见行,滚动查看时将加载其他行。您也可以参考“ 获取所有indexPaths”一节中的更多解决方案
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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
32273 次 |
| 最近记录: |