Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 972
Mike U Avatar asked Mar 24 '15 00:03

Mike U


2 Answers

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

like image 179
Wizkid Avatar answered Oct 21 '22 06:10

Wizkid


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

like image 31
Michael Avatar answered Oct 21 '22 06:10

Michael