Alessandro
Alessandro

Reputation: 4100

Accessing UITableViewCell from tag

I am trying to change the detail label on a UITableViewCell in this way:

UITableViewCell *cell = (UITableViewCell *)[folderDisplayTableView viewWithTag:CODE_FOR_OPEN_FOLDER];
if (cell != nil)
{
   cell.detailTextLabel.text = @"YO";
}

If CODE_FOR_OPEN_FOLDER corresponds to a tag, then the method works, otherwise the app crashes. I can't understand why since I check if cell is nil...

Upvotes: 0

Views: 86

Answers (2)

Seryozha
Seryozha

Reputation: 1639

note that 0 is the default value for UIView's tag property, in this case you should check your tag variable before getting cell via viewWithTag, instead of checking cell != nil, I'm sure cell != nil when CODE_FOR_OPEN_FOLDER = 0, in this case cell.detailTextLabel.text may cause a crash, here is your code edited,

UITableViewCell *cell;
if (CODE_FOR_OPEN_FOLDER == 11) // for example 11 is a tag value
{
    cell = (UITableViewCell *)[folderDisplayTableView viewWithTag:CODE_FOR_OPEN_FOLDER];
}
cell.detailTextLabel.text = @"YO"; // there's nothing wrong sending message to nil object

Hope it helps.

Upvotes: 1

coria7
coria7

Reputation: 1

Why don't you use didSelectRowAtIndexPath method and then get the number of row and text with tag

Upvotes: 0

Related Questions