Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Don't reuse cell in UITableView

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.

like image 353
Mukul More Avatar asked Apr 19 '17 06:04

Mukul More


People also ask

How can we use a reusable cell in UITableView?

For performance reasons, a table view's data source should generally reuse UITableViewCell objects when it assigns cells to rows in its tableView(_:cellForRowAt:) method. A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse.

What is the use of Resueidentifier in a UITableView?

The reuse identifier is associated with a UITableViewCell object that the table-view's delegate creates with the intent to reuse it as the basis (for performance reasons) for multiple rows of a table view. It is assigned to the cell object in initWithFrame:reuseIdentifier: and cannot be changed thereafter.

How do I populate UITableView?

There are two main base ways to populate a tableview. The more popular is through Interface Building, using a prototype cell UI object. The other is strictly through code when you don't need a prototype cell from Interface Builder.


2 Answers

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'

like image 145
Max Pevsner Avatar answered Oct 20 '22 17:10

Max Pevsner


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) 
}
like image 21
Shefali Soni Avatar answered Oct 20 '22 17:10

Shefali Soni