简体   繁体   中英

Don't reuse cell in UITableView

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

一旦创建单元格,我不想重用单元格,我希望它在内存中可用。

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 '

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)

 }

Don't use a tableview. Just use a scrollview. Use tableview if you have a long list where you can't keep all the views in memory.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM