我正在显示类似Expendable Tableview

时间:2019-05-09 07:30:37

标签: ios swift api uitableview

我正在用标题部分显示类似消耗性表格视图的数据。标头显示正常。但是当我显示行时。 (就像产品中的5个插件一样,数据显示在消耗性的Tableview中,具有相同类型名称标题的每一行)。我正在附上屏幕截图。显示什么类型的数据。

图像数据显示为:https://imgur.com/a/JXqUbJD

我正在附上我正在使用的当前代码。我已经尝试了太多次了,但是效果不佳。


import UIKit
import Reachability
import Alamofire

class ExpendableTableview: UIViewController, UITableViewDelegate, UITableViewDataSource, CollapsibleTableViewHeaderDelegate {

    @IBOutlet weak var tableview: UITableView!
    var catid:Int!
    var reachability = Reachability()!
    var arraySubCategory = [structSubCategory]()
    struct structSubCategory {
        var id:Int
        var minimum_people:String
        var title:String
        var package_price:String
        var package_image:String
        var package_label:String
        var categoryId:Int
//        var addons: [cateringAddOns]
//        var collapsed: Bool
    }

    var arraySection = [section]()
    struct section
    {
        var title: String
        var addons : [cateringAddOns]
        var collapsed: Bool
    }

    var arrayCateringAddOns = [cateringAddOns]()
    struct cateringAddOns {
        var no_of_items:Int
        var add_on_type:Int
        var package_item:String
        var upgrade_price:String
    }

    var NormalArr = ["Balance", "Sales", "Order Status", "Social Media Complaints", "Logout"]


    override func viewDidLoad() {
        super.viewDidLoad()

        print(catid)
        SubCateringAPI(cateringId: catid)
        // Do any additional setup after loading the view.
    }

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



    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arraySection[section].collapsed ? 0 : arraySection[section].addons.count

    }



    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CellRID") as! ExpendableAddOnsCell

        let item : cateringAddOns = arraySection[indexPath.section].addons[indexPath.row]
        cell.lblAddon.text = item.package_item


        return cell
    }


    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "header") as? CollapsibleTableViewHeader ?? CollapsibleTableViewHeader(reuseIdentifier: "header")

        header.titleLabel.text = arraySection[section].title
        header.arrowLabel.text = ">"
        header.setCollapsed(arraySection[section].collapsed)
        header.section = section
        header.delegate = self

        return header
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 44.0
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 1.0
    }

    func toggleSection(_ header: CollapsibleTableViewHeader, section: Int) {
        let collapsed = !arraySection[section].collapsed

        // Toggle collapse
        arraySection[section].collapsed = collapsed
        header.setCollapsed(collapsed)

        tableview.reloadSections(NSIndexSet(index: section) as IndexSet, with: .automatic)
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }

//    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
//        return arraySection[section].title
//    }
//





    @IBAction func btnDismiss(_ sender: Any) {
        dismiss(animated: true, completion: nil)
    }



    func SubCateringAPI(cateringId:Int)
    {
        if (reachability.connection == .wifi) || (reachability.connection == .cellular)
        {

            SwiftLoader.show(animated: true)
            let url = BaseUrl + ViewController.sharedInstance.subCatering + "cateringId=\(cateringId)"
            print(url)

            Alamofire.request(url, method: .get, parameters: nil, encoding: URLEncoding.default).responseJSON { response in

                switch response.result {
                case .success:
                    let json = response.result.value
                    print(json)

                    let code = (json as AnyObject).object(forKey: "code") as! Int
                    print(code)

                    if code == 200
                    {
                        let data = (json as AnyObject).object(forKey: "data") as? NSArray

                        for alldata in data!
                        {
                            let id = (alldata as AnyObject).object(forKey: "id") as! Int
                            let minimum_people = (alldata as AnyObject).object(forKey: "minimum_people") as! String
                            let title = (alldata as AnyObject).object(forKey: "title") as! String
                            let package_price = (alldata as AnyObject).object(forKey: "package_price") as! String
                            let package_image = (alldata as AnyObject).object(forKey: "package_image") as! String
                            let package_label = (alldata as AnyObject).object(forKey: "package_label") as! String
                            let categoryId = (alldata as AnyObject).object(forKey: "categoryId") as! Int

                            let arr = structSubCategory(id: id, minimum_people: minimum_people, title: title, package_price: package_price, package_image: package_image, package_label: package_label, categoryId: categoryId)
                            self.arraySubCategory.append(arr)



                        }
                        for i in self.arraySubCategory
                        {
                            let id = i.categoryId
                            let title = i.package_label
                            self.cateringAddOndsAPI(adonsId: id, title: title)
                        }
                        SwiftLoader.hide()
                    }
                    else
                    {

                    }
                case .failure:
                    print("error")
                }
            }
        }
        else
        {
            alert(title: "", message: "Please Check Your Internet Connection")
        }

    }




    func cateringAddOndsAPI(adonsId:Int, title: String)
    {
        if (reachability.connection == .wifi) || (reachability.connection == .cellular)
        {
            arraySection.removeAll()
            arrayCateringAddOns.removeAll()
            SwiftLoader.show(animated: true)
            let url = BaseUrl + ViewController.sharedInstance.cateringAddOnds + "adonsId=\(adonsId)"
            print(url)

            Alamofire.request(url, method: .get, parameters: nil, encoding: URLEncoding.default).responseJSON { response in

                switch response.result {
                case .success:
                    let json = response.result.value
                    print(json)

                    let code = (json as AnyObject).object(forKey: "code") as! Int
                    print(code)

                    if code == 200
                    {
                        let data = (json as AnyObject).object(forKey: "data") as? NSArray

                        for alldata in data!
                        {
                            let no_of_items = (alldata as AnyObject).object(forKey: "no_of_items") as! Int
                            let add_on_type = (alldata as AnyObject).object(forKey: "add_on_type") as! Int
                            let package_item = (alldata as AnyObject).object(forKey: "package_item") as! String
                            let upgrade_price = (alldata as AnyObject).object(forKey: "upgrade_price") as! String

                            let arr = cateringAddOns(no_of_items: no_of_items, add_on_type: add_on_type, package_item: package_item, upgrade_price: upgrade_price)
                            self.arrayCateringAddOns.append(arr)

                            let expandData = section(title: title, addons: [arr], collapsed: false)
                            self.arraySection.append(expandData)

                        }
                        self.tableview.reloadData()
                        SwiftLoader.hide()
                    }
                    else
                    {

                    }
                case .failure:
                    print("error")
                }
            }
        }
        else
        {
            alert(title: "", message: "Please Check Your Internet Connection")
        }

    }


}


1 个答案:

答案 0 :(得分:1)

您应该在课程override func prepareForReuse()内致电CollapsibleTableViewHeader

override func prepareForReuse() {
    super.prepareForReuse()
    //TODO: set default values
}

您能提供CollapsibleTableViewHeader类的代码吗?