Reputation: 71
converting my code to swift ONLY so no use of storyboards. After rewriting the code (no storyboard usage) the header information is NOT shown !!
If I change the viewForHeaderInSection return value from:
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCell(withIdentifier: "headerId") as! ListsVCHeader
cell.fillHeader(header: listTitles[section])
return cell.contentView
}
to:
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCell(withIdentifier: "headerId") as! ListsVCHeader
cell.fillHeader(header: listTitles[section])
return cell // ==> removed .contectView
}
the header content is shown BUT the section header moves left performing a swipe gesture that I use to initiate a row delete. Any suggestions? Find the relevant code below.
class ListsVC: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(ListsVCHeader.self, forCellReuseIdentifier: "headerId")
// register other Cells
}
override func numberOfSections(in tableView: UITableView) -> Int {
return listTitles.count
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int)
-> String? {
return listTitles[section]
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCell(withIdentifier: "headerId") as! ListsVCHeader
cell.fillHeader(header: listTitles[section])
return cell.contentView
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return HeaderSectionHeight
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
switch editingStyle {
case .delete:
// mycode
default:
break
}
}
}
the Section Header cell
class ListsVCHeader: UITableViewCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
// add subviews and constraints
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func fillHeader (header: String) {
name.text = header
}
}
Upvotes: 0
Views: 49
Reputation: 2193
Change your register
in viewDidLoad
to:
tableView.register(ListsVCHeader.self, forHeaderFooterViewReuseIdentifier: "headerId")
Your class ListsVCHeader
should be changed to:
class ListsVCHeader: UITableViewHeaderFooterView
and last change the following code:
let cell = tableView.dequeueReusableCell(withIdentifier: "headerId") as! ListsVCHeader
to
let cell = tableView.dequeueReusableHeaderFooterView(withIdentifier: "headerId) as! ListsVCHeader
Upvotes: 1