ios 重新加载 tableView 部分而不重新加载标题部分 - Swift
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32440885/
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
Reload tableView section without reloading header section - Swift
提问by CAN
I'm trying to reload tableView section instead of reloading the whole tableview because I have a textfield in the header section and when I call self.tableView.reloadData()
my keyboard get closed.
我正在尝试重新加载 tableView 部分而不是重新加载整个 tableview,因为我在标题部分有一个文本字段,并且当我调用self.tableView.reloadData()
键盘时会关闭。
I've tried the code below but I get this error Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete row 5 from section 2 which only contains 4 rows before the update'
So please where would be my issue?
我已经尝试了下面的代码,但我收到了这个错误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()
回答by hennes
You could use UITableView
's reloadSections
method instead.
您可以改用UITableView
'sreloadSections
方法。
tableView.reloadSections(IndexSet(integer: 2), with: .none)
回答by Danut Pralea
Swift 3.0.1:
斯威夫特 3.0.1:
let sectionIndex = IndexSet(integer: 0)
tableView.reloadSections(sectionIndex, with: .none) // or fade, right, left, top, bottom, none, middle, automatic
回答by Gennadii Tsypenko
Swift 3.1 Xcode 8.3.3 Answer :)
Swift 3.1 Xcode 8.3.3 答案:)
The problemis - each section includes header of course, so section is:
的问题是-每个部分包括当然头,所以部分是:
section = header + footer + rows
部分 = 页眉 + 页脚 + 行
The answeris - reload rows directly by IndexPath. Example:
的回答是-重装行直接IndexPath。例子:
Let's say you've got 1 sectionin your table and 'array' of something you use as datasource for table:
let indexes = (0..<array.count).map { IndexPath(row: $0, section: 0) }
Now we've got array of indexes of cell we want to reload, so:
tableView.reloadRows(at: indexes, with: .fade)
假设您的表中有1 个部分和用作表数据源的“数组”:
let indexes = (0..<array.count).map { IndexPath(row: $0, section: 0) }
现在我们有了要重新加载的单元格索引数组,所以:
tableView.reloadRows(at: indexes, with: .fade)
回答by Kris Roofe
Note:
笔记:
UITableView.reloadData()
will reload all the table. Docs about reloaddatareload all the data that is used to construct the table, including cells, section headers and footers, index arrays, and so on. For efficiency, the table view redisplays only those rows that are visible. It adjusts offsets if the table shrinks as a result of the reload. The table view's delegate or data source calls this method when it wants the table view to completely reload its data.
UITableView.reloadSections(_ sections: IndexSet, with animation: UITableViewRowAnimation)
will reload specific section( including section header and section footer). Docs about reloadsectionsUITableView.reloadRows(at indexPaths: [IndexPath], with animation: UITableViewRowAnimation)
will reload the specified rows. Docs about reloadRows
UITableView.reloadData()
将重新加载所有表。 关于 reloaddata 的文档重新加载用于构建表格的所有数据,包括单元格、节页眉和页脚、索引数组等。为了效率,表格视图只重新显示那些可见的行。如果表因重新加载而缩小,它会调整偏移量。当表视图的委托或数据源希望表视图完全重新加载其数据时,它会调用此方法。
UITableView.reloadSections(_ sections: IndexSet, with animation: UITableViewRowAnimation)
将重新加载特定部分(包括部分标题和部分页脚)。关于重新加载部分的文档UITableView.reloadRows(at indexPaths: [IndexPath], with animation: UITableViewRowAnimation)
将重新加载指定的行。关于 reloadRows 的文档
So here reloadSections
will not works here. If your header section data and status not change, call this method will make no diffrence by sight.
所以这里reloadSections
不会在这里工作。如果您的标题部分数据和状态没有改变,调用此方法将没有区别。
Solution:use reloadRows
to load the rows of all the sections or of the specific section. Note: here you can just load the visible rows, others will be loaded when you scroll you view.You also can refer to Get all indexPaths in a sectionfor more solutions
解决方案:用于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)
回答by Parth Adroja
Swift 5
斯威夫特 5
You can just reload section as below:
您可以重新加载部分,如下所示:
tableView.reloadSections([0], with: .none)
or
或者
tableView.reloadSections(IndexSet(integer: 0), with: .none)
Here zerorepresent the index it will reload.
这里零代表它将重新加载的索引。
Reload multiple sections:
重新加载多个部分:
tableView.reloadSections([0, 1, 3], with: .none)
回答by Vivek
Swift 5
斯威夫特 5
Here is put static index reload which is 1st index, you can change as per your requirement.
这是放置静态索引重新加载,它是第一个索引,您可以根据需要进行更改。
self.tblView.reloadSections(NSIndexSet(index: 1) as IndexSet, with: .none)