簡體   English   中英

在collectionview的部分中以id明智的方式顯示圖像

[英]Display images as id wise in section in collectionview

現在請從我的代碼中查看我在所有部分中僅獲得一張圖像

我正在創建水平集合視圖,並且集合視圖位於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
    }
}

我需要分段圖像,希望您能理解我的意思,如果有任何困惑,請在評論中提問,然后告訴我解決問題的方法

請檢查ViewController代碼

   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.您的UI層次結構應如下圖所示。

在此處輸入圖片說明

  1. MainController ,tableView單元格取決於主題數(phy,chem,math等)

     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結構

在此處輸入圖片說明

  1. 您必須在UITableViewCell中發送的數組

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM