Jorge
Jorge

Reputation: 2066

Performance issue alloc vs copy

I have a table view with custom cells. Each cell has images and text and they are listening notifications. Anyone knows if it would be better to have a cell allocated as a template in memory and then copy the base layout from that instead allocating a new instance every time i need to create a cell? I have to test that but I'm lazy :) Thanks.

Upvotes: 2

Views: 140

Answers (2)

jlehr
jlehr

Reputation: 15597

UITableViewCell doesn't implement the NSCopying protocol, so you'd have to implement that yourself in a custom subclass. So if you're going to wind up writing your own implementation of -copyWithZone:, the question is how would that code be more efficient than whatever it is you're currently doing?

Upvotes: 0

Marc W
Marc W

Reputation: 19251

Before you create your cell you should call dequeueReusableCellWithIdentifier: on your UITableView and check if it's nil. If it is, create your new cell with initWithStyle:reuseIdentifier:. Make sure your reuse identifier is the same for all your cells assuming they all have the same layout. That is the standard way Apple prescribes for dealing with this issue you describe.

Take a look here and here for further details.

Upvotes: 1

Related Questions