UITableView in Swift: Offscreen cells are not pre-

2019-09-21 16:25发布

问题:

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

回答1:

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.

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.

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.



回答2:

Seems like I had checked the hidden option for the cells causing this behavior. When removed it works flawlessly.