user3424472
user3424472

Reputation: 55

How do I update a label in a UITableViewCell (prototype cell) when the user taps the row in the table view?

I am trying to create a simple list using a Table View (dynamic) with Table View Cells loaded with data from a simple array.

I am trying to update a label on the Table View Cell when the user taps a row in teh Table View.

ISSUE: for some reason I can update the lable on the selected cell but the cell turns GREY and I cannot DeSelect it using:

[tableView deselectRowAtIndexPath:indexPath animated:NO];

but if I do NOT update the table view cell label, then the DeSelect works!

And vise-versa, if I remove the DeSelect row statement then the label update works!

I cannot get them to work at the same time :(

Here is my code I use to update the label: - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

 static NSString *CellIdentifier = @"ListCell";
 ListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];    
 cell.accessoryType = UITableViewCellAccessoryNone;
 cell.TitleLabel.text = @"testing";

Anyone have any ideas?

Upvotes: 0

Views: 1089

Answers (2)

klcjr89
klcjr89

Reputation: 5902

There's no need to create a new cell with a dequeueReusableCellIdentifier in the didSelectRowAtIndexPath method, that's why your text isn't updating. This below code should solve your problems.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    ListCell *cell = [tableView cellForRowAtIndexPath:indexPath];   
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.TitleLabel.text = @"testing";
}

Upvotes: 2

ilyes belfekih
ilyes belfekih

Reputation: 129

First, to get the selected cell use the cellForRowAtIndexPath method

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.TitleLabel.text = @"testing";

}

Second, to disable the selection style of your cell, set the selectionStyle of your cell to UITableViewCellSelectionStyleNone in the cellForRowAtIndexPath method

Upvotes: 0

Related Questions