2017-07-07 79 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像素的線。