Swipe to delete cell causes tableViewHeader to move with cell

IosObjective CUitableviewIos8

Ios Problem Overview


I have encountered a strange bug with my tableViewHeader on my UITableView in iOS 8. When swiping on a cell to reveal the delete button (standard iOS swipe-to-delete), it moves the tableViewHeader along with the cell that is being swiped. As I swipe the cell, the header moves in the same way that the cell being swiped does. No other cells in the table view are moved, only the header and whatever cell is being swiped. I have tested this on iOS 7 haven't encountered the problem. To me, this seems like a bug with tableViewHeader in iOS 8, being that it only occurs in this version and seems like something that should never occur. I see no reason for the header to ever be included in swipe-to-delete.

Below is just a mockup. Swipe-to-delete within the app is default iOS, nothing custom.

Below is just a mockup. Swipe-to-delete within the app is default iOS, nothing custom.

Ios Solutions


Solution 1 - Ios

Building on ferris's answer, I found the easiest way when using a UITableViewCell as a section header is to return the contentView of the cell in viewForHeaderInSection. The code is as follows:

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let  cell : cellSectionHeader = tableView.dequeueReusableCellWithIdentifier("SectionHeader") as cellSectionHeader
    return cell.contentView
    //cellSectionHeader is my subclassed UITableViewCell
}

Solution 2 - Ios

This was caused because I was using a UITableViewCell as the header for the table. To solve the swiping issue, instead of using tableView.tableHeaderView = cell, I use the following:

UIView *cellView = [[UIView alloc] init];
[cellView addSubview:cell];
tableView.tableHeaderView = cellView

I don't know why this solves the problem, especially being that it worked on iOS 7, but it seems to solve the problem.

Make sure to add all view to the cells view, as supposed to the cells contentView, otherwise the buttons will not be responsive.

Works:

[cell addSubview:view]; or [self addSubview:view];

Doesn't work:

[cell.contentView addSubview:view] or [self.contentView addSubview:view]

Solution 3 - Ios

The way to avoid the headers moving with the cells is to return the contentView of the cell in viewForHeaderInSection. If you have a subclassed UITableViewCell named SectionHeaderTableViewCell, this is the correct code:

-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    SectionHeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SectionHeader"];
    //Do stuff to configure your cell
    return cell.contentView; 
}

Solution 4 - Ios

SWIFT 3.0 Tested solution. As mentioned in the first example for Objective-C; the key point is returning cell.contentView instead of cell So new format syntax is as below.

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    
    // HeaderCell is the name of custom row designed in Storyboard->tableview->cell prototype
    let cell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell")
    
    cell.songLabel.text = "Your Section Name"
    return cell.contentView
}

Solution 5 - Ios

Try implementing this method and give proper conditions for checking the swipe.If this method get called for header view.

1.tableView:editingStyleForRowAtIndexPath: 2.tableView:titleForDeleteConfirmationButtonForRowAtIndexPath: 3.tableView:shouldIndentWhileEditingRowAtIndexPath:

Solution 6 - Ios

One problem not mentioned with the .contentView method is that if your table view cell makes use of layoutSubviews, you may not get the behavior you want-- because layoutSubviews will not get called. I ended up making an entirely separate UIView subclass that supports both normal cell operation and header operation, and creating a bare minimum UITableView cell class that uses that.

Solution 7 - Ios

Victor's issue of losing background color, is solved by below addition to Brad's answer:

cell.backgroundColor = UIColor.cyanColor()

To:

cell.contentView.backgroundColor = UIColor.cyanColor()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionferrisView Question on Stackoverflow
Solution 1 - IosBrad CView Answer on Stackoverflow
Solution 2 - IosferrisView Answer on Stackoverflow
Solution 3 - IosbodagettaView Answer on Stackoverflow
Solution 4 - IosTrevorView Answer on Stackoverflow
Solution 5 - IosiOSdevView Answer on Stackoverflow
Solution 6 - IosChris PrinceView Answer on Stackoverflow
Solution 7 - IosShivaView Answer on Stackoverflow