ios - 在多编辑模式下调整 UITableViewCell 分隔线

标签 ios swift uitableview

我希望在 iOS 9/10 的标准 UITableView 中处于多编辑模式时调整分隔符插入边距。

我的目标是将分隔线与单元格文本标签的左边缘对齐。

在过去,我通过以下方式完全去除了边距:

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    cell.separatorInset = .zero
    cell.layoutMargins = .zero
}

这给了我一个观点,例如:

如果我尝试增加分隔符内插,整个文本标签都会移动,这是我不希望的。 :

cell.separatorInset = UIEdgeInsetsMake(0, 40, 0, 0)

我怎样才能移动分隔线,而不影响文本布局,最好不要覆盖 layoutSubviews 或手动绘制线条?

最佳答案

你可以手动添加分隔符,尝试使用下面的代码

func viewDidLoad() {
    self.tableview.separatorStyle = UITableViewCellSeparatorStyle.None
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
   let separator = CALayer()

    separator.backgroundColor = UIColor.grayColor();
    separator.frame = CGRectMake(0, 43, self.view.frame.size.width, 1);
    cell.layer.insertSublayer(separator, atIndex:0)
    return cell;
} 

关于ios - 在多编辑模式下调整 UITableViewCell 分隔线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39601952/

相关文章:

ios - Google Consent SDK - 链接器错误

ios - 在没有互联网时防止 JSONSerialization 崩溃我的应用程序的最佳方法?

ios - 为什么 iOS 8 上的 SceneKit 无法编译我的自定义着色器?

ios - scrollToRowAtIndexPath 在 iOS 8 上无法正确滚动到底部

ios - 设置新区域时,iOS 模拟器会自动显示通知

ios - 检查应用程序是否通过 GameCenter 挑战启动

ios - 无法从 KASlideShow pod 加载图像

swift - 在 Swift 3 中创建 UnsafeMutablePointer<UInt8>

ios - 如何创建具有 TableView、搜索和一些编辑功能的自定义基类,以便其他类可以继承它

ios - 在显示表格之前加载解析中的所有数据