ios - 在 UITableViewCell 中重新加载 Collectionview Cell

标签 ios objective-c swift uitableview

我有一个 UITableViewCell,其中添加了 UICollectionViewCell (用于水平滚动)。

如果没有互联网,我将显示空白 View 。我想做的是当用户连接到互联网时我应该能够显示数据。 现在它是随机显示的,3 个单元中有 2 个根本不可见。 我正在使用通知用户是否进入前台并重新加载表数据。这是我的表格单元格和 Collection View 单元格的结构。

代码:

ViewController.swift:

class ViewController: UIViewController {
    var categories = ["Action", "Drama", "Science Fiction", "Kids", "Horror"]
}

extension ViewController : UITableViewDelegate { }

extension ViewController : UITableViewDataSource {

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return categories[section]
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return categories.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CategoryRow
        return cell
    }

}

CategoryRow.swift

class CategoryRow : UITableViewCell {
    @IBOutlet weak var collectionView: UICollectionView!
}

extension CategoryRow : UICollectionViewDataSource {

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "videoCell", for: indexPath) as! VideoCell
        return cell
    }

}

extension CategoryRow : UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let itemsPerRow:CGFloat = 4
        let hardCodedPadding:CGFloat = 5
        let itemWidth = (collectionView.bounds.width / itemsPerRow) - hardCodedPadding
        let itemHeight = collectionView.bounds.height - (2 * hardCodedPadding)
        return CGSize(width: itemWidth, height: itemHeight)
    }

}

VideoCell.swift

class VideoCell : UICollectionViewCell {

    @IBOutlet weak var imageView: UIImageView!
}

最佳答案

UITableViewDataSource方法中重新加载UICollectionView

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

        let cell = tableView.dequeueReusableCell(withIdentifier: "DashboardTableViewCell", for: indexPath) as! DashboardTableViewCell

       cell.collectionViewNeel.reloadData()

        return cell

    }

关于ios - 在 UITableViewCell 中重新加载 Collectionview Cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45484079/

相关文章:

ios - SKScene 中的 AdMob 奖励视频

ios - Skobbler 将自定义 POI 添加到热图中

ios - UIPageViewControllerDataSource 第一次运行时调用 viewControllerAfterViewController 两次

ios - 从 Parse localDataStore 查询对象

ios - CABasicAnimation + UIBezierPath

iOS 应用因 EXC_BAD_ACCESS (SIGSEGV) 而被拒绝,这是什么意思?

ios - 如何在 Xcode 中使用带有 api key 的 api?

objective-c - 委托(delegate)单例对象

ios - 无法从非父 VC 更新嵌入式子 VC

ios - 模态转换后释放 SKScene