简体   繁体   中英

How do I remove button selected state when clicked?

I have an app in which I have category collection view and in each cell there is a button whose image I need to change when selected. When the button is clicked the selected id is appended and when I press the tick button I go to another screen and it shows me all the data related to that category. When I unselect the category and press the button it still shows me the data of that category which I don't want. Can anyone help me with this problem?

screenshot of my app:

我的应用程序的屏幕截图

My code that I have tried so far is:

cellforrow method:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if selectedBtnIndex == 1{
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CategoryCell1", for: indexPath) as! CategoryCell1

        let dict = categoryData[indexPath.row]

        if let catName = dict.name, catName.count != 0 {
            cell.categoryName.text = catName
        }

        if let catOffersCount = dict.count {

            if catOffersCount == 1 {
              cell.catOfferCount.text = "\(catOffersCount)"+" "+"Offer"
            }else {
                cell.catOfferCount.text = "\(catOffersCount)"+" "+"Offers"
            }
        }

        cell.categoryImage.image = arrCategoryImages[indexPath.row]

        cell.btn_click.tag = indexPath.row
        cell.btn_click.addTarget(self, action: #selector(self.click_Category), for: .touchUpInside)

        if selectedIds.contains(categoryData[indexPath.row].ID!) {
            cell.btn_click.setImage(#imageLiteral(resourceName: "image_checked"), for: .normal)
            cell.btn_click.isSelected = true
        }else {
            cell.btn_click.setImage(#imageLiteral(resourceName: "image_unchecked"), for: .normal)
            cell.btn_click.isSelected = false
        }

        return cell

Button add target function:

@objc func click_Category(sender: UIButton!) {
    if sender.isSelected == true {
        selectedIds.append(categoryData[sender.tag].ID!)
        sender.setImage(#imageLiteral(resourceName: "image_checked"), for: .normal)
        sender.isSelected = false
    } else {
        selectedIds = selectedIds.filter{ $0 != sender.tag }
        sender.setImage(#imageLiteral(resourceName: "image_unchecked"), for: .normal)
        sender.isSelected = true
    }
}

There is mainly two bugs with the posted code. These logical bugs are in the click_Category function. Bugs are:

1.Toggling the UIButton image using sender.isSelected is not properly handled. 2. First block of "if" statement appends ID's in selectedIds array but when again tap on the same UIButton to uncheck it, selectedIds array does not remove the unchecked UIButtons ID.

Here is the code snippet for modified click_Category function:

 @objc func click_Category(sender: UIButton!) {

    sender.isSelected = !sender.isSelected
    if sender.isSelected {
        selectedIds.append(categoryData[sender.tag].ID!)
        sender.setImage(UIImage(named: "image_checked"), for: .normal)
    }else {
        selectedIds = selectedIds.filter {$0 != categoryData[sender.tag].ID!}//removing the unchecked ID
        sender.setImage(UIImage(named: "image_unchecked"), for: .normal)
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM