views:

385

answers:

3

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

Did you try to refresh only the selected cell instead of reloading the whole table ?

[cell setNeedsDisplay];

instead of

[tableView reloadData];

This will have better performance and I'm not but I suppose that selection is lost if you refresh the whole table (this may be the reason why you don't see any change in the end)....

yonel
What if you're using a custom cell view?
meridimus
A: 

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?

Saurabh G
A: 

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