Changing background color of selected cell?

后端 未结 30 2963
太阳男子
太阳男子 2020-11-29 00:22

Does anyone know how to change the background color of a cell using UITableViewCell, for each selected cell? I created this UITableViewCell inside the code for TableView.

30条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-29 00:47

    I have a highly customized UITableViewCell. So I implemented my own cell selection.

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    

    I created a method in my cell's class:

    - (void)highlightCell:(BOOL)highlight
    {
        if (highlight) {
            self.contentView.backgroundColor = RGB(0x355881);
            _bodyLabel.textColor = RGB(0xffffff);
            _fromLabel.textColor = RGB(0xffffff);
            _subjectLabel.textColor = RGB(0xffffff);
            _dateLabel.textColor = RGB(0xffffff);
        }
        else {
            self.contentView.backgroundColor = RGB(0xf7f7f7);;
            _bodyLabel.textColor = RGB(0xaaaaaa);
            _fromLabel.textColor = [UIColor blackColor];
            _subjectLabel.textColor = [UIColor blackColor];
            _dateLabel.textColor = RGB(0x496487);
        }
    }
    

    In my UITableViewController class in ViewWillAppear added this:

    NSIndexPath *tableSelection = [self.tableView indexPathForSelectedRow];
    SideSwipeTableViewCell *cell = (SideSwipeTableViewCell*)[self.tableView cellForRowAtIndexPath:tableSelection];
    [cell highlightCell:NO];
    

    In didSelectRow added this:

    SideSwipeTableViewCell *cell = (SideSwipeTableViewCell*)[self.tableView cellForRowAtIndexPath:indexPath];
    [cell highlightCell:YES];
    

提交回复
热议问题