10

I am using Xib files instead of storyboard. I have created a table view with some dummy data. Its working fine, Now I have created a custom cell with some labels and textFields. How can I use it as header or footer of UITableView ?

Byte
  • 629
  • 2
  • 11
  • 29
  • 1
    you can not use UITableView cell as header or footer , for that you need to create one view (xib) and you can use it – Anjali Bhimani Jul 01 '16 at 07:44
  • cell as header? tableHeaderView is a type of view, not a cell – ronan Jul 01 '16 at 07:44
  • How to create a UiView with and Xib ? when i create a new CocoaTouch file and make it subclass of UIView the option of "Also create Xib file" is not check able – Byte Jul 01 '16 at 07:51
  • Duplicated with here http://stackoverflow.com/questions/12556750/using-a-xib-file-for-custom-tableview-section-header – Proton Jul 01 '16 at 08:00

3 Answers3

33

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

Here is sample code.

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

You can set height for 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
}
Tushar Korde
  • 1,189
  • 12
  • 23
  • Adding Tableview cell is not a good solution https://stackoverflow.com/questions/15611374/customize-uitableview-header-section – bhupinder Dec 13 '18 at 10:49
2

You can create a custom view programmatically. I tried the following code. Try it once.

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

similarly you can do the same thing for header also.

Ramcharan Reddy
  • 621
  • 1
  • 5
  • 18
2

This is the Method add the Header of TableView: In that you can any controls Like 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
    }

Like Above you can set the Footer of Section 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
    }
Ronak Adeshara
  • 321
  • 4
  • 13
  • I have created a cstom view which contains an imageView. When i try to use this method with a simple view wotha nackground color it works but not with my custom view – Byte Jul 01 '16 at 09:35