当我实现UITableViewRowAction时,为什么所有Section Heading行都与表格单元格一起滑动

Mik*_*e U 6 uitableview ios swift uitableviewrowaction

我有以下swift代码来实现UITableViewRowAction,当我滑动一行时,行按预期向左滑动,但是所有Section Header Rows也同时向下滑动到表行.

我还包括一个屏幕截图来显示正在发生的事情

如果我删除viewForHeader覆盖并用titleForHeaderInSection替换它,那么我没有问题.

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

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) {

}
Run Code Online (Sandbox Code Playgroud)

当我向左滑动行时,所有Section Rows也会滑动

Wiz*_*kid 23

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

  • 如果您的部分具有背景颜色,请将其应用于`contentView`而不是单元格本身. (2认同)

Mic*_*ael 6

我有同样的问题.答案很简单.因为您使用UITableViewCell作为标题视图.请改用UILabel或UITableViewHeaderFooterView.