How to set up a custom cell as a UITableView header or footer

I use Xib files instead of storyboards. I created a table view with some dummy data. It works fine, now I created a custom cell with some shortcuts and text fields. How can I use it as a UITableView header or footer?

+6
source share
3 answers

The answer is actually quite simple. In viewForHeaderInSection()create the cell object as you do in cellForRowAtIndexPath()and return it.

Here is a sample code.

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifier") as! YourTableViewCell
    return cell
}

You can set the height for the header as:

override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 80
} 

Update for Swift 4.2

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier") as! YourTableViewCell
    return cell
}

and

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 80
}
+22
source

. . .

override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let customview:UIView = UIView();
        let label = UILabel();
        label.text = "ur custom data"
        /*
            You can add any number of subviews you want here
            And don't forget to add it to your customview.
         */
        customview.addSubview(label)
        return customview;
    }

.

+2

This method adds a TableView header: you can add any controls to it, such as UIButton, UIImageView, .... etc.

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let sectionView = UIView()
        sectionView.frame = CGRectMake(x,y,height,width) // For Set the Frame 
        sectionView.backgroundColor = UIColor.redColor()

        return sectionView
    }
func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let sectionView = UIView()
        sectionView.frame = CGRectMake(x,y,height,width) // For Set the Frame 
        sectionView.backgroundColor = UIColor.redColor()

        return sectionView
    }

As above, you can set the section footer and you can add the size of the header and footer

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 20.0
}

func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 20.0
}

Swift 4.0

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
                let sectionView = UIView()
                sectionView.frame = CGRect(x: x, y: y, width: width, height: height)// For Set the Frame
                sectionView.backgroundColor = UIColor.red

                return sectionView
    }

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
                let sectionView = UIView()
                sectionView.frame = CGRect(x: x, y: y, width: width, height: height)// For Set the Frame
                sectionView.backgroundColor = UIColor.red

                return sectionView
}

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 20.0
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 20.0
    }
+2
source

All Articles