Reputation: 81
I am implementing a UITableView
within my UIViewController
. I have been able to get my text to display with a custom UITableViewCell
, but I can not find a way to put spacing between my cells.
I have tried implementing tableView(_:heightForHeaderInSection:)
, but it seems that it is not called as I have tried to put in a print statement to see if it runs.
Upvotes: 0
Views: 3868
Reputation: 174
i had problem that heightForHeaderInSection was not working, but after i set viewForHeaderInSection then the height works properly. Make sure you have set the view header in section.
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView.init(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 44))
view.backgroundColor = .white
return view
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 44
}
Upvotes: 3
Reputation: 14040
Make sure that you set the tableview's delegate to be your viewcontroller (and make sure that your viewcontroller conforms to the UITableViewDelegate
protocol):
class CustomTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView! {
didSet {
tableView.dataSource = self
tableView.delegate = self
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 5
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 20
}
}
Results in:
Upvotes: 0
Reputation: 2425
Add this in your viewDidLoad()
:
let HEADER_HEIGHT = 100
tableView.tableHeaderView?.frame.size = CGSize(width: tableView.frame.width, height: CGFloat(HEADER_HEIGHT))
Another way -
public override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 100
}
Upvotes: 0