You have to iterate using the horizontalHeaderItem()
:
labels = []
for c in range(self.tableWidget.columnCount()):
it = self.tableWidget.horizontalHeaderItem(c)
labels.append(str(c + 1) if it is None
else it.text())
print(labels)
This method expects a return value of type UIView. The view returned from this method will be displayed as the header of the section specified by the viewForHeaderInSection parameter.,This method expects a return value of type UIView. The view returned from this method will be displayed as the footer of the section specified by the viewForFooterInSection parameter.,The return value of this method is of type NSString. This string will automatically be placed inside a label by the table view and will be displayed as the header of the section, which is specified in the titleForHeaderInSection parameter.,The return value of this method is of type NSString. This string will automatically be placed inside a label by the table view and will be displayed as the footer of the section, which is specified in the titleForFooterInSection parameter.
To add a table header view, grab a view and drop it near the top of your table view. (You can create your header view in a XIB if you prefer – the problem and solution are the same.),Create a CustomHeaderView class that subclasses UIView. In the Storyboard (or XIB), set your table header view’s class to CustomHeaderView.,Grab a label from the object library and drop it onto the header view. Use the guides to position it 8 points from each edge of the table view. Make sure to drop some long text into your label, too.,If you’ve tried to create a table header view in Interface Builder, you may have noticed that it doesn’t size properly even when you have all the right constraints. Here’s what happens if you just set up your constraints:
Create a new IBOutlet
in your CustomHeaderView class:
@IBOutlet
var label: UILabel!
Override layoutSubviews()
in CustomHeaderView, making sure to call super
, and set the preferredMaxLayoutWidth
on your label:
override func layoutSubviews() {
super.layoutSubviews()
label.preferredMaxLayoutWidth = label.bounds.width
}
Here’s how I did it in my ViewController
:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
sizeHeaderToFit()
}
func sizeHeaderToFit() {
let headerView = tableView.tableHeaderView!
headerView.setNeedsLayout()
headerView.layoutIfNeeded()
let height = headerView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
var frame = headerView.frame
frame.size.height = height
headerView.frame = frame
tableView.tableHeaderView = headerView
}
Paul Hudson @twostraws June 1st 2019
You can use the built-in iOS table section headers by returning a value from titleForHeaderInSection
like this:
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) - > String ? {
return "Section \(section)"
}
If you want to return a custom header view with something more than just some text, you should use viewForHeaderInSection
instead, like this:
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) - > UIView ? {
let vw = UIView()
vw.backgroundColor = UIColor.red
return vw
}