代码之家  ›  专栏  ›  技术社区  ›  Ali Ihsan URAL

使用Xib在Tableview标题中快速重新加载CollectionView

  •  2
  • Ali Ihsan URAL  · 技术社区  · 7 年前

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

    如何重新加载此collectionview?

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

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

    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
    }
    

    提前谢谢。

    1 回复  |  直到 7 年前
        1
  •  2
  •   Shehata Gamal    7 年前

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

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