Custom UI TableViewCell selected backgroundcolor swift

前端 未结 11 908
盖世英雄少女心
盖世英雄少女心 2020-12-28 16:51

I am trying to change the appearance of a custom selected TableViewCell using Swift.

Do I need to do it via the designer or programmatically?

I tried the fol

11条回答
  •  被撕碎了的回忆
    2020-12-28 17:22

    When you tap a cell, a subviews background color is actually being changed. That subview is 'selectedBackgroundView'. You can override the view of each cell in the cellForRowAtIndexPath TableView delegate method.

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
       let cell = tableView.dequeueReusableCellWithIdentifier("identifier", forIndexPath: indexPath) 
    
       let selectedView = UIView()
       selectedView.backgroundColor = UIColor(red: 250/255, green: 250/255, blue:     250/255, alpha: 1.0)
       cell.selectedBackgroundView = selectedView
    
       return cell
    }
    

    Change the color to whatever you like.

提交回复
热议问题