首页 > 解决方案 > CollectionView 委托方法未从 TableViewCell 调用

问题描述

截屏

没有从 tableViewCell 调用委托。这是UICollectionViewDelegateUICollectionViewDataSource代码

extension HomeVC:UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ProductCell", for: indexPath)
        return cell

    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("ok")

    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let lay = collectionViewLayout as! UICollectionViewFlowLayout
        lay.sectionInset = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
        lay.minimumInteritemSpacing = 0
        lay.minimumLineSpacing = 0
        collectionView.collectionViewLayout = lay
        let size = (collectionView.frame.size.width-10) / 2
        return CGSize(width: size, height: size)
    }

}

标签: iosxcodeuitableviewuicollectionviewswift4

解决方案


每次在 Tableview 委托 CellForRowAtIndexPath 中,您都需要为 collectionView 设置数据源和委托。


推荐阅读