不同版本的Swift中找不到sizeForItemAtIndexPath。

18

我正在使用UICollectionView,但找不到以下方法:

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

请建议我该如何替换它。

2个回答

82

对于我来说,sizeForItemAt 没有被调用,因为在Swift 3中,您需要明确设置您的类实现 UICollectionViewDelegateFlowLayout 协议。


3
你帮我省了很多时间,因为我对CollectionView不太熟悉,但是我必须在我的一个应用程序中使用它。非常感谢你的重要指示,点赞! - g212gs
1
特别有用,当您刚刚将现有项目迁移到Swift 3并想知道为什么您的集合视图突然出现问题时。 - retendo

54

这是其新的语法:

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

}  

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接