0

costumViewCell 内の 1 つのオブジェクトだけを更新しようとしていますが、試してみましcollectionView.reloadItems(at: [IndexPath])たが、このメソッドはセル全体を更新するため、アニメーションが非常に不安定になります。

これが私のcollectionViewセルのサンプルコードです。

class MyCollectionViewCell: UICollectionViewCell {


    @IBOutlet weak var buttonA: UIButton!
    @IBOutlet weak var buttonB: UIButton!


    var myButtonTitle: String? {
        didSet{
            if let title = myButtonTitle {
                self.buttonA.setTitle(title, for: .normal)
            }
        }
    }

    var buttonActionCallBack: (()->()?)

    override func awakeFromNib() {
        super.awakeFromNib()
        self.animation()

        buttonA.addTarget(self, action: #selector(buttonACallBack), for: .touchUpInside)
    }


    @objc fileprivate func buttonACallBack() {
        self.buttonActionCallBack?()
    }


    fileprivate func animation() {
        UIView.animate(withDuration: 1.0) {
            self.buttonA.transform = CGAffineTransform(translationX: 20, y: 20)
            self.buttonB.transform = CGAffineTransform(translationX: 20, y: 20)
        }
    }
}

これが私の DataSource メソッドです。

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

    let item = mainList[indexPath.row]

    collectionView.reloadItems(at: <#T##[IndexPath]#>)
    cell.buttonActionCallBack = {
        () in
        //Do Stuff and Update Just ButtonA Title
    }
    return cell
}

乾杯。

4

1 に答える 1