Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell edit cell content on click

I have a question about UITAbleViewCell's.

I have implemented UITableViewDelegate method

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];

    cell.backgroundColor = [UIColor redColor];
    cell.textLabel.textColor = [UIColor redColor];
    cell.textLabel.text = @"Title";
}

After I click on desired cell, nothing happens...

Why it doesn't work as I expected? Also, what should I do to make it work?

like image 367
Daumantas Versockas Avatar asked Feb 06 '26 21:02

Daumantas Versockas


1 Answers

You have to create some base model for cell states e.g:

@property NSString *modelState = @"red"; // this is fast hint, but it can be a enum with states.

all cell will have one title after tap.

... other controller code...

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

        UITableViewCell *cell = [self.restaurantTable  dequeueReusableCellWithIdentifier:@"cell_ID"];
// cell customization method        
       [self customizeCell:cell accordingToStateStr:modelState];

        return cell;
    }

... other controller code...

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

    // Set other state for cell
       self.modelState = @"red";

    [tableView reloadData];
    }

- (void)customizeCell:(UITableViewCell*)cell accordingToStateStr:(NSString *)str {
   if ([str isEqualToString:@"red"]) {
  cell.backgroundColor = [UIColor redColor];
    cell.textLabel.textColor = [UIColor redColor];
    cell.textLabel.text = @"Title";
   } else if(...) {
    //Other options..
   }
}

[tableView reloadData]; - will trigger once again "cellForRow" method and your table will be redrawn according to new model.

You can use for cell states emuns instead NSString object (this is only scaffold for you).

like image 159
Robert Avatar answered Feb 09 '26 10:02

Robert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!