How do I populate two sections in a tableview with two different arrays using swift?

后端 未结 5 1609
旧巷少年郎
旧巷少年郎 2020-12-04 10:09

I have two arrays Data1 and Data2 and I want to populate the data within each of these (they contain strings) into a tableview in two different sections.

The first s

5条回答
  •  醉话见心
    2020-12-04 10:37

    Can do Sections in Tableview and can change the colours of Header sections

       class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    @IBOutlet weak var tabview: UITableView!
    
    var tablecell = NewTableViewCell()
    
    let data = [["SWIFT", "BALENO", "ALTO", "CIAZ"], ["INNOVA", "GLANZA", "FORTUNER"] , ["BMW X5", "BMW M4", "BMW 7 Series", "BMW X7", "BMW i3"]]
    
    let brand: Array = ["MARUTHI", "TOYOTA", "BMW"]
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
    }
    
    
     func numberOfSections(in tableView: UITableView) -> Int {
        return brand.count
    }
    
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
           return data[section].count    }
    
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    
        return self.brand[section]
    
    }
    
    
    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    
        // can change the header color of background and title with this code :)
    
        (view as! UITableViewHeaderFooterView).contentView.backgroundColor = UIColor.red.withAlphaComponent(0.4)
        (view as! UITableViewHeaderFooterView).textLabel?.textColor = UIColor.yellow
    
        }
    
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        let cell1: NewTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell1") as! NewTableViewCell
    
        let text = data[indexPath.section][indexPath.row]
    
        cell1.textLabel!.text = text
    
        return cell1
    }
    
    
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    }
    

提交回复
热议问题