Ike
Ike

Reputation: 91

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

Upvotes: 9

Views: 18763

Answers (6)

user4254887
user4254887

Reputation:

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

Upvotes: 0

Akshay
Akshay

Reputation: 2983

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 ..

Upvotes: 0

Alexis
Alexis

Reputation: 16829

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

[yourTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];

Upvotes: 2

crafterm
crafterm

Reputation: 1881

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

Upvotes: 10

Saurabh G
Saurabh G

Reputation: 1875

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?

Upvotes: 2

yonel
yonel

Reputation: 7865

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)....

Upvotes: -1

Related Questions