CollectionView перезагрузить данные не работает быстрый IOS - PullRequest
0 голосов
/ 30 апреля 2018

Я пытаюсь получить данные с сервера и загружаю в collectionView. здесь я использую 3 collectionView в одном ViewController и использую xib в качестве collectionView cell. Я использую тот же cell для тех 3 collectionView. но я не могу перезагрузить данные в collectionView.

вот мой код.

 //MARK:- Service Cell

    func SetUpAlterationUIdata()
    {
        let headers = ["Authorization":"Token token=1aab17fe5281e49975a530db411f7de4"] as [String : String]

        self.showSpinner("Loading...", "Please wait!!")

        DispatchQueue.global(qos: .background).async {
            AFWrapper.AFsharedInstance.requestGETURL(Get_Hod_Hour_Alterations_Index_URL, headers: headers, success: { (json) in
                print(json)
                DispatchQueue.main.async {
                    self.hideSpinner()
                    let swiftyJsonVar = JSON(json)

                    // Alteration array data
                    let myAlterationsVar = swiftyJsonVar["my_alterations_to_others"].rawString()!
                    let myAlterationsjsonData = myAlterationsVar.data(using: .utf8)!
                    let myAlterationsDic = try? JSONSerialization.jsonObject(with: myAlterationsjsonData, options: [])  as! Array< Any>

                    for myAlter in myAlterationsDic! {
                        let totlDic  = NSMutableDictionary()
                        let dic = myAlter as! Dictionary<String,Any>


                      //  totlDic.setValue(dic["day_order"]!, forKey: "day_order")
                        totlDic.setValue(dic["batch"]!, forKey: "batch")
                        totlDic.setValue(dic["semester"]!, forKey: "semester")
                        totlDic.setValue(dic["section"]!, forKey: "section")
                        totlDic.setValue(dic["subject_altered"]!, forKey: "subject_altered")
                        totlDic.setValue(dic["subject_handled"]!, forKey: "subject_handled")
                        totlDic.setValue(dic["timing"]!, forKey: "timing")
                        totlDic.setValue(dic["date"]!, forKey: "date")
                        totlDic.setValue(dic["altered_to"]!, forKey: "altered_to")

                        self.myAlterationsArray.add(totlDic)
                        print("myAlterationsArray--",self.myAlterationsArray )


                    }
                    self.myAlterationsListView.reloadData()
                    // department_wise_alterations array data


                    let departmentVar = swiftyJsonVar["department_wise_alterations"].rawString()!
                    let departmentjsonData = departmentVar.data(using: .utf8)!
                    let departmentDic = try? JSONSerialization.jsonObject(with: departmentjsonData, options: [])  as! Array< Any>

                    for departments in departmentDic! {
                        let totlDic  = NSMutableDictionary()
                        let dic = departments as! Dictionary<String,Any>

                        totlDic.setValue(dic["day_order"]!, forKey: "day_order")
                        totlDic.setValue(dic["batch"]!, forKey: "batch")
                        totlDic.setValue(dic["semester"]!, forKey: "semester")
                        totlDic.setValue(dic["section"]!, forKey: "section")
                        totlDic.setValue(dic["subject_altered"]!, forKey: "subject_altered")
                        totlDic.setValue(dic["subject_handled"]!, forKey: "subject_handled")
                        totlDic.setValue(dic["timing"]!, forKey: "timing")
                        totlDic.setValue(dic["date"]!, forKey: "date")
                        totlDic.setValue(dic["altered_to"]!, forKey: "altered_to")

                        self.departmentArray.add(totlDic)
                        print("departmentArray--",self.departmentArray )
                    }
                    self.othersListView.reloadData()


                    // my_alteration_from_others array data


                    let othersalterationVar = swiftyJsonVar["my_alteration_from_others"].rawString()!
                    let othersjsonData = othersalterationVar.data(using: .utf8)!
                    let othersDic = try? JSONSerialization.jsonObject(with: othersjsonData, options: [])  as! Array< Any>

                    for othersdata in othersDic! {

                        let totlDic  = NSMutableDictionary()
                        let dic = othersdata as! Dictionary<String,Any>

                        totlDic.setValue(dic["day_order"]!, forKey: "day_order")
                        totlDic.setValue(dic["batch"]!, forKey: "batch")
                        totlDic.setValue(dic["semester"]!, forKey: "semester")
                        totlDic.setValue(dic["section"]!, forKey: "section")
                        totlDic.setValue(dic["subject_altered"]!, forKey: "subject_altered")
                        totlDic.setValue(dic["subject_handled"]!, forKey: "subject_handled")
                        totlDic.setValue(dic["timing"]!, forKey: "timing")
                        totlDic.setValue(dic["date"]!, forKey: "date")
                        totlDic.setValue(dic["altered_to"]!, forKey: "altered_to")

                        self.othersArray.add(totlDic)
                        print("othersArray--",self.othersArray )

                    }
                    self.departmentListView.reloadData()

                } } , failure: { (error) in
                    //error code
                    print(error)
            })
        }
    }


   // MARK:- UICollectionView DataSource

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if collectionView == self.othersListView {
            return othersArray.count

        }
        else if  collectionView == self.departmentListView
        {
            return departmentArray.count
        }
        return myAlterationsArray.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        var cell: UICollectionViewCell!

        if collectionView == self.myAlterationsListView {

            let cell1 = collectionView.dequeueReusableCell(withReuseIdentifier: "SKMyAlterationsCell", for: indexPath) as! SKMyAlterationsCell

            let localDic = myAlterationsArray.object(at: indexPath.row) as? NSDictionary

            cell1.dayOredrLbl.text = localDic?.value(forKey: "day_order") as? String
            cell1.batchLbl.text = localDic?.value(forKey: "batch") as? String
            cell1.semLbl.text = localDic?.value(forKey: "semester") as? String
            cell1.secLbl.text = localDic?.value(forKey: "section") as? String

            cell1.subjAlteredLbl.text = localDic?.value(forKey: "subject_altered") as? String
            cell1.subjHandledLbl.text = localDic?.value(forKey: "subject_handled") as? String
            cell1.timingLbl.text = localDic?.value(forKey: "timing") as? String
            cell1.dateLbl.text = localDic?.value(forKey: "date") as? String
            cell1.AlteredToLbl.text = localDic?.value(forKey: "altered_to") as? String

             return cell1
        }
        else if  collectionView == self.othersListView

        {
             let cell1 = collectionView.dequeueReusableCell(withReuseIdentifier: "SKMyAlterationsCell", for: indexPath) as! SKMyAlterationsCell

            let localDic = othersArray.object(at: indexPath.row) as? NSDictionary

            cell1.dayOredrLbl.text = localDic?.value(forKey: "day_order") as? String
            cell1.batchLbl.text = localDic?.value(forKey: "batch") as? String
            cell1.semLbl.text = localDic?.value(forKey: "semester") as? String
            cell1.secLbl.text = localDic?.value(forKey: "section") as? String

            cell1.subjAlteredLbl.text = localDic?.value(forKey: "subject_altered") as? String
            cell1.subjHandledLbl.text = localDic?.value(forKey: "subject_handled") as? String
            cell1.timingLbl.text = localDic?.value(forKey: "timing") as? String
            cell1.dateLbl.text = localDic?.value(forKey: "date") as? String
            cell1.AlteredToLbl.text = localDic?.value(forKey: "altered_to") as? String

            return cell1
        }
        else
        {

            let cell1 = collectionView.dequeueReusableCell(withReuseIdentifier: "SKMyAlterationsCell", for: indexPath) as! SKMyAlterationsCell

            let localDic = departmentArray.object(at: indexPath.row) as? NSDictionary
            cell1.dayOredrLbl.text = localDic?.value(forKey: "day_order") as? String
            cell1.batchLbl.text = localDic?.value(forKey: "batch") as? String
            cell1.semLbl.text = localDic?.value(forKey: "semester") as? String
            cell1.secLbl.text = localDic?.value(forKey: "section") as? String

            cell1.subjAlteredLbl.text = localDic?.value(forKey: "subject_altered") as? String
            cell1.subjHandledLbl.text = localDic?.value(forKey: "subject_handled") as? String
            cell1.timingLbl.text = localDic?.value(forKey: "timing") as? String
            cell1.dateLbl.text = localDic?.value(forKey: "date") as? String
            cell1.AlteredToLbl.text = localDic?.value(forKey: "altered_to") as? String

             return cell1

        }


        return cell

    }

}

