我使用的是Xcode8和Swift3。我在设计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关键字前添加private 2)在fun关键字前面添加@nonobjc
我尝试了这两种解决方案,它抑制了警告,但没有一个是永远不会调用的。我尝试了设置断点,并尝试了许多方法。如果有人能把我从这个坑里拉出来的话。
发布于 2017-01-17 20:58:12
UICollectionViewDelegate没有sizeForItemAtIndexPath委托方法。
你可能要找的是
optional func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize它可以在UICollectionViewDelegateFlowLayout协议上找到。
尝试用以下代码替换您的代码:
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)
}https://stackoverflow.com/questions/41697674
复制相似问题