1

I'm building an iOS app with a collection view inside the table view. I'm having three rows each row having a collection view inside it. I am planning to have three sections each section for each row. For example row, one should be in a separate section with a header and similarly for rows 2 and 3. Whenever I create three sections I'm getting all the three rows in all three sections. I want to have a separate section with a header for each row.

import UIKit

class StoreVC: UIViewController,UITableViewDelegate,UITableViewDataSource {


    @IBOutlet weak var CourseTableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        CourseTableView.tableFooterView = UIView()
        
        CourseTableView.delegate = self
        CourseTableView.dataSource = self
    }
    
   func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if section == 0
        {
            return "Courses"
        }
        
        else if section == 1
        {
            return "Tests"
        }
        
        return "Bundles"
    }
    
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      return 1
    }
    

    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       
        
        if indexPath.row == 0
        {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CourseRow
            
            return cell
        }
        
        else if indexPath.row == 1
        {
            let cell = tableView.dequeueReusableCell(withIdentifier: "testcell", for: indexPath) as! TestRow
            
            return cell
        }
        
        else if indexPath.row == 2
        
        {
            let cell = tableView.dequeueReusableCell(withIdentifier: "bundlecell", for: indexPath) as! BundleRow
            
            return cell
        }
        
        return UITableViewCell()
        
        
        
    }
    
    
    


}
Joshua
  • 3,055
  • 3
  • 22
  • 37
user11640506
  • 57
  • 2
  • 8
  • `numberOfSections(in...) { return 3 }` is a swift method for tableViews. Then in your `tableView(cellForRowAt..) { if section == 1 { //doSomething } else if section == 2 { //doSomething } else { //doSomething }` finally setup `numberOfRowsInSection to return 1` – xTwisteDx May 18 '20 at 14:19

1 Answers1

1

Try this code on your Xcode-playground and customize as you need. import UIKit import PlaygroundSupport

class ViewController: UITableViewController {

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

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

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

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = UILabel()
        headerView.text = "Header: \(section)"
        return headerView
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.textLabel?.text = "Cell: \(indexPath)"
        return cell
    }
}

PlaygroundPage.current.liveView = ViewController()
Frankenstein
  • 15,732
  • 4
  • 22
  • 47