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];
}
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";
}
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With