Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create 3 headers in the tableview given in the images and 8 students in each row, my code is as follows <a href="https://i.stack.imgur.com/zynoj.png">https://i.stack.imgur.com/zynoj.png</a>[<a href="https://i.stack.imgur.com/zynoj.png" target="_blank" title="New Window">^</a>]


What I have tried:

class ViewController: UIViewController {

    var schoolOfList: [Any] = [
    [["school": ["VIP", "Bright International", "Motherland"]],
    [["student1": ["Chirag", "Akshay", "Shailesh", "Vishal", "Mehul", "Vinod", "Mukesh", "Darshan"]],
    ["student2": ["Manjari", "Kairav", "Abhimanyu", "Akshara", "Arohi", "Neel", "Naksh", "Naman"]],
    ["student3": ["Paritosh", "Anuj", "Kavya", "Samar", "Vanraj", "Kinjal", "Anupama", "Riyan"]]]]]
    
    @IBOutlet weak var mytableview : UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

extension ViewController:UITableViewDelegate,UITableViewDataSource{

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> (Int) {
    return schoolOfList.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for:indexPath)
   var abc = schoolOfList["school"] as! String
    cell.textLabel?.text = abc.indexPath.row
    return cell
   }

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

    func tableView(_ tableView:UITableView,viewForHeaderInSection section: Int) -> UIView? {
      
        let headerview = UIView(frame: CGRect(x:0,y:0,width:self.mytableview.frame.width,height:50))
        headerview.backgroundColor = .green
        let lable = UILabel(frame: CGRect(x:0,y:0,width:self.mytableview.frame.width,height:50))
        lable.textAlignment = .center
        lable.text = (schoolOfList["school"]as!String)
        headerview.addSubview(lable)
        return  headerview
}
Posted
Updated 2-Jan-23 23:45pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900