-1

I have header to 1.section but I want to add 3.section. How can i do it?

main screen is have 3 section. I want to add header as views this my header to first header

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let view = UIView()
    view.sizeToFit()
    view.backgroundColor = .clear
    let titleLabel = UILabel(frame: CGRect(x: 0, y: 73, width: 250, height: 100))
    titleLabel.backgroundColor = .clear
    titleLabel.text = "Choose your \ntopics"
    titleLabel.textAlignment = .left
    titleLabel.numberOfLines = 0
    titleLabel.font = UIFont(name: "SFCompactDisplay-Semibold", size: 38)
    titleLabel.textColor = UIColor(red: 38/255, green: 50/255, blue: 91/255, alpha: 1)
    view.addSubview(titleLabel)
    return view   
}

this is the properties of the tableView

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        switch cells[section] {
        case .collection:
            return cells.count
        case .view:
            return 1
        case .list:
            return cells.count
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        switch cells[indexPath.row] {
        
        case .collection:
            let cell = tableView.dequeueReusableCell(withIdentifier: CollectionTableViewCell.identifier,
                                                     for: indexPath) as! CollectionTableViewCell
            cell.configure(with: models)
            return cell
            
        case .view:
            let cell = tableView.dequeueReusableCell(withIdentifier: FeaturedArticleTableViewCell.identifier,
                                                     for: indexPath) as! FeaturedArticleTableViewCell
            cell.configure(with: models[indexPath.row])
            return cell
            
        case .list:
            let cell = tableView.dequeueReusableCell(withIdentifier: ArticlesTableViewCell.identifier,
                                                     for: indexPath) as! ArticlesTableViewCell
            return cell
        }
        
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        switch cells[indexPath.row] {
        
        case .collection:
            return 67
            
        case .view:
            return 347
            
        case .list:
            return 130
        }
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 194
    }
    

This is screenshot, I want header where I write on the screenshot

enter image description here

Asperi
  • 228,894
  • 20
  • 464
  • 690

1 Answers1

0

Return a different view based on the section parameter:

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

    if section == 0 {
        let view = UIView()
        view.sizeToFit()
        view.backgroundColor = .clear
        let titleLabel = UILabel(frame: CGRect(x: 0, y: 73, width: 250, height: 100))
        titleLabel.backgroundColor = .clear
        titleLabel.text = "Choose your \ntopics"
        titleLabel.textAlignment = .left
        titleLabel.numberOfLines = 0
        titleLabel.font = UIFont(name: "SFCompactDisplay-Semibold", size: 38)
        titleLabel.textColor = UIColor(red: 38/255, green: 50/255, blue: 91/255, alpha: 1)
        view.addSubview(titleLabel)
        return view   
    }
    
    if section == 2 {
        let view = UIView()
        // configure your view for the 3rd section header
        return view
    }

}
DonMag
  • 69,424
  • 5
  • 50
  • 86