Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you change the textLabel when UITableViewCell is selected?

I want to change the textLabel and detailTextLabel of a cell when it has been selected. I've tried the following, but no change occurs:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MyAppDelegate *appDelegate = (MyPhoneAppDelegate*)[[UIApplication sharedApplication] delegate];

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.detailTextLabel.text = @"xxxxx";
    cell.textLabel.text =       @"zzzzz";
    [tableView reloadData];
}
like image 827
Ike Avatar asked Dec 03 '09 22:12

Ike


2 Answers

I agree, reloading the table view will actually dump and reload/display all the cells using tableView:cellForRowAtIndexPath: and use the original data, not the updated @"xxxxx" and @"yyyyy" in your tableView:didSelectRowAtIndexPath: method.

In a little test project I was able to change the labels upon selection with:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.textLabel.text = @"it was tapped";
}
like image 91
crafterm Avatar answered Sep 18 '22 13:09

crafterm


You should not be trying to reload the table while a cell is selected. Instead, try

[cell setNeedsLayout]

after you make the above changes to the labels.

Also, is there a reason you're making a reference to the app delegate in the method?

like image 21
Saurabh G Avatar answered Sep 17 '22 13:09

Saurabh G