收藏夹中的收藏夹按钮-Swift

时间:2020-04-30 22:13:11

标签: swift

我使用CollectionView中的“收藏夹”按钮。我已根据此收藏夹按钮的错误或真实状态定义了不同的图像。我想在应用程序中保存这些正确和错误的状态。为此,我首先定义值为true或false的不同图像。一旦确定真假,我就将钥匙串中的数据保存到selectedButtonNew键中,并通过获取布尔值将selectedButtonNew键放入newDAVID的ID中。这样,我为每个id分配了一个不同的true false值。但这并不像现在这样。正确,错误的值记录不正确。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    cell.callback = { [unowned self] flag in
        self.yeniDavID[indexPath.row] = flag
        self.keychain.set(flag, forKey: "selectedButtonNew")
    }

    cell.FavouritButton.addTarget(self, action: #selector(favDavetiye(_:)), for: .touchUpInside)
    cell.FavouritButton.accessibilityElements = [indexPath]
}

@objc func favDavetiye(_ sender:UITapGestureRecognizer) {

    let index = sender.accessibilityElements?.first as! IndexPath

    let davID = yeniDavID[index.row]
    let buttondeger = keychain.getBool("selectedButtonNew")
    self.keychain.set(buttondeger!, forKey: "\(davID)")
}

手机

class sonsuzCell: UICollectionViewCell {

    @IBOutlet weak var FavouritButton: UIButton!

    var callback : ((String) -> ())?
    var flag = Bool()

    @IBAction func FavouritButton(_ sender: UIButton) {
        DispatchQueue.main.async {
            if self.flag == true {
                let image1 = UIImage(named: "star-2favsiz") as UIImage?
                self.FavouritButton.setImage(image1, for: .normal)
                self.flag = false
                print(self.flag)
                //  self.webService(Action: "set_favourite_book", BookId: sender.tag)
                sender.isSelected = false
                self.callback?("true")
            } else {
                let image1 = UIImage(named: "star") as UIImage?
                self.FavouritButton.setImage(image1, for: .normal)
                self.flag = true
                sender.isSelected = true
                self.callback?("false")
            }
        }
    }
}

0 个答案:

没有答案