根据需要在每个自定义的UITableViewCell上设置分隔符插页

问题描述 投票:0回答:1

我只能让分隔符插入在表视图层工作。如果我在UITableViewCell上创建了一个自定义的镶边,或者尝试删除它,它就不会工作。我有一个tableview,在一些单元格中我需要一个自定义的分隔符插入,即在左边有一点缩进,在右边接触到边距,而对于一些单元格我根本不需要分隔符。

如果我设置 bookWorkspaceTableView.separatorStyle = .none in viewDidLoad()我根本看不到分隔符,即使在我想显示它的单元格上也是如此。如果我不设置`bookWorkspaceTableView.separatorStyle = .none',那么即使在我不想看到的单元格中,我也能看到分隔符。

下面的代码是我没有将表格视图的分隔符样式设置为none,我想隐藏默认的表格视图分隔符,但即使使用下面的代码,我仍然看到它......我该如何解决?:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        log.debug("MyPlaces::cellForRowAt \(indexPath.row)")

        if (indexPath.row == 0) {
            if let cell = tableView.dequeueReusableCell(withIdentifier: "allDaySwitchCell") as? LeftLabelRightSwitch {
                cell.selectionStyle = UITableViewCell.SelectionStyle.none
                cell.leftLabel.text = IndoorsLocalizedString.all_day.getLocalizedString()
                cell.leftLabel.textAlignment = .left

                cell.layoutMargins = UIEdgeInsets.zero
                cell.separatorInset = UIEdgeInsets.zero

                // Remove seperator inset
//                if cell.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
//                    cell.separatorInset = .zero
//                }
//                // Prevent the cell from inheriting the Table View's margin settings
//                if cell.responds(to: #selector(setter: UITableViewCell.preservesSuperviewLayoutMargins)) {
//                    cell.preservesSuperviewLayoutMargins = false
//                }
//                // Explictly set your cell's layout margins
//                if cell.responds(to: #selector(setter: UITableViewCell.layoutMargins)) {
//                    cell.layoutMargins = .zero
//                }

                //cell.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
                return cell
            }
        }

enter image description here

enter image description here

ios swift uitableview
1个回答
0
投票

这对我来说也是个难题。所以我不想使用UITableView的默认分隔符。那我就创建自己的分隔符。为UITableViewCell创建一个扩展并设置我自己的。

extension UITableViewCell {
    func addSeperator() {
        let seperator = UIView(frame: CGRect(x: 0, y: contentView.frame.height - 1, width: contentView.frame.width, height: 1))
        seperator.backgroundColor = .seperator
        contentView.addSubview(seperator)
    }
}

在你的情况下,我认为你只需要为这个函数设置边距的选项。

© www.soinside.com 2019 - 2024. All rights reserved.