我有一本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()
}
}
}
答案 0 :(得分:0)
这是一个非常常见的错误。您正在更改视图(单元格)中的值,但不会相应地更新模型(数据源阵列)。您还需要在控制器中的项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
。并且 - 再次 - 请遵守变量名称以小写字母开头的命名约定。