Swipe to delete cell causes tableViewHeader to move with cell

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
}

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]


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; 
}

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
}