在collectionview的部分中以id智慧显示图像

时间:2018-12-19 07:09:48

标签: ios swift struct uicollectionview alamofire

Please see now from my code I am getting only one image in all section

我正在创建水平集合视图,并且集合视图位于tableview单元内,一切在tableview中都可以正常运行,但是在集合视图中,我无法按节显示,让我向您展示我的回应

响应

{
    "subject_list" =     (
                {
            "con_id" = 2;
            "level_id" = 1;
            "sub_id" = 4;
            "sub_list" =             (
                                {
                    "ch_id" = 17;
                    "ch_image" = "http://mobileapp.xmeducation.com/upload/1530600693.jpg";
                    "ch_name" = " 01. Measurement";
                    "con_id" = 2;
                    "level_id" = 1;
                    "sub_id" = 4;
                },
                                {
                    "ch_id" = 23;
                    "ch_image" = "http://mobileapp.xmeducation.com/upload/1451930609.jpg";
                    "ch_name" = "1. Test Chapter";
                    "con_id" = 2;
                    "level_id" = 1;
                    "sub_id" = 4;
                },
                                {
                    "ch_id" = 24;
                    "ch_image" = "http://mobileapp.xmeducation.com/upload/1884777188.jpg";
                    "ch_name" = "1. Test Chapter";
                    "con_id" = 2;
                    "level_id" = 1;
                    "sub_id" = 4;
                },
                                {
                    "ch_id" = 25;
                    "ch_image" = "http://mobileapp.xmeducation.com/upload/1518702048.jpg";
                    "ch_name" = "1. Test Chapter";
                    "con_id" = 2;
                    "level_id" = 1;
                    "sub_id" = 4;
                }
            );
            "sub_name" = Physics;
        },
                {
            "con_id" = 2;
            "level_id" = 1;
            "sub_id" = 8;
            "sub_list" =             (
                                {
                    "ch_id" = 26;
                    "ch_image" = "http://mobileapp.xmeducation.com/upload/1437196139.jpg";
                    "ch_name" = " 1. Test Chapter";
                    "con_id" = 2;
                    "level_id" = 1;
                    "sub_id" = 8;
                },
                                {
                    "ch_id" = 27;
                    "ch_image" = "http://mobileapp.xmeducation.com/upload/1903171865.jpg";
                    "ch_name" = "1. Test Chapter";
                    "con_id" = 2;
                    "level_id" = 1;
                    "sub_id" = 8;
                }
            );
            "sub_name" = Chemistry;
        },
                {
            "con_id" = 2;
            "level_id" = 1;
            "sub_id" = 9;
            "sub_list" =             (
                                {
                    "ch_id" = 31;
                    "ch_image" = "http://mobileapp.xmeducation.com/upload/1319333294.jpg";
                    "ch_name" = "1. Test Chapter";
                    "con_id" = 2;
                    "level_id" = 1;
                    "sub_id" = 9;
                }
            );
            "sub_name" = Testing;
        },
                {
            "con_id" = 2;
            "level_id" = 1;
            "sub_id" = 10;
            "sub_list" =             (
                                {
                    "ch_id" = 28;
                    "ch_image" = "http://mobileapp.xmeducation.com/upload/1373218664.jpg";
                    "ch_name" = "1. Test Chapter";
                    "con_id" = 2;
                    "level_id" = 1;
                    "sub_id" = 10;
                }
            );
            "sub_name" = "Test Subject";
        },
                {
            "con_id" = 2;
            "level_id" = 1;
            "sub_id" = 11;
            "sub_list" =             (
                                {
                    "ch_id" = 29;
                    "ch_image" = "http://mobileapp.xmeducation.com/upload/246189282.jpg";
                    "ch_name" = "1. Test Chapter";
                    "con_id" = 2;
                    "level_id" = 1;
                    "sub_id" = 11;
                }
            );
            "sub_name" = "Test Subject 1";
        },
                {
            "con_id" = 2;
            "level_id" = 1;
            "sub_id" = 12;
            "sub_list" =             (
                                {
                    "ch_id" = 30;
                    "ch_image" = "http://mobileapp.xmeducation.com/upload/1342731807.jpg";
                    "ch_name" = "1. Test Chapter";
                    "con_id" = 2;
                    "level_id" = 1;
                    "sub_id" = 12;
                }
            );
            "sub_name" = "Test Subject 2";
        }
    ); 

我想按节显示sub_list图片,但不要按节显示,让我显示我的代码

代码

var urls = [String]()
struct SubjectResponse: Decodable {
    let subjectList: [Subject]
}

struct Subject: Decodable {
    let subList: [Chapter]
}

struct Chapter: Decodable {
    let chId : String
    let chImage: String
    let chName: String
    let conId: String
    let levelId: String
    let subId: String
}



   func callSubChapAPI(){
        let preferences = UserDefaults.standard
        let studentlvl = "student_lvl"
        let student_lvl = preferences.object(forKey: studentlvl) as! String
        print(student_lvl)
        let params = ["level_id": student_lvl]
        Alamofire.request(subListWithChapter, method: .post, parameters: params).responseData() { (response) in
            switch response.result {
            case .success(let data):
                do {
                    let decoder = JSONDecoder()
                    decoder.keyDecodingStrategy = .convertFromSnakeCase

                   // self.subjects = try decoder.decode(SubjectResponse.self, from: data)



                    let subjects = try decoder.decode(SubjectResponse.self, from: data);                                       print(subjects)
                    var indexPath: IndexPath?


                        let url = subjects.subjectList[indexPath?.section ?? 0].subList[indexPath?.row ?? 0].chImage
                        print(url)
                        self.urls.append(url)



                    self.collView.reloadData()
                } catch {
                    print(error.localizedDescription)
                }
            case .failure(let error):
                print(error.localizedDescription)
            }
        }
    }
}

