在 collectionview 中搜索具有多个部分的数据模型

问题描述

我的 API 响应如下:

{
  "success": true,"message": "List Loaded Successfully.","data": [
    {
      "movie_category": "Comedy","movie_category_code": "comedy","movie_list": [
        {
          "name": "Dhamaal","description": "Four lazy slacker conmen buddies who are jobless,homeless and broke learn about the secret of a hidden treasure from a dying thief and later embark on a race against time to find the mobster's buried treasure and claim it while being pursued by a determined police inspector who is hellbent to get the treasure all by himself.","release_year": "2007-09-07 07:00:00","movie_banner": "http:\/\/apis.dev.ganniti.com\/assets\/images\/dhamaal.jpg","movie_icon": "http:\/\/apis.dev.ganniti.com\/assets\/images\/dhamaal.jpg","ratings": "3.7","theatre_locations": [
            {
              "latitude": "19.099441290747407","longitude": "72.91631061735934"
            },{
              "latitude": "18.99418207785123","longitude": "72.82443855189815"
            },{
              "latitude": "19.065931910428006","longitude": "73.00118649905338"
            },{
              "latitude": "19.031441299550934","longitude": "72.88098383204391"
            }
          ]
        },{
          "name": "Bhagam Bhag","description": "Champak,the owner of a theatre group,travels to London to organise a show along with Bunty and Bablya,two of his group members. They soon find themselves embroiled in a murder they did not commit.","release_year": "2006-12-22 08:00:00","movie_banner": "http:\/\/apis.dev.ganniti.com\/assets\/images\/bhagam_bhag.jpg","movie_icon": "http:\/\/apis.dev.ganniti.com\/assets\/images\/bhagam_bhag.jpg","ratings": "3.3","longitude": "72.91631061735934"
            }
          ]
        },{
          "name": "Hulchul","description": "Anjali and Jai belong to two feuding families and pretend to be in love only to seek revenge. However,their plan goes awry when they really fall in love and decide to bring their families together.","release_year": "2004-11-26 07:30:00","movie_banner": "http:\/\/apis.dev.ganniti.com\/assets\/images\/hulchul.jpg","movie_icon": "http:\/\/apis.dev.ganniti.com\/assets\/images\/hulchul.jpg","ratings": "4","longitude": "72.82443855189815"
            }
          ]
        }
      ]
    },{
      "movie_category": "Action","movie_category_code": "action","movie_list": [
        {
          "name": "Don","description": "Vijay,a lookalike of criminal kingpin Don,is hired by DCP D'Silva in order to find Don's secrets. But after D'Silva dies,Vijay struggles to reveal his real identity.","release_year": "2006-10-20 07:00:00","movie_banner": "http:\/\/apis.dev.ganniti.com\/assets\/images\/don.jpg","movie_icon": "http:\/\/apis.dev.ganniti.com\/assets\/images\/don.jpg","ratings": "4.1",{
          "name": "Bang Bang!","description": "Harleen leads a boring life with her grandmother and works as a bank receptionist. However,her life takes a sudden turn after she falls in love with Rajveer,a thief.","release_year": "2014-10-02 08:00:00","movie_banner": "http:\/\/apis.dev.ganniti.com\/assets\/images\/bang-bang.jpeg","movie_icon": "http:\/\/apis.dev.ganniti.com\/assets\/images\/bang-bang.jpeg","ratings": "3.1",{
          "name": "Dhoom 2","description": "Mr A,a fearless thief,steals valuable artefacts and teams up with the girl he is attracted to but cannot trust. Close on their heels are three police officers trying to apprehend them.","release_year": "2006-11-24 07:30:00","movie_banner": "http:\/\/apis.dev.ganniti.com\/assets\/images\/dhoom_2.jpg","movie_icon": "http:\/\/apis.dev.ganniti.com\/assets\/images\/dhoom_2.jpg","ratings": "4.3","longitude": "72.82443855189815"
            }
          ]
        }
      ]
    }
  ]
}

它的模型类如下:

import Foundation

// MARK: - MovieModel
struct MovieModel: Codable,Equatable {
    
    static func == (lhs: MovieModel,rhs: MovieModel) -> Bool {
        return false
    }
    let success: Bool
    let message: String
    var data: [Datum]
}

// MARK: - Datum
struct Datum: Codable,Equatable {
    
    static func == (lhs: Datum,rhs: Datum) -> Bool {
        return false
    }
    let movieCategory,movieCategoryCode: String
    let movieList: [MovieList]

    enum CodingKeys: String,CodingKey {
        case movieCategory = "movie_category"
        case movieCategoryCode = "movie_category_code"
        case movieList = "movie_list"
    }
}

// MARK: - MovieList
struct MovieList: Codable,Equatable {
    static func == (lhs: MovieList,rhs: MovieList) -> Bool {
        return false
    }
    
    let name,movieListDescription,releaseYear: String?
    let movieBanner,movieIcon: String?
    let ratings: String?
    let theatreLocations: [TheatreLocation]

    enum CodingKeys: String,CodingKey {
        case name
        case movieListDescription = "description"
        case releaseYear = "release_year"
        case movieBanner = "movie_banner"
        case movieIcon = "movie_icon"
        case ratings
        case theatreLocations = "theatre_locations"
    }
}

// MARK: - TheatreLocation
struct TheatreLocation: Codable {
    let latitude,longitude: String?
}

来自 API 的这些数据将显示在多个部分和多个项目中。 集合视图数据源和委托如下:

func numberOfSections(in collectionView: UICollectionView) -> Int {
        return (self.moviemodel?.data.count)!
    }
    
    func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
        switch section {
        case 0:
            return (self.moviemodel?.data[section].movieList.count)!
        default:
            return (self.moviemodel?.data[section].movieList.count)!
        }
        
    }
    
    func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        
        let cell = alllistcollview.dequeueReusableCell(withReuseIdentifier: "Cell",for: indexPath) as! MovielistCell
 return cell
    }

现在,有一个搜索文本字段,它将搜索任何关键字并过滤要在 collectionview 中显示的结果。

搜索按钮功能如下:

  var filteredShopsA = searchDataModel?.data.filter({ item in
            print("item is ",item)
       $0.matchAddress(keyword)
              return false
           })
 moviemodel?.data = filteredShopsA!
        
  self.alllistcollview.reloadData()

扩展电影列表{

func matchAddress(_ string: String?) -> Bool {
    guard let string = string else { return true }
    guard let name = name else { return true }
    guard let desc =  movieListDescription else { return true }
    guard let year = releaseYear else { return true }
   // guard let rating = ratings else { return true }
    return (name.contains(string) || desc.contains(string) || year.contains(string))
}

}

过滤器功能是我没有得到正确的逻辑来展示集合视图上的值。如何根据集合视图的多个部分中的任何文本进行搜索

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)