//MARK:- collectionViewCell Nibs


    func loadNibs()
    {

        //MARK:- SKSubmissAssigmCell

        let nib = UINib(nibName: "SKMyAlterationsCell", bundle: nil)
        myAlterationsListView?.register(nib, forCellWithReuseIdentifier: "SKMyAlterationsCell")
        if let flowLayout = myAlterationsListView.collectionViewLayout as? UICollectionViewFlowLayout {
            flowLayout.estimatedItemSize = CGSize(width: 1,height: 1)
        }
        myAlterationsListView.dataSource = self



        let nib2 = UINib(nibName: "SKMyAlterationsCell", bundle: nil)
        othersListView?.register(nib2, forCellWithReuseIdentifier: "SKMyAlterationsCell")
        if let flowLayout = othersListView.collectionViewLayout as? UICollectionViewFlowLayout {
            flowLayout.estimatedItemSize = CGSize(width: 1,height: 1)
        }
        othersListView.dataSource = self




        let nib3 = UINib(nibName: "SKMyAlterationsCell", bundle: nil)
        departmentListView?.register(nib3, forCellWithReuseIdentifier: "SKMyAlterationsCell")
        if let flowLayout = departmentListView.collectionViewLayout as? UICollectionViewFlowLayout {
            flowLayout.estimatedItemSize = CGSize(width: 1,height: 1)
        }
        departmentListView.dataSource = self

    }

Вот мой код ячейки коллекции.

import UIKit

class SKMyAlterationsCell: UICollectionViewCell{

    @IBOutlet weak var dayOredrLbl:UILabel!
    @IBOutlet weak var batchLbl:UILabel!
    @IBOutlet weak var semLbl:UILabel!
    @IBOutlet weak var secLbl:UILabel!
    @IBOutlet weak var subjAlteredLbl:UILabel!
    @IBOutlet weak var subjHandledLbl:UILabel!
    @IBOutlet weak var timingLbl:UILabel!
    @IBOutlet weak var dateLbl:UILabel!
    @IBOutlet weak var AlteredToLbl:UILabel!

    @IBOutlet weak var subview:UIView!
    @IBOutlet weak var widthConstraint: NSLayoutConstraint!

    override func awakeFromNib() {
        super.awakeFromNib()
        self.subview.layer.cornerRadius = 5.0

        self.contentView.translatesAutoresizingMaskIntoConstraints = false
        let screenWidth = UIScreen.main.bounds.size.width
        widthConstraint.constant = screenWidth - (2 * 12)
        // Initialization code
    }

}

1 Ответ

0 голосов
/ 03 мая 2018

Я прошел с вашим кодом, все в порядке, я думаю, что вы делаете динамическую высоту ячейки. Удалите ячейку и создайте новую, убедитесь, что метка имеет ограничения для верха, низа, слева и справа. потом почисти и запусти он должен работает.

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