[英]Modifying the size of the cell of UICollectionView
I am trying to modify the size of the cells that the UICollectionview contains. 我正在尝试修改UICollectionview包含的单元格的大小。 I believe that the sizeForItemAtIndexPath
should do the trick. 我相信sizeForItemAtIndexPath
应该可以解决问题。 Yet nothing is happening. 然而什么也没有发生。
I have looked into similar questions, they have advised doing the same thing. 我研究过类似的问题,他们建议做同样的事情。 I am sort of suspicious of my inheritance. 我有点怀疑自己的继承权。
The problem is that the cells remain in the same size regardless of the value they were fed in. 问题在于,无论输入的值是多少,单元格都保持相同的大小。
class HikeViewController: UIViewController {
var test: Int?
@IBOutlet weak var options: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.prefersLargeTitles = false
print(test!)
options.dataSource = self
options.delegate = self
}
}
extension HikeViewController: UICollectionViewDelegate, UICollectionViewDataSource {
//collectionCell
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath as IndexPath)
cell.backgroundColor = UIColor.green
return cell
}
func collectionView(collectionView : UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeForItemAtIndexPath indexPath:NSIndexPath) -> CGSize
{
return CGSize(width: 400, height: 500)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print(indexPath.row)
}
}
And when I conform to UICollectionViewDelegateFlowLayout
the app termainates where I set the followings: 当我遵循UICollectionViewDelegateFlowLayout
该应用程序将终止于设置以下内容的位置:
options.dataSource = self
options.delegate = self
And the error is: 错误是:
Could not cast value of type 'HikingClub.HikeViewController' (0x108e1f948) to 'UICollectionViewDataSource' (0x10e723ff0).
It's worth adding that I have made sure of the connection between HikeViewController
and the storyboard. 值得补充的是,我已经确定了HikeViewController
和情节HikeViewController
之间的连接。
Make your extension a subclass of UICollectionViewDelegateFlowLayout
使您的扩展成为UICollectionViewDelegateFlowLayout
的子类
Use the following function 使用以下功能
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { // code }
extension HikeViewController: UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
}
应该是这样的
You need to specify that you implement the protocol UICollectionViewDelegateFlowLayout in your class declaration. 您需要指定在类声明中实现协议UICollectionViewDelegateFlowLayout。
extension HikeViewController: UICollectionViewDelegateFlowLayout {}
also you have are not calling the delegate method, just defining a new one. 您也没有调用委托方法,只是定义了一个新方法。
check the first parameter (collectionView -> _ collectionView) 检查第一个参数(collectionView-> _ collectionView)
func collectionView(_ collectionView : UICollectionView,layout collectionViewLayout:UICollec func collectionView(_ collectionView:UICollectionView,layout collectionViewLayout:UICollec
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.