Home > Software engineering >  How to add a custom cell of uitableviewcell type as tableview header?
How to add a custom cell of uitableviewcell type as tableview header?

Time:01-06

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:

  1. TableHeader is taking a lot of space for which I am not sure how to assign height
  2. TableHeader has a button which is not clickable.

CodePudding user response:

You should use the UITableViewDelagates and Datasource functions for the sections.

Try this:


func numberOfSections(in tableView: UITableView) -> Int {

        return 1
    }

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

      if let headerView = tableView.dequeueReusableCell(withIdentifier: "CustomCellHeader") as? CustomCellHeader {
            cell.configure(tit: model.title)
            cell.delegate = self
      return headerView
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

        return 30
    }

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        return 110
    }

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

      return anyArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

      if let cell = tableView.dequeueReusableCell(withIdentifier: "TableCell") as? TableCell {
            cell.configureCell(with: model.data[indexPath.row])

            return cell
        }
}

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.

Hope this works.

CodePudding user response:

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

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCellHeader") as? CustomCellHeader {
     cell.configure(tit: model.title)
     cell.delegate = self
     return cell
}

and for height

 func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return yourDesireHeight
}
  •  Tags:  
  • Related