[UITableView] Use Different Section in One Array "Expand & Collapse Table View"


//Image: original

I want to build tableview in different sections, contents of the section are:

section 1 = string array
section 2 = string array
section 3 = button

First, u must create expand and collapse table view. There are many ways to create the appearance of expanding TableView cells.  In the below code, we make the cell to be expanded .

After u create that you can follow this code bellow.


import UIKit

struct NewData {
    var isOpen: Bool = false
    var title: String = ""
    var data: [String] = []
    var data2: [String] = []
}

class ExpandExpertVC: UIViewController {
    @IBOutlet weak var tableExpand: UITableView!
    
     var dataPicker: [NewData] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupComponent()
     
        var data: [NewData] = []
        
        data.append(NewData(isOpen: false, title: "First", data: ["Produk:", "Perusahaan:", "tipe:", "Aksesoris:", " Total:"], data2: ["", "", "", "", ""]))
        data.append(NewData(isOpen: false, title: "Second", data: ["Name:", "No:", "gender:"], data2: ["", "", ""]))
        data.append(NewData(isOpen: false, title: "Other", data: [""], data2: [""]))
        
        self.dataPicker = data
    }
    
    func setupComponent() {
        tableExpand.dataSource = self
        tableExpand.delegate = self
        tableExpand.rowHeight = UITableViewAutomaticDimension
        tableExpand.estimatedRowHeight = 40
        tableExpand.separatorInset = UIEdgeInsetsMake(0, UIScreen.main.bounds.width, 0, 0)
        tableExpand.register(UINib(nibName: "IsiTableViewCell", bundle: nil), forCellReuseIdentifier: "list")
        tableExpand.register(UINib(nibName: "TitleTableViewCell", bundle: nil), forCellReuseIdentifier: "tittle")
        tableExpand.register(UINib(nibName: "ButtonTableViewCell", bundle: nil), forCellReuseIdentifier: "button")
        
    }

}

extension ExpandExpertVC: UITableViewDataSource, UITableViewDelegate {
    func numberOfSections(in _: UITableView) -> Int {
        return dataPicker.count
    }
    
    func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
        if dataPicker[section].isOpen {
            return dataPicker[section].data.count + 1
        } else {
            return 1
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "tittle", for: indexPath) as! TitleTableViewCell
            cell.titleLabel.text = dataPicker[indexPath.section].title.uppercased()
            cell.separatorInset = UIEdgeInsetsMake(1, 1, 1, 1)
            
            return cell

       } else if indexPath.section == 2 && indexPath.row == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "button", for: indexPath) as! ButtonTableViewCell
            
            return cell
        } else {
            
            let cell = tableView.dequeueReusableCell(withIdentifier: "list", for: indexPath) as! IsiTableViewCell
            cell.isilabel.text = dataPicker[indexPath.section].data[indexPath.row - 1]
            cell.detailLabel.text = dataPicker[indexPath.section].data2[indexPath.row - 1]
            return cell
        }
    }
    
    func tableView(_: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 0 {
            if dataPicker[indexPath.section].isOpen {
                dataPicker[indexPath.section].isOpen = false
                let section = IndexSet(integer: indexPath.section)
                tableExpand.reloadSections(section, with: .fade)
                print("section \(section)")
                
               
            } else {
                dataPicker[indexPath.section].isOpen = true
                let section = IndexSet(integer: indexPath.section)
                tableExpand.reloadSections(section, with: .fade)
                print("section \(section)")
            }
            
        } else {
            print("CELL section: \(indexPath.section) - row: \(indexPath.row)")
        }
    }
    
}





No comments: