8

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];
}
leppie
  • 115,091
  • 17
  • 196
  • 297
Ike
  • 81
  • 1
  • 1
  • 2

6 Answers6

10

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
  • 1,831
  • 19
  • 17
2

Try to reload the cell you selected (described by indexPath) :

[yourTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
Alexis
  • 16,629
  • 17
  • 62
  • 107
2

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
  • 1,895
  • 14
  • 15
0

Not sure what you're trying to do with the delegate but you should try calling the tableView already instantiated; i.e. call

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath: indexPath];

Maybe I'm not clear What I'm saying is that you are instantiating a new empty table view

UITableViewCell *cell = [**tableView** cellForRowAtIndexPath: indexPath]; //cell has nothing it is new.

consider replacing to call the old

UITableViewCell *cell = [**self.tableView** cellForRowAtIndexPath: indexPath]; //now you have one that has a textField already in it
  • Do you see what I'm saying? You're creating a new instance not the old one. –  Nov 21 '14 at 23:00
  • If you create a new one you must reload data to see it in the table view. If you use the old one the view will reflect the changes. –  Nov 21 '14 at 23:05
0

Create a New iPad Project (Split View) and Now go through the Classes->Files. The easiest way's been given there. The XCode's Generated Codes.

Sample Code Lines :-

cell.textLabel.text = [NSString stringWithFormat:@"Row %d", indexPath.row];

You can use them in cellForRowAtIndexPath ||&& didSelectRowAtIndexPath ..

Hiren
  • 12,720
  • 7
  • 52
  • 72
Akshay
  • 2,973
  • 6
  • 43
  • 75
-1

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
  • 7,855
  • 2
  • 44
  • 51