gpt4 book ai didi

ios - 使用 didHighlightItemAtIndexPath 实例化自定义 UICollectionViewCell

转载 作者:行者123 更新时间:2023-11-30 13:37:57 25 4
gpt4 key购买 nike

我将 UICollectionView 与自定义 UICollectionViewCell 结合使用。为了在 cellForItemAtIndexPathdidSelectItemAtIndexPath 中实例化我的单元格,我使用了这一行:

let music_Cell:MusicCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("music_Cell", forIndexPath: indexPath) as! MusicCollectionViewCell

通过这种方式,我可以调用我的自定义音乐单元,然后我可以根据该歌曲的状态(例如它是否已经播放等)来操作它。

例如,我可以像这样操作单元格:

music_Cell.song_Title_Label.text = song_Title! 

在函数结束时我只返回music_Cell。一切正常......直到

在我看来,我无法使用函数 didHighlightItemAtIndexPathunHighlight 函数实例化自定义单元格,因为这些函数不允许您使用返回自定义 UICollectioViewCell。我希望能够在人物突出显示时进行自定义 UI 更改。到目前为止,我唯一能做的就是实例化一个常规单元,而不是我的自定义单元:

let cell: UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)!
cell.contentView.backgroundColor = opus_Page_Primary_Color

但我希望能够控制其他 UI 元素,而不仅仅是背景颜色。

我该怎么做?我到处都找过了?

最佳答案

您可以在自定义 UICollectionViewCell 子类中处理选择和突出显示的外观。

class MyCell: UICollectionViewCell {
override var selected: Bool {
didSet {
print("selected") // Do your custom selection state here.
}
}

override var highlighted: Bool {
didSet {
print("highlighted") // Do you custom highlighting here.
}
}
}

您还可以利用selectedBackgroundView 属性。将其设置为自定义 View 将在选择单元格时在单元格背景 View 上方为该 View 设置动画:

public var selectedBackgroundView: UIView?

注意:唯一一次应该使可重用单元出队的是在 cellForItemAtIndexPath 中(这就是为什么其他委托(delegate)方法不返回 UICollectionViewCell)。

关于ios - 使用 didHighlightItemAtIndexPath 实例化自定义 UICollectionViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35880412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com