expandable drop down inside tableview in swift4

好久不见. 提交于 2021-01-28 10:23:44

问题


I tried, expandable drop down inside tableview in swift4 but not worked my below code. if i changed custom model class to String it is working. my requirement use below custom model class. please check my below code.

class ExpandViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, ExpandableHeaderViewDelegate {
        var empDetails = [EmpWorking]()
        var hiredDetails = [Hired]()

        @IBOutlet weak var tableView: UITableView!
        var sections = [SectionData]()

        override func viewDidLoad() {
            super.viewDidLoad()
            loadData()
            // Do any additional setup after loading the view.
        }

        func numberOfSections(in tableView: UITableView) -> Int {
            return sections.count
        }

        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return sections[section].emp.count
        }

        func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
            return 44
        }
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            if(sections[indexPath.section].expanded){
                return 80
            }
            else{
                return 0
            }
        }
        func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
            return 2
        }

        func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
            var header = ExpandableHeaderView()
            header.customInit(title: sections[section].genre, section: section, delegate: self)
            return header
        }

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "labelCell", for: indexPath) as! expandDataCell
            cell.lbl1.text = "\(sections[indexPath.section].emp![indexPath.item].name)"
            cell.lbl2.text = "\(sections[indexPath.section].emp![indexPath.item].dept)"
            return cell as expandDataCell
        }

        func toggleSection(header: ExpandableHeaderView, section: Int) {
            sections[section].expanded = !sections[section].expanded
            tableView.beginUpdates()
            for i in 0 ..< sections[section].emp.count
            {
                tableView.reloadRows(at: [IndexPath(row: i, section: section)], with: .automatic)
            }
            tableView.endUpdates()
        }

    func loadData(){
        let url = "http://www.json-generator.com/api/json/get/ccgFfBFWqa?indent=2"
        guard let resourceString = URL(string: url) else{return}
        let decoder = JSONDecoder()
        URLSession.shared.dataTask(with: resourceString) { (data, response, err) in
            guard let data = data else{return}
            do{
            let expandData = try decoder.decode([ExpandModel].self, from: data)
                print("Expand Data:\(expandData)")
                for i in expandData[0].emp_working{
                    print("i=\(i)")
                    self.empDetails.append(i)
                }
                for j in expandData[0].emp_recent_hired{
                    self.hiredDetails.append(j)
                }

                DispatchQueue.main.async {
//                    self.sections = [SectionData(genre: "Employee Working", emp: self.empDetails, expanded: false),
//                    SectionData(genre: "Employee Hired", emp: self.hiredDetails, expanded: false)
//                    ]
                    self.tableView.reloadData()
                }
            }
            catch let jsonErr{
                print("Expand Data Err:\(jsonErr)")

            }
        }.resume()

    }

}

struct ExpandModel: Codable{
    let emp_working: [EmpWorking]
    let emp_recent_hired: [Hired]
}

struct EmpWorking: Codable{
    let dept: String
    let name: String
}

struct Hired: Codable {
    let name: String
    let qualification: String
}


struct SectionData {
        var genre: String!
        var emp: [EmpWorking]!
        var expanded: Bool!

        init(genre: String, emp: [EmpWorking] = [], expanded: Bool)
        {
            self.genre = genre
            self.emp = emp
            self.expanded = expanded
        }
}

Required output:

Employee Working > Employee Hired >

if click on Employee Working, then it will display the below output:

Employee Working V John Web

Alex Mobile


回答1:


storyBoard design

in above image you can see there are two label where dynamic data will appear. On every cell index click it will expand the cell as you see in below screen.

simple view appear like this

After expend a cell it will appear like this

so all you need to do these steps:- first create a variable for store the didselectrowat index for ex:-

var int_row = Int()
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
        int_row = indexPath.row . // save the index path
        yourTableViewName.reloadData() // reload the table
} 

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
     if int_row == indexPath.row { // when you tap on cell then it will expend 
     the cell
        return UITableView.automaticDimension
    } else {
        return 48 // at this time only show your uppar labal only 
    }
   }


来源:https://stackoverflow.com/questions/59280032/expandable-drop-down-inside-tableview-in-swift4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!