UITableView Setting some cells as “unselectable”

后端 未结 16 1470

How can I set the UITableView\'s cell property to be unselectable? I don\'t want to see that blue selection box when the user taps on the cell.

16条回答
  •  半阙折子戏
    2020-12-12 18:27

    To make certain row unselected you have to make some changes in two methods of UITableView delegate.

    In the method below

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

    after allocation of the cell, write the code below

    if(indexPath.row == someCellNumber) cell.selectionStyle =UITableViewCellSelectionStyleNone;
    

    The above code will prevent highlighting the cell, if somehow user tries to selects.

    in this delegate method below

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        if(indexPath.row == someCellNumber) return;
    
        //for other cells write the code below...
    }
    

    if you don't write if(indexPath.row == someCellNumber) return; row will still be selected and there is a chance of app crash

提交回复
热议问题