【发布时间】:2018-12-27 10:12:41
【问题描述】:
当时我应该调用颜色集合视图单元格并在索引路径处选择项目项..
extension ProductDetailViewController: UICollectionViewDataSource
{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if( collectionView .isEqual(sizeCollection))
{
return (size[section].product_option_value!.count)
}
else
{
return (colorArray[section].product_option_value!.count)
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if (collectionView .isEqual(sizeCollection))
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "sizecell", for: indexPath) as! SizeCollectionViewCell
cell.sizeLabel.text = size[indexPath.section].product_option_value?[indexPath.item].name
return cell
}
else
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "colorcell", for: indexPath) as! ColorCollectionViewCell
DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async {
if let imageString = self.products[indexPath.section].images?[indexPath.item].thumb
{
if let imageUrl = URL(string:imageString)
{
if let imageData = try? Data(contentsOf: imageUrl)
{
DispatchQueue.main.async {
cell.colorTypesOfImages.image = UIImage(data: imageData)
cell.colorLabel.text = self.colorArray[indexPath.section].product_option_value?[indexPath.item].name
}
}}}}
return cell
}
}
}
extension ProductDetailViewController:UICollectionViewDelegate
{
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
if let imageString = self.products[indexPath.section].images?[indexPath.item].popup
{
if let imageUrl = URL(string:imageString)
{
if let imageData = try? Data(contentsOf: imageUrl)
{
DispatchQueue.main.async {
self.productOriginalImage.image = UIImage(data: imageData)
}
}}}
}
请帮我解决这个问题。
【问题讨论】:
-
从代码中我了解到您有 2 个不同的集合视图,您在 cellForItemAt 中进行了区分,但您忘记在 didSelectItemAt 中进行区分。
-
didSelectItemAt中如何区分,请解决这个问题?
-
当您比较 cellForItemAt 中的 collectionView 对象时,与 didSelectItemAt indexPath 中的比较方式相同。
标签: ios iphone swift3 uicollectionviewcell