Don't reuse cell in UITableView

梦想与她 提交于 2020-01-21 11:39:13

问题


let cell = tableView.dequeueReusableCellWithIdentifier("cellReuseIdentifier", forIndexPath: indexPath) as! CustomTableViewCell

I don't want to reuse the cells once the cell is created, I want it to be available in memory.


回答1:


It's your decision of course, but it's a very bad idea. Unless you have less than 10 cells in your tableView and you are 100% sure there will be no more cells. Otherwise the app will crash on memory pressure pretty fast.

Just don't dequeue cells. Create new each time:

let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "CellReuseIdentifier")

Not recommended, but it's your decision after all.




回答2:


If you have limited number of cell then only you should use this method

On viewDidLoad() you can create NSArray of custom cell

 self.arrMainCell.addObject(your custom cell);

 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.arrMain.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = self.arrMain.objectAtIndex(indexPath.row)

 }


来源:https://stackoverflow.com/questions/43487865/dont-reuse-cell-in-uitableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!