繁体   English   中英

我在Swift 4中上下滚动时最喜欢的按钮更改

[英]Favourite button change when i scroll down and up in swift 4

我有一个bookCollectionview,因为我有一个喜欢的按钮。 问题是当我单击按钮成为收藏夹时,但是当我上下滚动时,它将取消选择。 我的api工作正常。 当刷新api成为选中状态。 只在第一次发行。

collectionview.swift

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

    let ad: Int? = Int(BookId[indexPath.item])

    cell.FavouritButton.tag = ad!

    if  BookFlag[indexPath.row] == "false" {
        cell.flag = 0
        cell.FavouritButton.setImage(UIImage(named:"icons8-sta-30.png"), for: UIControlState.normal)
        cell.FavouritButton.isSelected = true
    } else {
        cell.flag = 1
        cell.FavouritButton.setImage(UIImage(named:"icons8-star-filled-30.png"), for: UIControlState.normal)
        cell.FavouritButton.isSelected = false
    }
    if let url = NSURL(string: BookImageUrl[indexPath.row])
    {
        cell.CellImage.setUrl(url as URL)
    }

    cell.BookExtensionLabel.text = BookFileExtension[indexPath.row]
    cell.BookTitleTextView.text = BookTitle[indexPath.row]
    indicator.stopAnimating()
    BookCollectionView.allowsMultipleSelection = true

    return cell
}

CollectionViewCell.swift

@IBOutlet weak var FavouritButton: UIButton!

@IBAction func FavouritButton(_ sender: UIButton) {
    DispatchQueue.main.async {
        if self.flag == 0
        {
            let image1 = UIImage(named: "icons8-star-filled-30.png") as UIImage!
            self.FavouritButton.setImage(image1, for: .normal)
            self.flag = 1
            print(self.flag)
            self.webService(Action: "set_favourite_book", BookId: sender.tag)
            sender.isSelected = false

            // BookCollectionView.reloadData()
        }
        else
        {
            let image1 = UIImage(named: "icons8-sta-30.png") as UIImage!
            self.FavouritButton.setImage(image1, for: .normal)
            self.flag = 0
            self.webService(Action: "set_favourite_book", BookId: sender.tag)
            print(self.flag)
            sender.isSelected = true
            //  BookCollectionView.reloadData()
        }
    }
}

这是一个非常常见的错误。 您正在更改视图(单元格)中的值,但没有相应地更新模型(数据源数组)。 您还需要在控制器的BookFlag[indexPath.row]项目中更改该值。


在单元格中添加一个闭合变量,并在IBAction的分支中调用它

class AllBooksCollectionViewCell : UICollectionViewCell {

    @IBOutlet weak var FavouritButton: UIButton!

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

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

cellForItemAt添加回调闭包

...
   } else {
        cell.flag = 1
        cell.FavouritButton.setImage(UIImage(named:"icons8-star-filled-30.png"), for: UIControlState.normal)
        cell.FavouritButton.isSelected = false
    }

    cell.callback = { [unowned self] flag in
        self.BookFlag[indexPath.row] = flag
    }

    if let url = NSURL(string: BookImageUrl[indexPath.row])
    {
        cell.CellImage.setUrl(url as URL)
    }
...

注意:为了确保数据一致性,您应该对标志使用Bool而不是控制器中的String和单元格中的Int 并且-再次- 请遵守命名约定,即变量名以小写字母开头。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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