UITableView With Multiple Sections

后端 未结 6 1681
名媛妹妹
名媛妹妹 2020-12-04 19:36

I want to make tableView with multiple section but i do not want to use dictionary i have two arrays i want that first array should be loaded in first section and second in

6条回答
  •  感情败类
    2020-12-04 20:09

    Here are the relevant Swift 3 lines:

    func numberOfSections (in tableView: UITableView) -> Int {
        ...
        return numberOfSections
    }
    
    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        //Not necessary, but will let you format the section headers. Example:
        guard let header = view as? UITableViewHeaderFooterView else { return }
        header.textLabel?.font = UIFont.boldSystemFont(ofSize: 24)
        header.textLabel?.textColor = UIColor.darkGray
        header.textLabel?.textAlignment = .center
        header.textLabel?.frame = header.frame
        view.tintColor = UIColor.white
    }
    
    func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {
        //Also not necessary, but clear footers help space out sections
        view.tintColor = UIColor.clear   
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        //Spacing between sections:
        return 25
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        ...
        return sectionCount
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //All your cell setup
        ...
        //I call this to get the absolute row number (disregarding sections)
        let row = getAbsoluteRow_InCurrentSection(indexPath: indexPath)
        //Now you can use the row number to grab a value from an array or CoreData object and use the value to populate your cell!! 
    }
    
    func getAbsoluteRow_InCurrentSection(indexPath: IndexPath) -> Int {
        var aggRows = 0
        if currentListEntity == "GrocTask" {
            let curSection = indexPath.section
            for i in 0..

    //Please be aware, I only included those directly relevant to building a UITableView with sections. I did not include editActions, didSelect, or other UITableView delegate functions.

提交回复
热议问题