skip to Main Content

My scenario is that I have three different types of arrays that might or might not contain values. I have 3 sections with section headers for my tableview. I am having trouble finding a solution that would be to dynamically set the sections i.e, if one of my arrays doesn’t have a value then I don’t want to show the section. If 3 arrays have value then show the 3 sections or if any one of the arrays doesn’t have value then I don’t want to show that section.

4

Answers


  1. Your numberOfSections will be the number of arrays. And the numberOfRowsInSection will be the count of each arrays for that section in your tableViewDataSource.

    func numberOfSections(in tableView: UITableView) -> Int {
        
        return 3
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if section == 0 {
            return array1.count
        } else if section == 1 {
            return array2.count
        } else {
            return array3.count
        }
        
    }
    

    If there are no items in an array, then the rows will be zero for that section.

    Login or Signup to reply.
  2. You can do something like this

    // Create enum for simplifying the implementation.
    
    enum SectionType{
    case type1
    case type2
    case type3
    }
    
    class TestVC: UIViewController {
    // create to show sections only when data is available
    var sections: [SectionType] = []
    // create you array types
    var array1 = [String]()
    var array2 = [String]()
    var array3 = [String]()
    @IBOutlet weak var tableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.dataSource = self
        // Add enums to section only when array has some value
        // You can do this when you get API data
        if array1.count > 0{
            sections.append(.type1)
        }
        if array2.count > 0{
            sections.append(.type2)
        }
        if array3.count > 0{
            sections.append(.type3)
        }
    }
    
    }
    extension TestVC: UITableViewDataSource{
    func numberOfSections(in tableView: UITableView) -> Int {
        // only those sections with data wil be visible
        return sections.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        switch sections[section]{
            
        case .type1:
            return array1.count
        case .type2:
            return array2.count
        case .type3:
            return array3.count
            
        }
        
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        switch sections[indexPath.section]{
            // return different type of cells if required accourding to the data in array
        case .type1, .type2, .type3:
            return UITableViewCell()
        
    
        }
    
    }
      }
    
    Login or Signup to reply.
  3. If the arrays can change dynamically (after the view is loaded), you can implement number of sections like that:

    func numberOfSections(in tableView: UITableView) -> Int {
      var numberOfSections = 0
      if array1.count > 0 { numberOfSections++ }
      if array2.count > 0 { numberOfSections++ }
      if array3.count > 0 { numberOfSections++ }
      return numberOfSections 
    }
    
    Login or Signup to reply.
  4. Your data source should look like this – sectionModels = [[cellModels]]
    The outer array represents number of sections and inner array represents number of cells in that section.

    func numberOfSections(in tableView: UITableView) -> Int {
        return sectionModels.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sectionModels[section].count            
    }
        
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellModel = sectionModels[indexPath.section][indexPath.row]
        // Configure UITableViewCell with cellModel
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search