从API中填充TableView中的CollectionView

时间:2017-12-01 12:54:47

标签: ios uitableview uicollectionview swift4

我正在使用alamofire在模型类的帮助下从数组中的API存储中获取数据。 现在我想在TableView中的CollectionView中显示这些数据。因此传递该数组以显示数据是一个问题,这里有一些代码看看你是否能找到问题。

告诉我您是否希望我再向您提供信息

tableview的cellforrowat

if let cell = tableView.dequeueReusableCell(withIdentifier: "HeadlineRow", for: indexPath) as? HeadlineRow {

            let latest = self.latest[indexPath.section]
            cell.updateUI(latest: latest)

            return cell
        }

tableviewcell类

var latestNews = [LatestNews]()

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

        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HeadlineCell", for: indexPath) as? HeadlineCell {

            let latest = self.latestNews[indexPath.row]
            cell.updateUI(latest: latest)

            return cell
        }

        return UICollectionViewCell()
    }

    func updateUI(latest: LatestNews) {
        self.latestNews.append(latest)
    }

集合视图类

func updateUI(latest: LatestNews){

        self.headlineImg.sd_setImage(with: URL(string: latest.thumbnail))
        headlineTextView.text = latest.headline
    }

所以问题基本上是如何将数组从MainViewController传输到自定义TableView类到自定义Collection View类。

1 个答案:

答案 0 :(得分:3)

UITableViewCell类:

class MyTableViewCell: UITableViewCell,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout
{
 var data : [SomeData]?

@IBOutlet var collectionView: UICollectionView! // initialize your collectionview from storyboard

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

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCellIdentifier", for: indexPath) as! MyCustomCollectionViewCell
    return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: 100, height: 100)
}
func initCollection()
{
    self.collectionView.dataSource = self
    self.collectionView.delegate = self
    self.collectionView.register(UINib(nibName:"MyCustomCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "myCellIdentifier")
}


func setData(data: [SomeData])
{
    self.data = data
    self.initCollection()
    self.collectionView.reloadData()
}
}

tableview的cellForRowAt:

let cell = tableView.dequeueReusableCell(withIdentifier: "MyTableViewCellIdentifier", for: indexPath) as! MyTableViewCell
cell.setData(data: yourArray)

你也可以从cellForRowAt初始化“data”数组,如:

cell.data = yourArray

这是我为我的项目做的一个示例,其中包含tableviewcell中的水平集合视图