extension ExploreTableViewCell : UICollectionViewDataSource,UICollectionViewDelegate {

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return self.urls.count

    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        //let section1 = subjects?.subjectList[section]
        return urls[section].count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! ExploreCollectionViewCell
      let imageUrl = URL(string: urls[indexPath.row])!
        cell.imageView.sd_setImage(with: imageUrl, placeholderImage: UIImage(named: "logo_grey"), options: .refreshCached, completed: nil)
        //let name = subjects?.subjectList[indexPath.section].subList[indexPath.row].chName
        //cell.lblChapName.text = name
        return cell
    }
}

我想要逐段图像,希望您能理解我要告诉您的意思,如果有任何困惑,请在评论中提问,然后告诉我解决问题的方法

请检查视图控制器代码

   func callSubChapAPI(){
        let preferences = UserDefaults.standard
        let studentlvl = "student_lvl"
        let student_lvl = preferences.object(forKey: studentlvl) as! String
        print(student_lvl)
        let params = ["level_id": student_lvl]
        Alamofire.request(subListWithChapter, method: .post, parameters: params).responseJSON(completionHandler: {(response) in
            switch response.result{
            case.success(let value):

                let result = response.result

                if let dict = result.value as? Dictionary<String,AnyObject>{
                    if let categorylist = dict["subject_list"]{
                        self.subList = categorylist as! [AnyObject]
                        print(self.subList)
                        self.tbllist.reloadData()
                    }
                }
            case.failure(let error):
                print(error.localizedDescription)
            }

        })
    }

表格视图方法

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return subList.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! ExploreTableViewCell
        let name = subList[indexPath.row]["sub_name"]
        cell.lblTitle.text = name as? String
        return cell
    }

1 个答案:

答案 0 :(得分:0)

1。您的UI层次结构应如下图所示。

enter image description here

  1. MainController中,您的tableView单元格取决于您的主题数量(物理,化学,数学等)

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return subject_List.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! ExploreTableViewCell
    
        cell.lblTitle.text = subject_List[indexPath.row]. sub_name
        cell.arrSubList = subject_List.sub_list
        return cell
    }
    
  2. ExploreTableViewCell

    class ExploreTableViewCell : UICollectionViewDataSource,UICollectionViewDelegate {
    
        var arrSubList = [sub_list]() 
    
        func numberOfSections(in collectionView: UICollectionView) -> Int {
            return self.arrSubList.count
    
        }
    
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            //let section1 = subjects?.subjectList[section]
            return arrSubList.count
        }
    
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! ExploreCollectionViewCell
    
            let aSubList = arrSubList[indexPath.row]
    
            let imageUrl = URL(string: aSubList.ch_image)!
            cell.imageView.sd_setImage(with: imageUrl, placeholderImage: UIImage(named: "logo_grey"), options: .refreshCached, completed: nil)
            cell.lblChapName.text = aSubList.ch_name
            return cell
        }
    }
    
  

注意:

可能是因为我没有正确的模型名称和使用的约定而导致数据类型错误或其他错误。

从上面您了解了如何在MVC中轻松维护复杂结构的基本思想。对于任何查询,您可以进一步询问。

  

编辑

  1. 您的api结构

enter image description here

  1. 您必须在UITableViewCell中发送的数组

enter image description here