ios - 在 UITableViewCell 上设置自定义分隔符

标签 ios swift uitableview separator uiedgeinsets

在我的 UITableView 中,我想要一个“居中”分隔符效果,其中分隔符从左侧缩小 30pt,从右侧缩小 30pt。 我已经设法通过 Interface Builder 设置 TableView 本身的“Custom Insets”属性来实现这一点,但我无法通过代码重现此行为(我必须这样做)。

特别是这段代码:

self.tableView.separatorColor = .green
self.tableView.separatorStyle = .singleLine
self.tableView.separatorInset = UIEdgeInsets(top: 0, left: 30, bottom: 0, right: 30)

还有这个:

@objc(tableView:cellForRowAtIndexPath:) func tableView(_ tableView:  UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "recent_cell") as! TPExpandableTableViewCell
    //setting cell insets
    cell.separatorInset = separatorInset
    cell.item = items[indexPath.row]
    return cell
}

我在 iPhone 6S 模拟器 上获得了以下输出:

Separator

似乎分隔符内容 View 缩小了,但分隔符背景 View 没有缩小。我还尝试删除设置单元格的 separatorInset 的行,结果是插入等于 UIEdgeInset.zero

我可以确认绿色on下的白线是separator-related view,因为如果我把separatorStyle改成.none,它就消失了

有什么帮助吗?

最佳答案

基本上,第一段代码对于设置分隔符插入、颜色和样式是正确的:

self.tableView.separatorColor = .green
self.tableView.separatorStyle = .singleLine
self.tableView.separatorInset = UIEdgeInsets(top: 0, left: 30, bottom: 0, right: 30)

单元格的 separatorInset 是单元格的内容。所以我对你在这里真正想要实现的目标感到困惑。从你的最后一句话开始,你的内容被缩小了,这是由“cell.separatorInset = separatorInset”引起的,你不希望这样。

所以我建议你删除这行代码:

cell.separatorInset = separatorInset

关于ios - 在 UITableViewCell 上设置自定义分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40744389/

相关文章:

ios - 同一个 UIView 中的多个 UIPickerView

.net - Swift 和 .Net Web 应用程序的业务类

ios - 未调用 BEMCheckBox 委托(delegate)

ios - 如何在自定义tableview单元类中使用UITableviewcell的dragStateDidChange方法

ios - UITableViewCell 分隔线错位

ios - Swift:获取字典中的键数

ios - scrollView 放大时无法选择 UIView

ios - 检查 FaceID 是否启用

ios - Swift AVCaptureSession关闭打开按钮错误: Multiple audio/video AVCaptureInputs are not currently supported

ios - 如何在 swift 中使用 MapKit 旋转自定义 userLocationAnnotationView 图像?