簡體   English   中英

快速收集視圖按鈕影響其他單元格

[英]Swift collection view button affecting other cells

我有一個自定義的收藏視圖,頂部有一個“收藏夾”按鈕,當按下該按鈕時,應該在中心顯示一個像心臟動畫一樣的Instagram。 到目前為止,我所做的事情當然會導致心臟動畫出現在其他隨機單元格中,這歸因於此代碼的重復使用標識符。

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "shopCell", for: indexPath) as! ShopCell

但是我應該如何解決這個問題? 我已經閱讀了多篇關於它的文章並實現了解決方案,但是沒有一個對我有用。 例如,將索引路徑設置為單元格,然后在按鈕上使用委托單擊

cell.indexPath = indexPath

在我的商店單元中

@IBAction func favHeartBtn(_ sender: Any) {
    delegate.favoriteButton(sender: self)
}

在我的shopView中我有這個

func favoriteButton(sender: ShopCollectionViewCell) {  
    sender.centerHeart.isHidden = false
    sender.centerHeart.zoomIn()
}

靜止動畫從其他單元格開始。 即使我檢查indexPath

您需要在自定義UICollectionViewCell處理中心心臟的顯示/隱藏。

class CustomCollectionCell : UICollectionViewCell
{
    @IBOutlet weak var centerHeart: UILabel!

    override func awakeFromNib()
    {
        super.awakeFromNib()
        self.centerHeart.alpha = 0.0
    }

    @IBAction func onTapHeartButton(_ sender: UIButton)
    {
        UIView.animate(withDuration: 0.5, animations: { 
            self.centerHeart.alpha = 1.0
        }) { (completed) in
            if completed
            {
                UIView.animate(withDuration: 0.5, animations: { 
                    self.centerHeart.alpha = 0.0
                })
            }
        }
    }
}

您可以根據需要添加所需的任何動畫。

示例項目: https //github.com/pgpt10/CollectionViewSample

在您的類中創建帶有索引的數組:

var favoriteList = [Int]()

在單元設置代碼中,將按鈕的標記設置為indexPath.row

[skip cell setup code]
cell.centerHeart.tag = indexPath.row 

您的收藏按鈕將如下所示:

func favoriteButton(sender: ShopCollectionViewCell) {
    ///update model
    favoriteList.append(sender.tag)
    ///refresh UI
    self.tableView.reloadData()
}

在此委托中,檢查IndexPath。 如果在列表中找到此indexPath,則顯示按鈕。

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if self.shouldAnimateCell(indexPath) {
        cell.showHearthAnimation(indexPath)
    } else {
        cell.hideHearthAnimation()
    }   
}


func showHearthAnimation(indexPath: IndexPath) {
    let cell = self.tableView.cellForRow(indexPath)

    cell.displayCenterHearth()
}

func hideHearthAnimation(indexPath: IndexPath) {
    let cell = self.tableView.cellForRow(indexPath)

    cell.hideCenterHearth()
}

func shouldAnimateCell(indexPath: IndexPath) {
    return self.favoriteList.contains(indexPath.row)
}

對於單元,您可以實現:

func displayCenterHearth() {
    self.centerHeart.isHidden = false
    self.centerHeart.zoomIn()
}
func hideCenterHearth() {
    self.centerHeart.isHidden = true
}

該解決方案應該為您工作。 抱歉,可能會出現錯誤,我已使用TextEditor編寫此代碼。


原始解決方案的主要問題是什么? 我們正在使用MVC。 這意味着我們擁有模型和視圖。 當您按下按鈕時,您將更新UI(視圖),但對數據模型不做任何事情。 正確的方案是:更新模型並重新加載或刷新視圖。

我相信您已經有了數據模型,它看起來像:

var model: [Record]

struct Record {
    var title: String
    var description: String
    var isFavorite: Bool
}

為了使其與您的數據模型一起使用,請在委托方法中將isFavorite屬性設置為true,然后重寫shouldAnimateCell。

func favoriteButton(sender: ShopCollectionViewCell) {
    ///update model
    let indexPath = self.tableView.indexPath(for: sender)

    self.model[indexPath.row].isFavorite = true

    ///refresh UI
    self.tableView.reloadData()
}

func shouldAnimateCell(indexPath: IndexPath) {
    return self.model[indexPath.row].isFavorite
}

盡管PGDev的答案很完美,但我接受它是正確的答案。 導致PGDev代碼在我的自定義類中不起作用的問題是我在使用按鈕的委派。

cell.delegate = self

我刪除了這一行,並添加了PGDev在單元IBOutlet中為按鈕提供的動畫的代碼,從而達到了目的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM