【问题标题】:How to display dynamically data from Server in CollectionViewCell in TableViewCell with swift3?如何使用swift3在TableViewCell中的CollectionViewCell中动态显示来自Server的数据?
【发布时间】:2017-11-22 19:41:44
【问题描述】:

我从 TableViewCell 获得了我的 json 链接数据,然后从服务器检索该数据并在 collectionView 中显示相关的 TableViewCell 数据。 如何在swift3中显示这些数据?请帮帮我。

我从 TableViewCell 获得了 url 链接 (mainThemeList.main_associated_url,main_name)。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {



        let mainThemeList = mainHomeThemeTable[(indexPath as NSIndexPath).row]

        let cell = tableView.dequeueReusableCell(withIdentifier: "homecell") as! HomeCategoryRowCell



         DispatchQueue.main.async {
            cell.categoryTitle.text = mainThemeList.main_name
            cell.mainAssociatedURL.text = mainThemeList.main_associated_url


            self.prefs.set(mainThemeList.main_name, forKey: "main_name")
            cell.categoryTitle.font = UIFont.boldSystemFont(ofSize: 17.0)
            cell.collectionView.reloadData()

        }

        DispatchQueue.main.async {

            self.retrieveDataFromServer(associated_url: mainThemeList.main_associated_url,main_name: mainThemeList.main_name)
        }

        return cell
    }

然后是来自Server的数据相关的url链接数据。

 private func retrieveDataFromServe(associated_url : String , main_name: String) {
        SwiftLoading().showLoading()
        if Reachability().isInternetAvailable() == true {

            self.rest.auth(auth: prefs.value(forKey: "access_token") as! String!)
            rest.get(url: StringResource().mainURL + associated_url ,  parma: [ "show_min": "true" ], finished: {(result : NSDictionary, status : Int) -> Void in


                self.assetsTable.removeAll()
                if(status == 200){
                    let data = result["data"] as! NSArray
                    if (data.count>0){
                        for item in 0...(data.count) - 1 {

                            let themes : AnyObject = data[item] as AnyObject
                            let created = themes["created"] as! String
                            let assets_id = themes["id"] as! Int
                            let name = themes["name"] as! String
                            var poster_img_url = themes["poster_image_url"] as! String
                            let provider_id = themes["provider_id"] as! Int

                            poster_img_url = StringResource().posterURL + poster_img_url

                            self.assetsTable.append(AssetsTableItem(main_name: main_name,created: created,assets_id: assets_id, name: name, poster_image_url: poster_img_url,provider_id: provider_id))

                        }
                    }

                    SwiftLoading().hideLoading()

                }else{

                    SwiftLoading().hideLoading()

                }
            })
        }
    }

从 assetsTable 中的服务器数据存储中检索数据。

然后将 assetsTable 数据显示在 CollectionViewCell 中。

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



    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "videoCell", for: indexPath) as! HomeVideoCell

            cell.movieTitle.text = list.name
            cell.imageView.image = list.image



    return cell

}

我的问题是 collectionViewCell 数据与以前的 assetsTable 数据重复,并且在 CollectionView 中没有显示正确的数据。

My tableViewCell 显示(动作、戏剧)标签和 My CollectionViewCell 显示电影名称和电影图像。我从服务器检索 CollectionViewCell 的数据,但 CollectionViewCell 没有显示相关数据。

【问题讨论】:

  • 编辑您的问题,并通过添加更多内容来更清楚地了解场景。
  • 所以基本上你正在尝试在 tableViewCell 中使用集合视图。并且您的问题与表视图单元格的出列有关。您必须为每个 tableViewCell 的集合视图设置单独的数据,将需要显示的 tableView 的 cellForRowAtIndex 中的数据传递。每个单元格将包含/存储/拥有自己的集合视图数据。
  • 在tableViewCell的类中创建和数组字典,并在从webservice检索到的tableView的cellForRowAtIndexPath中传递尊重的数据。
  • 我的问题是我可能不知道 Server 有多少 tableViewCell 数据。来自服务器数据的 tableViewCell 数据是动态数据。这就是为什么在我的代码中为未知数量数据单独的 CollectionView 是不行的。 @PramodKumar
  • 您必须从后端获取每个类别的数据,例如 { "Action" : [{"title":"The Dark Knight", "thumb":"url_of_thumb"},{"title ": "The Godfather", "Thumb":"url_of_thumb"}] "Drama": [{"title": "Malgudi Days", "Thumb":"url_of_thumb"}] } 等等。然后你必须创建根据此字典的总键(“Action”、“Drama”),表格视图中的总单元格。然后你必须将动作数据发送到动作单元的collectionView,将戏剧数据发送到戏剧单元的collectionView

标签: ios iphone xcode swift3 tableviewcell


【解决方案1】:

在 HomeVideoCell 子类中清理prepareforreuse

中的数据
override func prepareForReuse() {
    super.prepareForReuse()
    self.movieTitle.text = ""
    self.imageView.image = nil
}

【讨论】:

  • 我无法调用 HomeVideoCell.swift 中的单元格。怎么打电话? @MeghsDhameliya
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多