Don't reuse cell in UITableView

前端 未结 3 1449
醉梦人生
醉梦人生 2021-01-18 11:48
let cell = tableView.dequeueReusableCellWithIdentifier(\"cellReuseIdentifier\", forIndexPath: indexPath) as! CustomTableViewCell

I don\'t want to r

相关标签:
3条回答
  • 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)
    
     }
    
    0 讨论(0)
  • 2021-01-18 12:46

    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.


    A note about most recent swift versions:

    'UITableViewCellStyle' has been renamed to 'UITableViewCell.CellStyle'

    0 讨论(0)
  • 2021-01-18 12:48

    When you need a cell object at runtime, call the table view’s dequeueReusableCell(withIdentifier:for:) method, passing the reuse identifier for the cell you want. The table view maintains an internal queue of already-created cells.

    If the queue contains a cell of the requested type, the table view returns that cell. If not, it creates a new cell using the prototype cell in your storyboard. Reusing cells improves performance by minimizing memory allocations during critical times, such as during scrolling.

    If you change the appearance of your custom cell’s views, implement the prepareForReuse() method of your cell subclass. In your implementation, return the appearance of your cell's views to their original state. For example, if you change the alpha property of a view in your cell, return that property to its original value

    0 讨论(0)
提交回复
热议问题