首页 文章

如何使自定义collectionview单元格可见?

提问于
浏览
0

我似乎无法使我的自定义collectionView Cell可见 . 我知道CollectionView显示,因为我更改了属性检查器上的背景颜色,但自定义单元格没有显示 . 我在属性检查器中设置了约束,并在图像下面有一个水平堆栈视图 .

我完全按照了Udemy课程的指示,但似乎没有用 . 这就是我配置单元格的方式:

Image

代码如下:

import UIKit

class ListVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    @IBOutlet weak var productsCollection: UICollectionView!

    private(set) public var products = [Product]()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        productsCollection.dataSource = self
        productsCollection.delegate = self
    }

    func initProducts(Product: Product) {
        products = DataService.instance.getGroceryOptions()
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return products.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ProductCell", for: indexPath) as? ProductCell {
            let product = products[indexPath.row]
            cell.updateViews(product: product)
            return cell
        }
        return ProductCell()

    }
}

这是ProductCell实现:

class ProductCell: UICollectionViewCell {
    @IBOutlet weak var itemImage: UIImageView!
    @IBOutlet weak var productName: UILabel!
    @IBOutlet weak var productPrice: UILabel!
    @IBOutlet weak var calCount: UILabel!

    func updateViews(product: Product){
        itemImage.image = UIImage(named: product.imageName)
        productName.text = product.title
        calCount.text = product.cal
        productPrice.text = product.price
    }

}

这是DataService实现:

class DataService {
    static let instance = DataService()

    private let item = [
        Product(title: "Eggs", price: "4.94", imageName: "eggs.jpeg", cal: "340"),
    ]

    private let Groceries = [Product]()

    func getGroceryOptions() -> [Product] {
        return Products
    }
}

这是产品实现:

struct Product {
    private(set) public var title: String
    private(set) public var price: String
    private(set) public var imageName: String
    private(set) public var cal: String

    init(title: String, price: String, imageName: String, cal: String) {
        self.title = title
        self.price = price
        self.imageName = imageName
        self.cal = cal
    }
}

3 回答

  • 0

    1)您的 initProducts 方法不会在代码中的任何位置调用 .

    2)更新数据源后,必须调用 collectionView.reloadData() . 这意味着,您必须在更新 products 属性后调用此方法 .

    3)另外,正如其他人在他们的答案中提到的那样,你的 getGroceryOptions() 方法实际上应该返回 items 而不是 Products (那是什么,顺便说一下?) .

    4)请按照swift样式指南编写代码 . 您可以更容易地将类/结构与变量区分开来 . 您可以了解更多相关信息here以及here .

  • 0

    您的 initProducts 方法未被调用 . 因为这个 products 数组保持为空,并且您在集合中看不到任何单元格 .

    您应该调用 initProducts (例如在 viewDidLoad 中)以使用将在您的集合中显示的元素填充 products 数组 .

    还要考虑从这个函数中删除参数(你没有在这个方法中使用参数,所以你不需要它) . 它应该看起来像:

    func initProducts() {
            products = DataService.instance.getGroceryOptions()
    }
    

    另外你应该纠正 getGroceryOptions 方法 . 它应该如评论中指出的那样返回 item .

  • 1

    你有一个 numberOfSectionsInCollectionView 部分超越其他 collectionView 功能吗?

    看看这个例子:

    func numberOfSectionsInCollectionView(collectionView:UICollectionView) -> Int {
        return 1
    }
    

相关问题