iOS
iOS

Reputation: 3626

UITableViewCell subclass or Prototype cell

My question may be stupid, but, I am just curious to know the better approach. When a same complex cell is to be used in many tableViews, it is better to create a UITableViewCell subclass.

But, if want to go with Prototype cells, by just copy pasting the cell in tableViews, will there be difference? Will there be any impact on memory consumption?

Upvotes: 1

Views: 226

Answers (1)

Miknash
Miknash

Reputation: 7948

Create separate class + xib file (for instance MyCellUITableViewCell). Move all your complex UI/Logic to that cell. Then you can reuse this cell every where. Just register nib to tableView

For instance:

let nib = UINib(nibName: "MyCellUITableViewCell", bundle: nil)
self.tableView.register(nib, forCellReuseIdentifier: "MyCellUITableViewCell")

That way, you can reuse cell on multiple places and won't have to copy it on multiple places.

If you copy your cell, it won't take more memory but app will be heavier. After all, that way you could keep it DRY ( don't repeat yourself).

I missed objective-c flag and wrote in Swift 3 - for Objective-c:

UINib *cellNib = [UINib nibWithNibName:@"MyCellUITableViewCell" bundle:nil];
[self.tableView registerNib:cellNib forCellReuseIdentifier:@"MyCellUITableViewCell"];

Upvotes: 2

Related Questions