如何使用swift3在TableViewCell中的CollectionViewCell中动态显示来自Server的数据? [英] How to display dynamically data from Server in CollectionViewCell in TableViewCell with swift3?

查看:17
本文介绍了如何使用swift3在TableViewCell中的CollectionViewCell中动态显示来自Server的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I got my json link data from TableViewCell , and then retrieve that data from server and display in collectionView with related TableViewCell data. How to display this data in swift3? Please, help me.

我从 TableViewCell 得到了 url 链接 (mainThemeList.main_related_url,main_name).

I got url link (mainThemeList.main_associated_url,main_name) from TableViewCell.

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
    }

然后是来自服务器的数据相关的url链接数据.

And then data related url link data from Server.

 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 中的服务器数据存储中检索数据.

Retrieve data from Server data store in assetsTable.

然后assetsTable数据显示在CollectionViewCell中.

And then assetsTable data display in 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 problem is collectionViewCell data are duplicate with previous assetsTable data and didn't show correct data in CollectionView.

My tableViewCell 显示(Action, Drama) 标签和My CollectionViewCell 显示电影名称和电影图像.我从服务器检索 CollectionViewCell 的数据,但 CollectionViewCell 没有显示相关数据.

My tableViewCell show (Action, Drama) label and My CollectionViewCell show movies Name and Movie Image. I retrieve data for CollectionViewCell from server but CollectionViewCell didn't display related data.

推荐答案

在HomeVideoCell子类中清理prepareforreuse

in HomeVideoCell Subclass clean up data in prepareforreuse

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

这篇关于如何使用swift3在TableViewCell中的CollectionViewCell中动态显示来自Server的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