Doug Smith
Doug Smith

Reputation: 29326

Does it gravely affect performance to use a bunch of different classes for UITableViewCells?

I have a bunch of different UITableViewCell types. Performance-wise, would it be better to separate them into different classes, or to have one central class and change around some attributes and views programmatically?

The reason I ask is because in cellForRowAtIndexPath you dequeue your cell, and if you dequeue a bunch of different classes, wouldn't that make reuse difficult as there would be a massive divide of classes in the reusable "pool" of cells?

Upvotes: 1

Views: 43

Answers (2)

Duncan C
Duncan C

Reputation: 131471

To add to user1840001's excellent answer (voted) :

You should use different classes when your cell STRUCTURE varies in any non-trivial way.

Use the same cell class if you have the same structure (set of fields) but different content to display in that structure.

EDIT: Looks like my up-vote didn't "take." NOW I upvoted @user1840001's answer...

Upvotes: 0

cph2117
cph2117

Reputation: 2681

You should create a class for each UITableViewCell type that you have. That's best practice for a number of reasons, including, but not limited to: 1) Modularity of code (You can modify each cell easily when the need arises) 2) Simplicity (Having one class for multiple different types of cells would be a headache with all the if/else statements etc... 3) There won't be any memory issues

Why won't there be memory issues or as you describe, "a massive divide of classes"? The UITableView will only allocate as many cells as it needs to fill the tableview when it appears. As you scroll, the cells that were allocated at first, are now queued for reuse. If an instance arises where the cell can be reused, it will be, thereby avoiding another allocation, and thereby avoiding a UI slowdown. So, at any one time, regardless of how many UITableViewCell classes you have (unless you have thousands), there will be relatively few cells in the queue waiting to be reused.

Upvotes: 1

Related Questions