[英]uicollectionview - data automatically load without calling reloaddata
I got two questions. 我有两个问题。
I am wondering why my collection view automatically load data without calling imageCollectionView.reloadData()
. 我想知道为什么我的收藏imageCollectionView.reloadData()
视图自动加载数据而无需调用imageCollectionView.reloadData()
。
Solved. 解决了。 See comment 查看评论
Why func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
is not called? 为什么func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
不被调用? I didn't see the print("collectionViewLayout called")
I am trying to modify the cell's size so the cell's height equals to the collection view height 我没有看到print("collectionViewLayout called")
我试图修改单元格的大小,因此单元格的高度等于集合视图的高度
Solved. 解决了。 See comment 查看评论
Here is the codes 这是代码
class ProductInternalDetailVC: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
var selectedProduct: Product?
@IBOutlet weak var imageCollectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
imageCollectionView.delegate = self
imageCollectionView.dataSource = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath as IndexPath) as! ProductInternalDetailCVC
if indexPath.row == 0 {
cell.productImage.image = selectedProduct!.image1
} else {
cell.productImage.image = selectedProduct!.image2
}
cell.productImage.frame = CGRect(x: 1, y: 1, width: cell.frame.size.width-2, height: cell.frame.size.height-2)
return cell
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
//return CGSize(width: collectionView.frame.size.height-1, height: collectionView.frame.size.height-1)
print("collectionViewLayout called")
return CGSize(width: 10, height: 10)
}
}
class ProductInternalDetailCVC: UICollectionViewCell {
@IBOutlet weak var productImage: UIImageView!
}
Thanks for the help. 谢谢您的帮助。
Question 2: The function signature has changed a bit. 问题2:功能签名有一些变化。 Change the function signature with this: 使用以下方法更改功能签名:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
...
}
And don't forget to implement the UICollectionViewDelegateFlowLayout
protocol. 并且不要忘记实现UICollectionViewDelegateFlowLayout
协议。
Question 1: 问题1:
When entering the ViewController your collectionView loads the datasource of the collectionView automatically if this is your question.for example, make changes on datasource without changing the view (means without calling viewDidLoad
method) you will not see the changes until you call imageCollectionView.reloadData()
. 进入ViewController时,如果这是您的问题,您的collectionView会自动加载collectionView的数据源。例如,在不更改视图的情况下对数据源进行更改(意味着不调用viewDidLoad
方法),直到调用imageCollectionView.reloadData()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.