disable the uitableview highlighting but allow the selection of individual cells

前端 未结 12 1236
心在旅途
心在旅途 2020-12-07 18:29

when you tap on a cell the row gets selected and highlighted.Now what i want to do is disable the highlighting but allow the selection.Is there a way around it.There is ques

相关标签:
12条回答
  • 2020-12-07 18:40

    To add a custom color use the below code. And to make it transparent use alpha: 0.0

    cell.selectedBackgroundView = UIView(frame: CGRect.zero)
    cell.selectedBackgroundView?.backgroundColor = UIColor(red:0.27, green:0.71, blue:0.73, alpha:1.0)
    

    If you use custom color and want to give it rounded corner look use:

    cell.layer.cornerRadius = 8
    

    Also, use this for better animation and feel

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
        tableView.deselectRow(at: indexPath, animated: true)
    }
    
    0 讨论(0)
  • 2020-12-07 18:44

    in swift 3

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            tableView.deselectRow(at: indexPath, animated: true)
    }
    
    0 讨论(0)
  • 2020-12-07 18:44

    Here is the solution for swift 3,works even in editing mode

    cell.selectionStyle = .gray
    cell.selectedBackgroundView = {
    
        let colorView = UIView()
        colorView.backgroundColor = UIColor.black.withAlphaComponent(0.0)
        //change the alpha value or color to match with you UI/UX
    
        return colorView
    }()
    
    0 讨论(0)
  • 2020-12-07 18:45

    For Objc:

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    - (void)viewDidLoad {
      [super viewDidLoad];
      _tableView.allowsSelection = YES;
    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        .. .. .. .. 
       [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
       . . . . .. 
    }
    
    0 讨论(0)
  • 2020-12-07 18:47

    Try setting cell selection style to None -

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    

    This will solve your problem

    0 讨论(0)
  • 2020-12-07 18:48

    For those looking for a programmatic way in Swift 3

    cell.selectionStyle = UITableViewCellSelectionStyle.none

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