Skip to content

Unable to reload the data at runtime. #13

Open

Description

@sazidIqabal

How to reload the data with updated data at runtime?

I am trying to reload the data after the tap on didselectRowAtIndexPath method
table reload fine but when I am scrolling the table than crash occurs in
below method

public func cellIdentifierUsingTableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> Node{
return arrayVisibles[indexPath.row]
}

Can you please send the solution to the problem, I am stuck in solving the problem

Activity

  1. KiranJasvanee commented on Mar 16, 2018

    @KiranJasvanee
    Owner
  2. sazidIqabal commented on Mar 16, 2018

    @sazidIqabal
    Author

    I only want to reload the data as normally we do

  3. KiranJasvanee commented on Mar 16, 2018

    @KiranJasvanee
    Owner
  4. sazidIqabal commented on Mar 16, 2018

    @sazidIqabal
    Author

    import UIKit
    import KJExpandableTableTree

    class POIPlacesVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
    // KJ Tree instances -------------------------
    var arrayTree:[Parent] = []
    var kjtreeInstance: KJTree = KJTree()

    @IBOutlet weak var tblList: UITableView!
    @IBOutlet weak var viewHeader: UIView!
    @IBOutlet weak var lblHeader: UILabel!
    var arrayParents: NSArray?
    
    override func viewDidLoad() {
        super.viewDidLoad()
    

    // let filepath: String? = Bundle.main.path(forResource: "Tree", ofType: "json")
    // let url = URL(fileURLWithPath: filepath ?? "")
    //
    // var jsonData: Data?
    // do {
    // jsonData = try Data(contentsOf: url)
    // }catch{
    // print("error")
    // }
    //
    // var jsonDictionary: NSDictionary?
    // do {
    // jsonDictionary = try JSONSerialization.jsonObject(with: jsonData!, options: .init(rawValue: 0)) as? NSDictionary
    // }catch{
    // print("error")
    // }
    //
    //// var arrayParents: NSArray?
    // if let treeDictionary = jsonDictionary?.object(forKey: "Tree") as? NSDictionary {
    // if let arrayOfParents = treeDictionary.object(forKey: "Parents") as? NSArray {
    // arrayParents = arrayOfParents
    // }
    // }

        if let arrayOfParents = arrayParents {
            kjtreeInstance = KJTree(parents: arrayOfParents, childrenKey: "Children", expandableKey: "Expanded", key: "Id")
    

    // kjtreeInstance = KJTree(parents: poiDummyDat as NSArray, childrenKey: "Children", expandableKey: "Expanded", key: "Id")
    }
    kjtreeInstance.isInitiallyExpanded = true
    kjtreeInstance.animation = .fade

        print(arrayParents?.count)
        tblList.delegate = self
        tblList.dataSource = self
        tblList.rowHeight = UITableViewAutomaticDimension
        tblList.estimatedRowHeight = 44
        
    }
    
    func resetTblData() {
        if let arrayOfParents = arrayParents {
            kjtreeInstance = KJTree(parents: arrayOfParents, childrenKey: "Children", expandableKey: "Expanded", key: "Id")
        }
        
       // kjtreeInstance.isInitiallyExpanded = true
       // kjtreeInstance.animation = .fade
    
        
        print(arrayParents?.count)
        tblList.delegate = self
        tblList.dataSource = self
        tblList.rowHeight = UITableViewAutomaticDimension
        tblList.estimatedRowHeight = 44
       // tblList.reloadData()
    }
    
    @IBAction func tapBack(_ sender: UIButton) {
        self.navigationController?.popViewController(animated: true)
    }
    
    
    
    
    @IBAction func tapToSeeMap(_ sender: UIButton) {
    

    // let mapVC = self.storyboard?.instantiateViewController(withIdentifier: "POIMapsVC")as! POIMapsVC
    // self.navigationController?.pushViewController(mapVC, animated: true)
    }

    }

    extension POIPlacesVC {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let total = kjtreeInstance.tableView(tableView, numberOfRowsInSection: section)
        return total
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let node = kjtreeInstance.cellIdentifierUsingTableView(tableView, cellForRowAt: indexPath)
        let indexTuples = node.index.components(separatedBy: ".")
        
        if indexTuples.count == 1  {
            
            // Parents
            let cellIdentifierParents = "ParentsTableViewCellIdentity"
            var cellParents: ParentsTableViewCell? = tableView.dequeueReusableCell(withIdentifier: cellIdentifierParents) as? ParentsTableViewCell
            if cellParents == nil {
                tableView.register(UINib(nibName: "ParentsTableViewCell", bundle: nil), forCellReuseIdentifier: cellIdentifierParents)
                cellParents = tableView.dequeueReusableCell(withIdentifier: cellIdentifierParents) as? ParentsTableViewCell
            }
            cellParents?.cellFillUp(indexParam: node.index, tupleCount: indexTuples.count)
            cellParents?.selectionStyle = .none
            
            if node.state == .open {
                cellParents?.buttonState.setImage(UIImage(named: "minus"), for: .normal)
            }else if node.state == .close {
                cellParents?.buttonState.setImage(UIImage(named: "plus"), for: .normal)
            }else{
                cellParents?.buttonState.setImage(nil, for: .normal)
            }
            
            return cellParents!
            
        }else if indexTuples.count == 2{
            
            // Parents
            let cellIdentifierChilds = "Childs2ndStageTableViewCellIdentity"
            var cellChild: Childs2ndStageTableViewCell? = tableView.dequeueReusableCell(withIdentifier: cellIdentifierChilds) as? Childs2ndStageTableViewCell
            if cellChild == nil {
                tableView.register(UINib(nibName: "Childs2ndStageTableViewCell", bundle: nil), forCellReuseIdentifier: cellIdentifierChilds)
                cellChild = tableView.dequeueReusableCell(withIdentifier: cellIdentifierChilds) as? Childs2ndStageTableViewCell
            }
            cellChild?.cellFillUp(indexParam: node.index)
            cellChild?.selectionStyle = .none
            
            if node.state == .open {
                cellChild?.buttonState.setImage(UIImage(named: "minus"), for: .normal)
            }else if node.state == .close {
                cellChild?.buttonState.setImage(UIImage(named: "plus"), for: .normal)
            }else{
                cellChild?.buttonState.setImage(nil, for: .normal)
            }
            
            return cellChild!
            
        }else if indexTuples.count == 3{
            
            // Parents
            let cellIdentifierChilds = "Childs3rdStageTableViewCellIdentity"
            var cellChild: Childs3rdStageTableViewCell? = tableView.dequeueReusableCell(withIdentifier: cellIdentifierChilds) as? Childs3rdStageTableViewCell
            if cellChild == nil {
                tableView.register(UINib(nibName: "Childs3rdStageTableViewCell", bundle: nil), forCellReuseIdentifier: cellIdentifierChilds)
                cellChild = tableView.dequeueReusableCell(withIdentifier: cellIdentifierChilds) as? Childs3rdStageTableViewCell
            }
            cellChild?.cellFillUp(indexParam: node.index)
            cellChild?.selectionStyle = .none
            
            if node.state == .open {
                cellChild?.buttonState.setImage(UIImage(named: "minus"), for: .normal)
            }else if node.state == .close {
                cellChild?.buttonState.setImage(UIImage(named: "plus"), for: .normal)
            }else{
                cellChild?.buttonState.setImage(nil, for: .normal)
            }
            
            return cellChild!
            
        }
        else if indexTuples.count == 4{
            
            // Parents
            let cellIdentifierChilds = "Childs4thStageTableViewCellIdentity"
            var cellChild: Childs4thStageTableViewCell? = tableView.dequeueReusableCell(withIdentifier: cellIdentifierChilds) as? Childs4thStageTableViewCell
            if cellChild == nil {
                tableView.register(UINib(nibName: "Childs4thStageTableViewCell", bundle: nil), forCellReuseIdentifier: cellIdentifierChilds)
                cellChild = tableView.dequeueReusableCell(withIdentifier: cellIdentifierChilds) as? Childs4thStageTableViewCell
            }
            cellChild?.cellFillUp(indexParam: node.index)
            cellChild?.selectionStyle = .none
            
            if node.state == .open {
                cellChild?.buttonState.setImage(UIImage(named: "minus"), for: .normal)
            }else if node.state == .close {
                cellChild?.buttonState.setImage(UIImage(named: "plus"), for: .normal)
            }else{
                cellChild?.buttonState.setImage(nil, for: .normal)
            }
            
            return cellChild!
            
        }
        else{
            // Childs
            // grab cell
            var tableviewcell = tableView.dequeueReusableCell(withIdentifier: "cellidentity")
            if tableviewcell == nil {
                tableviewcell = UITableViewCell(style: .default, reuseIdentifier: "cellidentity")
            }
            tableviewcell?.textLabel?.text = node.index
            tableviewcell?.backgroundColor = UIColor.yellow
            tableviewcell?.selectionStyle = .none
            return tableviewcell!
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let node = kjtreeInstance.cellIdentifierUsingTableView2(tableView, cellForRowAt: indexPath)
        let indexTuples = node.index.components(separatedBy: ".")
        if indexTuples.count == 3 {
            print(indexTuples)
            print(node.index)
            let firstLevelData = arrayParents![Int(indexTuples.first!)!] as! [String : Any] //State Level DAta
            //print(firstLevelData.count)
            print(firstLevelData)
            let secondLavelData = firstLevelData["Children"] as! NSArray
            print(secondLavelData[Int(indexTuples[1])!])
            let thirdLevelData = secondLavelData[Int(indexTuples[1])!] as! [String : Any]// Community Level Data
            print(thirdLevelData["Children"])
            print(route2Data)
            
            let arrPois = thirdLevelData["Children"] as! NSArray
    

    // let lastPoi = arrPois.lastObject as! [String : Any]// replace with current poi tupple value
    let lastPoi = arrPois[Int(indexTuples[2])!]as! [String : Any]//.lastObject as! [String : Any]
    let detailsPOI = lastPoi["details"]as! [String : Any]
    print(lastPoi)
    print(arrPois)
    print(detailsPOI)
    print(detailsPOI["diversion"])
    print(detailsPOI["id"])

            for dict in thirdLevelData["Children"]as! NSArray {
                print(dict)
                let data = dict as! [String : Any]
                print(data["diversion"])
            }
            
            
            let firstLevelData1 = route2Data[0] as! [String : Any]
            let secondLavelData1 = firstLevelData1["Children"] as! NSArray
            let thirdLevelData1 = secondLavelData1[0] as! [String : Any]
            let arrPois1 = thirdLevelData1["Children"] as! NSArray
            let firstPoi1 = arrPois1.firstObject as! [String : Any]
            let detailsPOI1 = firstPoi1["details"]as! [String : Any]
            if detailsPOI["id"]as! String == detailsPOI1["id"]as! String {
                print("can be switch other route")
               // arrayParents![Int(indexTuples.first!)!] = route2Data
                var arrRouteData: Array<Dictionary<String,Any>> = arrayParents as! Array<Dictionary<String,Any>>
                arrRouteData.remove(at: Int(indexTuples.first!)!)
                arrRouteData.insert(firstLevelData1, at: Int(indexTuples.first!)!)
                arrayParents = arrRouteData as! NSArray
                resetTblData()
                
            }
    

    // for dict in thirdLevelData["Children"]as! NSArray {
    // print(dict)
    // let data = dict as! [String : Any]
    //
    // arrDetailsData.append(data["data"] as! [String : String])
    // }
    // print(arrDetailsData)
    }

    }
    
  5. sazidIqabal commented on Mar 16, 2018

    @sazidIqabal
    Author

    I have sent the whole class and In didSelectRowAtIndexPath I need nod so I have use the same in above without expending/collapsing the cell

  6. sazidIqabal commented on Mar 20, 2018

    @sazidIqabal
    Author

    Hi, Have any solution for that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions