如何将数据传递到嵌入在TableViewCell中的UICollectionView(xCode-iOS-Swift 3) [英] How pass data to an UICollectionView embedded in a TableViewCell (xCode - iOS - Swift 3)

查看:318
本文介绍了如何将数据传递到嵌入在TableViewCell中的UICollectionView(xCode-iOS-Swift 3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在TableViewCell中实现了CollectionView,但是我需要读取CollectionView上设置单元格的动态数据.

I implemented a CollectionView in a TableViewCell but I need read dynamic data for set cells on CollectionView.

我可以从TableViewCell类读取扩展中的数据,也许可以帮助我将数据从ViewController传递到TableViewCell类.

I can read data on extension from class TableViewCell, maybe can help me pass data from ViewController to the class TableViewCell.

 class MultipleTableViewCell: UITableViewCell {

        @IBOutlet fileprivate weak var collectionView: UICollectionView!

    }

    extension MultipleTableViewCell : UICollectionViewDataSource {

        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 3 // array.count
        }

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

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

     let dict = array.object(at: indexPath.row) as! NSDictionary //Like this

            cell.name.text = "How do you read data from ViewController"
            cell.email.text = "How do you read data from ViewController"
            cell.phone.text =  dict["phone"] as? String //Like this
            cell.image.image = "How do you read data from ViewController"

            return cell
        }

    }

    extension MultipleFoodTableViewCell : UICollectionViewDelegateFlowLayout {

        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

            let itemsPerRow:CGFloat = 1
            let hardCodedPadding:CGFloat = 20
            let itemWidth = (collectionView.bounds.width / itemsPerRow) - hardCodedPadding
            let itemHeight = collectionView.bounds.height - (2 * hardCodedPadding)
            return CGSize(width: itemWidth, height: itemHeight)
        }

    }

推荐答案

最简单的方法是在MultipleTableViewCell中使用要填充CollectionView的数据源数组创建一个方法.现在,在TableViewcellForRowAt方法中调用此方法.

The simplest way is create one method inside your MultipleTableViewCell with datasource array that you want to fill with CollectionView. Now call this method in cellForRowAt method of TableView.

class MultipleTableViewCell: UITableViewCell {

     @IBOutlet fileprivate weak var collectionView: UICollectionView!

     var array = [String]() //Change with Your array type

     func fillCollectionView(with array: [String]) {
          self.array = array
          self.collectionView.reloadData()
     }
}

现在在cellForRowAt中调用此方法,并将数据源数组传递给collectionView.

Now call this method in cellForRowAt and pass the datasource array for collectionView.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier") as! MultipleTableViewCell
    cell.fillCollectionView(with: ["A","B","C"]) //Pass your array
    return cell
}

这篇关于如何将数据传递到嵌入在TableViewCell中的UICollectionView(xCode-iOS-Swift 3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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