gpt4 book ai didi

swift - 如何获取collectionViewLayout中的CollectionViewCell?

转载 作者:行者123 更新时间:2023-11-28 15:04:47 25 4
gpt4 key购买 nike

下面是我的代码。

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

cell.lblUsername.text = self.arrSelectedUsersToChat.object(at: indexPath.row) as? String

cell.layoutSubviews()
return cell
}

我想根据内容更改单元格宽度,为此我使用了以下方法。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 150.0, height: 44.0)
}

目前我通过了静态宽度。现在我想要的是我想在 collectionViewLayout 方法中访问单元格,这样我就可以获得单元格标签,并根据这个标签计算它的宽度。我怎样才能做到这一点?

最佳答案

可以通过调用collectionview的cellForItem获取cell。在 sizeForItemAt 方法中编写代码

if let collectionViewCell = collectionView.cellForItem(at: indexPath) as? YourCollectionViewCell {
}

关于swift - 如何获取collectionViewLayout中的CollectionViewCell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48640339/

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