D-Nice
D-Nice

Reputation: 4870

Storing unique ID in UITableViewCell

I have a table where there will often be two cells with the same title. I'm zooming in on a map whenever a cell in the table is clicked, so using the title as a unique identifier is out of the question. I already have a unique identifier, but I need to find a way to store it in a UITableViewCell object.

I have been considering two options, both of which are poor, IMO.

1) store the unique ID as the text inside the text of the detailTextLabel property.

2) build a custom UITableViewCell class.

I'm new to objective C, and I would essentially like to know if theres a third option that isnt as inefficient as #1, but not as involved as #2.

Thanks

Upvotes: 1

Views: 2110

Answers (1)

Ole Begemann
Ole Begemann

Reputation: 135548

Use the cell's tag property (works only if your identifier is an integer).

That said, cells already have a unique identifier (albeit not necessarily constant) and that is their indexPath, i.e. their position in the table. You should generally not use the cells to store any data directly. Instead, store the data in a model object and maintain the connection between model data and view via the indexPath.

This is especially important when dealing with table views as cell views get reused or deallocated as soon as a cell is scrolled off the screen.

Upvotes: 1

Related Questions