繁体   English   中英

uicollectionview-数据自动加载而无需调用reloaddata

[英]uicollectionview - data automatically load without calling reloaddata

我有两个问题。

  1. 我想知道为什么我的收藏imageCollectionView.reloadData()视图自动加载数据而无需调用imageCollectionView.reloadData()

    解决了。 查看评论

  2. 为什么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!
}

谢谢您的帮助。

问题2:功能签名有一些变化。 使用以下方法更改功能签名:

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

并且不要忘记实现UICollectionViewDelegateFlowLayout协议。

问题1:

进入ViewController时,如果这是您的问题,您的collectionView会自动加载collectionView的数据源。例如,在不更改视图的情况下对数据源进行更改(意味着不调用viewDidLoad方法),直到调用imageCollectionView.reloadData()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM