How to make UITableViewCell highlighted state persist

前端 未结 1 1263
一整个雨季
一整个雨季 2021-02-14 14:21

I have a UITableviewCell. When a user clicks the cell, Im saving the indexpath and then calling the cellforrowAtIndexpath method to get the cell and then call the SetHighlighted

1条回答
  •  无人共我
    2021-02-14 14:28

    save the indexpath of the selected cell

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        self.selectedIndexPath = indexPath;
    }
    

    and compare in tableVIew:cellForRowAtIndexPath:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        // configure cell
        if ([indexPath isEqual:self.selectedIndexPath]) {
            [cell setHighlighted:YES];
        }
        else {
            [cell setHighlighted:NO];
        }
        return cell;
    
    }
    

    However, keep in mind that apple discourages the use of the cell highlight state to indicate selected cell. You should probably use cell.accessoryType = UITableViewCellAccessoryCheckmark;

    0 讨论(0)
提交回复
热议问题