代码之家  ›  专栏  ›  技术社区  ›  NullHypothesis

当点击不工作时更改uiCollectionViewCell内的视图

  •  0
  • NullHypothesis  · 技术社区  · 6 年前

    我在一个uiCollectionViewCell中有一个用户化身列表。当用户点击其中一项时,我想将所选项目添加到集合中,并将其突出显示以指示它已被点击。

    不幸的是,用户界面似乎没有更新。有什么想法吗?

    最初我加载图像并将其设置为圆形。如果我愿意的话,我甚至可以设置边框颜色,但现在我将其设置为清除。所有这些都在加载单元时工作:

     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! UICollectionViewCell
            // Configure the cell
            let member: UserProfile = groupMembers[indexPath.item]
            let imgAvatar = cell.viewWithTag(2) as! UIImageView
    
            imgAvatar.layer.cornerRadius = contactAvatar.frame.size.width / 2
            imgAvatar.clipsToBounds = true
            imgAvatar.contentMode = UIViewContentMode.scaleAspectFill
            imgAvatar.layer.borderWidth = 2.0
            imgAvatar.layer.borderColor  = UIColor.clear.cgColor
            imgAvatar.layer.cornerRadius = 30.0
    
            let downloadURL = NSURL(string: member.avatarUrl)!
            imgAvatar.af_setImage(withURL: downloadURL as URL)
    
            return cell 
        }
    

    下面是当您点击集合中任何给定的uiImageView时执行的代码,但它似乎没有更新图像:

     ///Fired when tapped on an image of a person
        func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
             let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! UICollectionViewCell
            let tappedUser: UserProfile = groupMembers[indexPath.item]
            let imgAvatar = cell.viewWithTag(2) as! UIImageView
    
               ..add item to collection, etc.. 
    
               //Update imageview to indicate it's been tapped.
               DispatchQueue.main.async {
                contactAvatar.layer.cornerRadius = contactAvatar.frame.size.width / 2
                imgAvatar.clipsToBounds = true
                imgAvatar.contentMode = UIViewContentMode.scaleAspectFill
                imgAvatar.layer.borderWidth = 2.0
                imgAvatar.layer.cornerRadius = 30.0
                imgAvatar.layer.borderColor = UIcolor.blue.cgColor
                }
    
            }
        } 
    

    运行此代码时,它会命中断点以指示我已点击该项,但它不会更新UI。我确信有一个线程/UI问题,在这个问题中,集合视图不会“重新绘制”我对图像所做的更改。也许我不能改变一个收藏品内部视图的外观?

    提前感谢您提供任何见解。

    2 回复  |  直到 6 年前
        1
  •  1
  •   agibson007    6 年前

    您的didselect不正确。从集合视图中获取单元格。 cellForItem

     ///Fired when tapped on an image of a person
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
         let cell = collectionView.cellForItem(at:indexPath) as! UICollectionViewCell
        let tappedUser: UserProfile = groupMembers[indexPath.item]
        let imgAvatar = cell.viewWithTag(2) as! UIImageView
    
           ..add item to collection, etc.. 
    
           //Update imageview to indicate it's been tapped.
           DispatchQueue.main.async {
            contactAvatar.layer.cornerRadius = contactAvatar.frame.size.width / 2
            contactAvatar.clipsToBounds = true
            contactAvatar.contentMode = UIViewContentMode.scaleAspectFill
            contactAvatar.layer.borderWidth = 2.0
            contactAvatar.layer.cornerRadius = 30.0
            contactAvatar.layer.borderColor = UIcolor.blue.cgColor
            }
    
        }
    } 
    

    使用手机,如果不起作用请告诉我。

        2
  •  0
  •   Vinod Pandey    6 年前

    您为获取单元实例而编写的代码不正确,请使用下面的代码行,其余的代码行都似乎正确,希望通过更改该行可以正常工作。

        let cell = collectionView.cellForItem(at:indexPath) as! UICollectionViewCell