Kunal Verma Kunal Verma - 11 days ago 6
iOS Question

Create new UITableView everytime (not reuse any cell)

I have a table view with 3 types of custom cells.
Interface in storyboard.

3 different classes in the project for the cells.

I'm doing this currently and the cells IBOutlets are coming out to be nil.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = ListCell()
cell.configureCell(Data[indexPath.row])
return cell
}


class ListCell : UITableViewCell {

@IBOutlet weak var selectButton: UIButton!

func configureCell(data: ParamList) {
selectButton.setTitle("Select " + data.name, forState: .Normal)
}

@IBAction func selectButtonPressed(sender: AnyObject) {
print(selectButton.currentTitle)
}

}


I don't want to reuse any cells because every cell has different properties.
Some have textboxes other have different interactive matter which needs to be submitted in the end to a webservice.

I want to create new cells in memory for every cell.
Max cells will be around 15. So won't have much of a memory problems.

Any way I can Do that??

Answer

I did finally solve this. Here's what I did for the same. I am describing my process if anyone else wanna do the same.

Warning, only do this if absolutely necessary and you do not have that many cells that can affect the performance.

For making UITableView's cells to remain in memory even if scrolled outside the screen encapsulate your table view in a scroll view, and set your UITableView's scrollEnabled to false.

If using Storyboards set a height constraint to your TableView and make an IBOutlet to your code file.

After getting your data, set your tableView's height to a multiplier of your data count.

i.e.

tableViewHeightConstraint.constant = heightOfOneCell * data.count tableView.delegate = self tableView.dataSource = self

This way you can scroll with the scrollView and all your cell will still be in the memory.