gpt4 book ai didi

ios - UICollectionViewDelegate 中的 Swift 3 警告

转载 作者:行者123 更新时间:2023-11-29 00:31:21 25 4
gpt4 key购买 nike

我正在使用 Xcode 8 和 Swift 3。我正在设计 UICollectionView 并想要动态设置高度和宽度,因此需要一些答案。在我得到这个解决方案的路上(正确与否 - 我不知道):

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

let Labell : UILabel = UILabel()
Labell.text = self.items[indexPath.item]
let labelTextWidth = Labell.intrinsicContentSize.width
return CGSize(width: labelTextWidth + 20, height: 35)

}

但是这个答案是在 swift 2.0 中给出的,给出这个答案的人声称它已经解决了。我将它粘贴到 Xcode 中并更改了 Xcode 建议我的一些内容。

现在我收到以下警告:

instance method 'collectionView:layout:sizeForItemAtIndexPath:)' nearly matches optional requirement 'collection(_:willDisplaySupplementaryView:forElement:at:)' of protocol 'UICollectionViewDelegate'

Xcode 建议我 2 个解决方案1) 在 func 关键字前添加 private2)在fun关键字前添加@nonobjc

我尝试了两种解决方案,它抑制了警告,但从未调用过上述任何一个。我尝试设置断点并尝试了很多方法。如果有人能把我从这个坑里拉出来就好了。

最佳答案

UICollectionViewDelegate 没有sizeForItemAtIndexPath 委托(delegate)方法。

您可能正在寻找的是

optional func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize

可以在 UICollectionViewDelegateFlowLayout 协议(protocol)中找到。

尝试用这个替换你的代码:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

let Labell : UILabel = UILabel()
Labell.text = self.items[indexPath.item]
let labelTextWidth = Labell.intrinsicContentSize.width
return CGSize(width: labelTextWidth + 20, height: 35)

}

关于ios - UICollectionViewDelegate 中的 Swift 3 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41697674/

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