当选择另一个单元格按钮时,取消选择集合视图单元格按钮

时间:2019-07-18 06:06:29

标签: ios swift uicollectionview uicollectionviewcell

当我选择一个集合视图单元格按钮时,另一个单元格按钮将被取消选择。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FeedHot", for: indexPath) as! FeedHotRightNowCollectionViewCell
cell.playView.tag = indexPath.row
cell.playView.addTarget(self, action: #selector(hotRightNowPlay(_:)), for: .touchDown)
}

点击了单元格按钮

@objc func hottestOftheWeekPlay(_ sender: UIButton){

    if  GlobalVariable.isAudioPlaying {

    let img = UIImage(named: "icPanelPause.png")
    sender.setImage(img, for: .normal)
    }else{
    let img = UIImage(named: "icPanelPlay.png")
    sender.setImage(img, for: .normal)
    }
    GlobalVariable.isAudioPlaying.toggle()

}

注意:在didSelectItemAt / didDeselectItemAt单元格按钮内,单击不起作用。

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            if collectionView == self.feedRightNowCollectionView{

           let selectedCell: FeedHotRightNowCollectionViewCell = collectionView.cellForItem(at: indexPath) as! FeedHotRightNowCollectionViewCell
           selectedCell.playView.isSelected = true
 }

另一个注意事项:我有多个收藏夹视图。更好地了解屏幕截图

enter image description here

1 个答案:

答案 0 :(得分:0)

在didSelectItemAt函数中手动调用hottestOftheWeekPlay()。

像这样:

 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            if collectionView == self.feedRightNowCollectionView{

           let selectedCell: FeedHotRightNowCollectionViewCell = collectionView.cellForItem(at: indexPath) as! FeedHotRightNowCollectionViewCell
           selectedCell.playView.isSelected = true
           hottestOftheWeekPlay(selectedCell.playView)
 }