liv a
liv a

Reputation: 3340

How can I the identify the UITableViewCell in code?

If I have a table view with different types of cells each with its unique identifier, can I find out which reuse identifier the indexPath relates to before I dequeue it in:

- (UITableViewCell *)tableView:(UITableView *)tableView 
                     cellForRowAtIndexPath:(NSIndexPath *)indexPath

If there is another method that can do some sort of reflection on the cell that would be fine too.

Thanks

Upvotes: 0

Views: 192

Answers (4)

MaheshShanbhag
MaheshShanbhag

Reputation: 1494

you need to store the data as to what at indexpath you need what type of cell, because the reusabele identifier is the pool from which the cell is picked for reuse.

Eg: if the

 reusableidentifier = @"reuse"; then when 
-dequeueReusableCellWithIdentifier:

is used it picks a cell from a pool that is named "reuse" if a cell is available for reuse else a new cell is created. So you cannot find which indexpath belongs to which reusable identifier.

Upvotes: 0

Seamus Campbell
Seamus Campbell

Reputation: 17916

You can, but you have to do all the work. It's your job to inspect the indexPath and determine what kind of content you are generating for the particular cell, then to dequeue/or create a cell of that type, set it up, and return it.

You can't do reflection on the cell, though, because the cell doesn't exist yet. That's the purpose of -tableView:cellForRowAtIndexPath:.

One way you might approach this is to have an array with a dictionary of information about each cell (you'd need one array per section of the table; I'll just assume there's only a single section). In the beginning of -tableView:cellForRowAtIndexPath:, you'd index into the array with the row number from the index path, and then inspect values of the dictionary to determine what kind of cell to dequeue and how to set it up.

Edit: It sounds like you may have some confusion about what the cell reuse identifier is and how it is generated. A cell reuse identifier is an arbitrary string that is attached to cells inside a tableview. When you ask for a cell to be dequeued, the tableview checks to see if any unused cells with that reuse identifier are available. If all of the cells are the same type of cell and configured in the same way, you'll just have one reuse identifier, and it can be any string you choose. Just make sure that you use that string both in calls to -dequeueReusableCellWithIdentifier: and UITableViewCell's -initWithStyle:reuseIdentifier:.

Upvotes: 2

QED
QED

Reputation: 9923

Ultimately, you are the originator of cellIds, giving them to newly created cells in tableView:cellForRowAtIndexPath:, and so you should not need to ask the table view for cellIds. The best way to accomplish what you seek is to add a function to your class:

-(NSString *)cellReuseIdForIndexPath:(NSIndexPath *)indexPath;

The function can look up cellIds in a dictionary, an array, or via any other mean you wish. Use that function whenever you need it, as long as it stays consistent with the data you present in the table view, it will work.

Upvotes: 0

Burhanuddin Sunelwala
Burhanuddin Sunelwala

Reputation: 5343

if(cell != nil)    
NSLog(@"%@", cell.reuseIdentifier);

Upvotes: -1

Related Questions