2017-07-07 60 views
1

当我有一个UITableView呈现的自定义类DrawerTableCell的细胞,即宣告这样的UITableViewCell隐藏电池隔膜:UITableView的自定义selectedBackgroundView定制选择

class DrawerTableCell: UITableViewCell { 

    @IBInspectable var selectionColor: UIColor = .gray { 
     didSet { 
      configureSelectedBackgroundView() 
     } 
    } 

    func configureSelectedBackgroundView() { 
     let view = UIView() 
     view.backgroundColor = selectionColor 
     selectedBackgroundView = view 
    } 
} 

UITableViewCell子类是为了创建能够为单元格指定选定的背景颜色。

在我的视图控制器我设置:

tableView.separatorColor = .white 

,我将单元配置是这样的:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "drawerCell") as! DrawerTableCell 
    cell.selectionColor = UIColor.blue 
    return cell 
} 

的结果是,当没有选定单元格,所有的隔板正确显示。当我选择一个单元格时,会显示背景颜色,但分隔符仅隐藏在所选单元格中。

有关如何将分隔线保留在选定单元格中的任何想法?

谢谢。

回答

0

根据Apple的文档,UITableViewCell仅在选择单元格时才将此属性的值作为子视图添加。它将选定的背景视图作为子视图直接添加到背景视图(backgroundView)的上方,如果它不是零,或者在所有其他视图之后。

所以根据这个,我认为你的分隔符是隐藏的,因为它在你的新视图下面。尝试将您的selectionColor设置为.clear并查看发生了什么。如果这不起作用,您也可以尝试完全移除桌面视图上的分隔符,然后在每个单元格的底部添加一个0.5像素的线。