Search code examples
iosuitableviewswift2tableheader

multiple prototype cell header moves when deleting


I have two prototype cells. One for displaying data & other I am using for header view.

When I try to delete my regular cell. The header cell moves with it.

enter image description here

I don't want header to move when I try to delete regular cells.

I have disable user interactions on header prototype cell. Still it keeps moving. In commit editing style I do immediate return for header prototype cell. Still it keeps moving. I don't know what else to do. Please help.

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    if tableView == upcomingTableView {

        if let headerCell = tableView.dequeueReusableCellWithIdentifier("headerCell") {

            headerCell.textLabel?.text = sectionNames[section]

            headerCell.detailTextLabel?.text = "Rs\(sum[section])"

            headerCell.detailTextLabel?.textColor = UIColor.blackColor()

            headerCell.backgroundColor = UIColor.lightGrayColor()

            return headerCell
        }
    }

    return nil
}

cell for row at index path is also very regular code.


Solution

  • Updated Answer:

    Create an UIView and add tableViewCell as subview and return the UIView.

    Solution in Swift:

    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    
        if let headerCell = tableView.dequeueReusableCellWithIdentifier("headerCell") {
    
            //Create an UIView programmatically
            let headerView = UIView()
    
            headerCell.textLabel?.text = (section == 0) ? "Apple" : "Microsoft"
            headerCell.backgroundColor = UIColor.lightGrayColor()
    
            //Add cell as subview to UIView
            headerView.addSubview(headerCell)
    
            //Return the header view
            return headerView
        }
    
        return nil
    
    }
    

    Output:

    enter image description here