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

2

6

I have the following swift code to implement a UITableViewRowAction, when I swipe a row the row slides out to the left as expected, however all the Section Header Rows also slide to the left with the table row at the same time.

I have also included a screen shot to show what is happening

If I remove the viewForHeader override and replace it with titleForHeaderInSection then I have no problem.

The reason for overiding the viewForHeader is that I want to place an image in the 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) {

}

As I slide the row to the left all the Section Rows slide as well

Caprifoliaceous answered 24/3, 2015 at 0:36 Comment(0)
C
6

I had the same problem. The answer is simple. Because you use UITableViewCell as header view. Try UILabel or UITableViewHeaderFooterView instead.

Cahier answered 30/4, 2015 at 16:30 Comment(0)
A
23

Simply return cell.contentView instead of cell within your viewForHeaderInSection function and your problem will be resolved.

Anthropomorphism answered 10/7, 2015 at 14:24 Comment(1)
And if your section has a background color, apply it to the contentView instead of to the cell itself.Abilene
C
6

I had the same problem. The answer is simple. Because you use UITableViewCell as header view. Try UILabel or UITableViewHeaderFooterView instead.

Cahier answered 30/4, 2015 at 16:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.