我尝试实现有关产品的详细 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/