ios - 使用 Xib 在 Tableview header 中快速重新加载 CollectionView

标签 ios swift xcode uitableview xib

我尝试实现有关产品的详细 View 。我使用 TableView 来显示产品详细信息,并决定在 TableView 标题中为产品图像创建一个 Collection View 。我创建了一个 Xib 文件,其中包含一个 Collection View 和 2 个标签。我将这个 Xib 实现到了 tableview header 。我可以重新加载 TableView 行,但我没有重新加载标题中的 Collection View 。

如何重新加载此 Collection View ?

我在提取 json 后使用此代码,它允许我在表格 View 单元格中显示参数。

let sectionIndex = IndexSet(integer: 0)
self.tableView.reloadSections(sectionIndex, with: .none)

在viewDidLoad()中

if element.result.productImages?.count ?? 0 > 0 {
    for x in element.result.productImages! {
        print(x)
        self.productImages.append(x.imageUrl)
        self.productDetailHeaderView.productImagesCollectionView.reloadData()
    }
}

在 CellForRowAt 中

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ProductImagesCollectionViewCell", for: indexPath) as! ProductImagesCollectionViewCell
    cell.productImage.sd_setImage(with: URL(string: "\(productImages[indexPath.row])"), placeholderImage: UIImage(named: "placeholder"))
    return cell
}

--

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let v : ProductDetailHeaderView = UIView.fromNib()
    productDetailHeaderView = v
    productDetailHeaderView.translatesAutoresizingMaskIntoConstraints = false

    productDetailHeaderView. productImagesCollectionView.register(UINib(nibName: "ProductImagesCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "ProductImagesCollectionViewCell")

    productDetailHeaderView.backgroundColor = .yellow


    return productDetailHeaderView
}

提前致谢。

最佳答案

您可能需要设置委托(delegate)和数据源

productDetailHeaderView.productImagesCollectionView.register(UINib(nibName: "ProductImagesCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "ProductImagesCollectionViewCell")  
productDetailHeaderView.productImagesCollectionView.delegate = self 
productDetailHeaderView.productImagesCollectionView.dataSource = self

关于ios - 使用 Xib 在 Tableview header 中快速重新加载 CollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53977435/

相关文章:

iphone - 访问使用 iTunes 传输的 "private Documents"文件夹中的文件

ios - 如何在 Mobile Safari 中弹出 "Add my app to the springboard"通知?

ios - 如何从当前 View ios Swift 中删除 UIVisualEffect?

ios - 如何将值从一个 View Controller 传递到另一个 swift?

ios - 将新对象添加到 Realm 中的现有列表

c - ld : symbol(s) not found for architecture x86_64 error message when compiling

ios - 如何在 iOS 的导航中制作带有 Logo 和文本的自定义导航栏?

ios - VoiceOver 正在读取我的 UIAlertController 后面的 View Controller 内容

ios - Xcode6:运行模拟器的两个实例

xcode - NSDateFormater +0000