Swift 中的 UITableView:未预加载屏幕外单元格 [英] UITableView in Swift: Offscreen cells are not pre-loaded
问题描述
我对 UITableView 有点问题.当我第一次向下滚动时,不会加载屏幕外的单元格.当我向上滚动(以便未显示的单元格再次离开屏幕)并再次向下滚动时,它们突然出现.我认为这与 dequeueReusableCellWithIdentifier
有关.知道如何解决这个问题吗?
I have a little problem with UITableView. Cells that are offscreen are not loaded when I first scroll down. When I scroll up (so that the not showing cells are offscreen again) and scroll down again they suddenly show up. I think this has something to do with dequeueReusableCellWithIdentifier
. Any idea how to fix this?
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCellClass
return cell
推荐答案
这是 UITableView
的预期行为.UITableView
的全部意义在于排列需要的单元格并释放不需要的单元格来管理内存.
This is the intended behaviour of a UITableView
. The whole point of a UITableView
is to queue up cells that are needed and release cells that aren't needed to manage memory.
如果您希望所有单元格都被加载(我建议不要这样做,可怕的性能和内存使用情况),请填充一个滚动视图,其中的视图被每一行的一些 y 常量偏移.
If you would like the cells to be all loaded (I would recommend against this, horrible performance and memory usage), populate a scroll view with views offsetted by some y constant for every row.
再说一次,单元格没有加载是有原因的,这就是 UITableView
的全部意义所在.Apple 没有进行全面加载,我认为您也不应该这样做.
Once again, cells aren't loaded for a reason and that's the whole point for a UITableView
. Apple didn't do a load-all implementation and neither do I think you should.
这篇关于Swift 中的 UITableView:未预加载屏幕外单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!