Я показываю данные как Expendable Tableview - PullRequest
0 голосов
/ 09 мая 2019

Я показываю данные, как расходуемые таблицы с разделом заголовка.Заголовок показывает нормально.но когда я показываю строки.(Как и в продукте 5 дополнений, данные отображаются так же, как в расходуемом Таблице Каждая строка с одинаковым заголовком имени типа).Я прилагаю скриншот.Какой тип данных отображается.

Данные изображения отображаются как: 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 Ответ

1 голос
/ 09 мая 2019

Вы должны позвонить override func prepareForReuse() внутри класса CollapsibleTableViewHeader.

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

Не могли бы вы предоставить код CollapsibleTableViewHeader класса?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...