Pak*_*ung 1 reloaddata ios uicollectionview uicollectionviewcell swift
我有两个问题。
我想知道为什么我的集合视图无需调用即可自动加载数据imageCollectionView.reloadData()。
解决了。查看评论
为什么func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize不叫?我没有看到print("collectionViewLayout called")我正在尝试修改单元格的大小,因此单元格的高度等于集合视图的高度
解决了。查看评论
这是代码
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!
}
Run Code Online (Sandbox Code Playgroud)
谢谢您的帮助。
问题2:功能签名有一些变化。使用以下方法更改功能签名:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
...
}
Run Code Online (Sandbox Code Playgroud)
并且不要忘记实现该UICollectionViewDelegateFlowLayout协议。
问题1:
进入ViewController时,如果这是您的问题,您的collectionView会自动加载collectionView的数据源。例如,在不更改视图的情况下对数据源进行更改(意味着没有调用viewDidLoad方法),直到您调用才会看到更改imageCollectionView.reloadData()。
| 归档时间: |
|
| 查看次数: |
433 次 |
| 最近记录: |