I have a custom tableview cell used in many different table Views. Now, my requirement is to use the same tableview cell as a header in one of the table view. But the problem that I am facing is, I don’t want to change anything in the custom table cell but while building the cells of the tableview I want to add the header view in it.
Currently, What I am doing is something like this:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let model = model as? AParticularModel {
if let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCellHeader", for: indexPath) as? CustomCellHeader {
cell.configure(tit: model.title)
cell.delegate = self
tableView.tableHeaderView = cell.contentView
}
if let cell = tableView.dequeueReusableCell(withIdentifier: "TableCell") as? TableCell {
cell.configureCell(with: model.data[indexPath.row])
return cell
}
}
Currently the issues are:
- TableHeader is taking a lot of space for which I am not sure how to assign height
- TableHeader has a button which is not clickable.
2
Answers
You should use the UITableViewDelagates and Datasource functions for the sections.
Try this:
You can add number of sections according to your choice and also can set the header’s cell and the other’s cell height separately.
As you know , giving a headerview to tableview’s is generally use for fix a view to spesific section . If you use it in cell , when you scroll down tableview , your header is not gonna appear until you scroll to top.If you want to do this behaviour , It’s better to use sections instead of headerview.
Anyway , you can use below code to make a headerview from customcell
and for height