How do you change the textLabel when UITableViewCell is selected?

Ike picture Ike · Dec 3, 2009 · Viewed 18.1k times · Source

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];
}

Answer

crafterm picture crafterm · Apr 20, 2010

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";
}