gpt4 book ai didi

ios - UITableView - 自定义 UITableViewCell 中的自定义 selectedBackgroundView 在选择时隐藏单元格分隔符

转载 作者:搜寻专家 更新时间:2023-10-31 22:56:49 25 4
gpt4 key购买 nike

我有一个 UITableView 呈现自定义类 DrawerTableCell 的单元格,它是这样声明的:

class DrawerTableCell: UITableViewCell {

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

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

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

在我的 View Controller 中我设置:

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
}

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

关于如何在所选单元格中保留分隔线有什么想法吗?

谢谢。

最佳答案

根据 Apple 的文档,只有当单元格被选中时,UITableViewCell 才会将此属性的值添加为 subview 。如果它不为零,它将选择的背景 View 作为 subview 直接添加到背景 View (backgroundView) 之上,或者在所有其他 View 之后。

所以根据这个,我认为你的分隔符是隐藏的,因为它在你的新 View 下面。尝试将 selectionColor 设置为 .clear,看看会发生什么。如果这不起作用,您还可以尝试完全删除表格 View 中的分隔符,而只需在每个单元格的底部添加一条 0.5px 的线。

关于ios - UITableView - 自定义 UITableViewCell 中的自定义 selectedBackgroundView 在选择时隐藏单元格分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44978721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com