在表格视图节标题顶部显示编辑行操作。

3
在iOS 11上,如果表格视图样式设置为普通(而不是分组),则单元格编辑操作或滑动操作(仅适用于iOS 11)将显示在节标题的顶部。这似乎是默认功能,我想知道这是系统错误还是我的实现有误。
我使用了默认表格视图实现和以下功能来实现标题和编辑操作行: 标题名称:
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return "Section \(section)"
    }

编辑行的操作:

override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        let firstAction = UITableViewRowAction(style: .normal, title: "Action1", handler: {_,_ in })
        let secondAction = UITableViewRowAction(style: .destructive, title: "Action2", handler: {_,_ in })
        return [firstAction, secondAction]
    }

滑动删除操作(只适用于iOS 11的新实现):

override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        let action1 = UIContextualAction(style: .normal, title: "Action1", handler: {_,_,_ in })
        let action2 = UIContextualAction(style: .destructive, title: "Action2", handler: {_,_,_ in })
        return UISwipeActionsConfiguration(actions: [action1, action2])
    }

这是编辑操作的屏幕截图: Edit action overlapping section header 这种情况只会在iOS 11上发生。我已经尝试交替使用上述两种方法,但结果相同。
你有什么想法可以解决这个问题吗?这是iOS 11系统的一个bug吗?

看起来这只发生在iOS 11.4上。至少对我来说是这样的情况。 - Pratik Mistry
我已经在其他版本的iOS上进行了测试,对于我来说,在11.2.2上也会出现这种情况,因此我认为这是iOS 11的普遍问题。 - frozencure
1
可能我们做错了什么,但到目前为止,对我来说看起来像是系统错误。因为同样的事情在iOS 9和iOS 10中似乎都正常工作。 - Pratik Mistry
1
刚刚遇到了这个问题:(在iOS 9、10上工作正常,但在11上不行。有什么解决方法吗? - kd02
你找到什么了吗?我在iOS 11.4上遇到了这个问题。 - mikro098
显示剩余4条评论
1个回答

3
任何遇到这个问题的人,我发现一个解决方法是通过子类化UITableViewHeaderFooterView类,然后将layer's zPostion设置为1,这将改变头部视图在窗口视图渲染中的顺序,从而解决了我的问题。 解决方案1:
open class HeaderFooterView: UITableViewHeaderFooterView {

    public override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)

        if #available(iOS 11.0, *) {
            self.layer.zPosition = 1;
        }
    }
}

如果您不想子类化UITableViewHeaderFooterView类,则可以使用替代方法,只需覆盖viewForHeaderInSection代理方法,然后在返回视图之前设置视图的zPosition即可。 解决方案2:
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "CustomHeader") as! CustomHeader

    headerView.title = "Header title"

    if #available(iOS 11.0, *) {
         headerView.layer.zPosition = 1;
    }

    return headerView
}

1
我认为这个解决方案会触发另一个 bug: 滚动条将出现在部分标题下面,因为滚动条的 zPosition 是 0。你可以在这里看到 UICollectionView 中对应的 bug,解决方案是将标题视图的 zPosition 设置回 0(这将导致本问题的 bug 重新浮现)。 - peco
1
是的,@peco,你说得对,我能够复制出现滚动条在标题后面的错误。我将尝试找到解决方法。谢谢你让我知道。 - kd02
嘿,我正在使用这个,但是对于“Section”它不起作用,只能为行进行滑动。 - A.s.ALI

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接