【问题标题】:Swift Reload CollectionView in Tableview Header with Xib使用 Xib 在 Tableview Header 中快速重新加载 CollectionView
【发布时间】:2018-12-30 12:05:43
【问题描述】:

我尝试实现有关产品的详细视图。我将 tableview 用于产品详细信息,并决定在 tableview 标题中为产品图像创建一个 collectionview。我创建了一个包含集合视图和 2 个标签的 Xib 文件。我将这个 Xib 实现为 tableview 标题。我可以重新加载表格视图行,但我没有在标题中重新加载集合视图。

如何重新加载此收藏视图?

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

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
}

提前致谢。

【问题讨论】:

    标签: ios swift xcode uitableview xib


    【解决方案1】:

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

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

    【讨论】:

    • 对不起,我在问题中写错了。我编辑了问题
    • 谢谢你。我忘记了委托和数据源。这是一个大错误。 :(
    猜你喜欢
    • 2020-10-18
    • 1970-01-01
    • 2020-06-03
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    相关资源
    最近更新 更多