Kaz
Kaz

Reputation: 1466

update title of selected row in Uitableview

i'm using the following code to get the selected cell in the UiTableView

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        // Mofify the cell here
    }

when i change the cell text or color it does not make any change in the actual UitableView

Upvotes: 0

Views: 2194

Answers (4)

Darren
Darren

Reputation: 10129

I would suggest that you reload the cell. Use:

[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:NO];

You should keep track of which cell was selected, and update the cell in

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

I don't know why the text isn't updating for you when you change it in

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

but even if you get it to work, it will revert if you scroll that cell off the screen.

Upvotes: 3

Anton
Anton

Reputation: 4018

Try doing:

  1. Set the backgroundColor property of the UITableViewCell.
  2. Set the backgroundColor property of the UILabel (may not do what you need).
  3. Set the backgroundView property if the UITableViewCell to a custom class you make by subclassing UIView, where you create a label and color.

Also, you can't just change the cell color, you need to have a data source from which cellForRowAtIndexPath: reads, which sets the cell color there. This way, the color change will persist.

Upvotes: 0

Vincent Bernier
Vincent Bernier

Reputation: 8664

UITableView is a subclass of UIView and UIView has this method to force it's refresh

- (void)setNeedsDisplay

But if you don't take that change into account in

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

you may never see the change.

Upvotes: 1

Fernando Valente
Fernando Valente

Reputation: 1096

UITableViewCell has the -titleLabel method which returns a UILabel. Then you might use the setText: and -setColor: methods.

Upvotes: 0

Related Questions