Reputation: 1238
I have a TableView in my app which contains 3 columns(Date, Name & Amount) whose values are fetched from Core data. I am able to display these values in my App.
Now I want to provide fixed header names to every column which doesn't go on scrolling down the list. But I am not getting any way of adding multiple headers to my table list.
Anyone has any idea about the same?
Implemented method.
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView()
let label = UILabel()
switch section {
case 0:
label.text="Date"
case 1:
label.text="Name"
case 2:
label.text="Amount"
default:
print()
}
view.addSubview(label)
return view
}
Adding screenshot of my Table View.
Upvotes: 2
Views: 4884
Reputation: 1238
Finally I managed to find the solution. It's about adding a View section just at the top of prototype. And then add labels/column name to that view. It will work. No need to make any change in TableViewController class.
Upvotes: 2
Reputation: 535129
But I am not getting any way of adding multiple headers to my table list
Correct. You don't have multiple columns, really, either; you have a single column with three things in each row. Similarly, you need to make a section header which also has three things in a row. In other words, your tableView(_:viewForHeaderInSection:)
implementation needs to return a view containing three labels side by side, saying "Date", "Name", and "Amount", exactly as your cell contains three labels side by side.
Upvotes: 1
Reputation: 810
You are doing correct just set the CGRect for label and view in viewForHeaderInSection and you can set the height of headerSection
public override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 61.0
}
Upvotes: 0
Reputation: 393
Use tableview sections for this, using the following method pass your custom section header
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
Upvotes: 1