swift4中tableview内部的可扩展下拉菜单 [英] expandable drop down inside tableview in swift4

查看:89
本文介绍了swift4中tableview内部的可扩展下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过在swift4的tableview中进行可扩展的下拉菜单,但无法运行下面的代码.如果我将自定义模型类更改为String,则它可以正常工作.我的要求使用下面的自定义模型类.请检查我下面的代码.

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
        }
}

必填输出:

员工工作>员工雇用>

Employee Working > Employee Hired >

如果单击Employee Working,则将显示以下输出:

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

员工工作V约翰网页

亚历克斯手机

推荐答案

故事板设计

在上图中,您可以看到有两个标签将显示动态数据.在每个单元格索引上单击,它将展开该单元格,如您在下面的屏幕中看到的那样.

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.

简单视图如下所示

在扩展单元格之后,它将显示如下

因此,您需要执行以下所有步骤:-首先创建一个变量,用于存储ex:-

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 
    }
   }

这篇关于swift4中tableview内部的可扩展下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