我的xcode版本是13,不支持collectionView下面的方法

kmb7vmvb  于 2023-02-09  发布在  其他
关注(0)|答案(1)|浏览(117)
import UIKit

class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {
    var shoppingapp:[String] = ["cher","sale Small","shoes"]

    override func viewDidLoad() {
        super.viewDidLoad()
        
    }

   //this function is not working in my xcode 13 not calle this function 
# func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
# return shoppingapp.count
#     }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! MyCollectionCell
        
        cell.shoppinfappimage.image = UIImage(named: shoppingapp[indexPath.row] + ".jpeg")
        cell.shoppinfappimage.layer.cornerRadius=50.0
        return cell
    }

}

我不知道还有哪种方法是我正在学习和实践的iOS应用程序开发****

8gsdolmq

8gsdolmq1#

首先,请删除代码中的#,将其划分为ViewController扩展。

extension ViewController: UICollectionViewDelegate,UICollectionViewDataSource {
     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
         return shoppingapp.count
    }
        
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! MyCollectionCell
        
        cell.shoppinfappimage.image = UIImage(named: shoppingapp[indexPath.row] + ".jpeg")
        cell.shoppinfappimage.layer.cornerRadius=50.0
        return cell
    }
}

但是当我看到你的代码时,我认为你没有声明delegate和DataSource。

//in viewDidLoad
collectionView.delegate = self
collectionView.dataSource = self

在viewDidLoad中编写上面的代码。所以我也不能做太多。
如果不管用,就这样试试。

@available(iOS 13.0, *)
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
     return shoppingapp.count
}

我希望写作对我有帮助。

相关问题