为什么当我实现 UITableViewRowAction 时,所有节标题行都与 table 单元格一起滑动

Why do all Section Heading rows slide with table cell when i implement UITableViewRowAction

我有以下 swift 代码来实现 UITableViewRowAction,当我滑动一行时,该行会按预期向左滑动,但是 Header 部分的所有行也会向左滑动同时显示 table 行。

我还提供了一个屏幕截图来显示正在发生的事情

如果我删除 viewForHeader 覆盖并将其替换为 titleForHeaderInSection 则我没有问题。

覆盖 viewForHeader 的原因是我想在 Header 行中放置一个图像。

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let cell = tableView.dequeueReusableCellWithIdentifier("header") as UITableViewCell

    cell.textLabel?.text = instrumentGroups[section].name
    cell.imageView?.image = UIImage(named: instrumentGroups[section].name)

    return cell
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> InstrumentTableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("instrumentCell", forIndexPath: indexPath) as InstrumentTableViewCell

    cell.instrument = instrumentGroups[indexPath.section].instruments[indexPath.row]

    return cell
}

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {

    let instrument = instrumentGroups[indexPath.section].instruments[indexPath.row]

    var actions: [AnyObject] = []

    var action = UITableViewRowAction(style: .Normal, title: "Remove Watch") { (action, indexPath) -> Void in
        tableView.editing = false

        instrument.SetWatchList(false)

        self.refresh()
    }

    action.backgroundColor = UIColor.redColor()
    actions.append(action)

    return actions
}

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

}

我遇到了同样的问题。答案很简单。 因为您使用 UITableViewCell 作为 header 视图。请尝试使用 UILabel 或 UITableViewHeaderFooterView。

只需在您的 viewForHeaderInSection 函数中 return cell.contentView 而不是 cell,您的问题就会得到解决。