Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使自定义collectionview单元格可见?_Ios_Swift_Uicollectionview_Uicollectionviewcell - Fatal编程技术网

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

Ios 如何使自定义collectionview单元格可见?,ios,swift,uicollectionview,uicollectionviewcell,Ios,Swift,Uicollectionview,Uicollectionviewcell,我似乎无法使自定义collectionView单元格可见。我知道CollectionView出现是因为我在属性检查器上更改了背景色,但是自定义单元格没有出现。我在属性检查器中设置了约束,并且在图像下有一个水平堆栈视图 我完全按照课程的方向走,但似乎不起作用。以下是我如何配置单元格的外观: 代码如下: import UIKit class ListVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource

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

我完全按照课程的方向走,但似乎不起作用。以下是我如何配置单元格的外观:

代码如下:

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
    }

}
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
    }
}
以下是DataService实现:

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
    }

}
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
    }
}
以下是产品实现:

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
    }

}
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
    }
}
未调用您的initProducts方法。由于此products数组保持为空,因此在集合中看不到任何单元格

您应该调用initProducts,例如在viewDidLoad中调用,用集合中显示的元素填充products数组

也考虑从这个函数中移除参数,你不在这个方法中使用参数,所以你不需要它。它应该是这样的:

func initProducts() {
        products = DataService.instance.getGroceryOptions()
}
另外,您应该纠正getGroceryOptions方法。它应该返回注释中指出的项目。

您是否在collectionView部分中有一个numberOfSectionsInCollectionView部分位于其他collectionView功能之前

看看这个例子:

func numberOfSectionsInCollectionView(collectionView:UICollectionView) -> Int {
    return 1
}
1您的initProducts方法没有在代码中的任何地方调用

2更新数据源后,必须调用collectionView.reloadData。也就是说,您必须在更新products属性后调用此方法

3另外,正如其他人在回答中提到的,似乎你的getGroceryOptions方法实际上应该返回项目而不是产品。顺便说一句,这是什么


4请按照swift风格指南编写代码。您将更容易区分类/结构和变量。您还可以了解更多信息。

getGroceryOptions的功能是什么?确保有要显示的数据。我会先添加一些打印语句或调试器断点,以获取实际执行的代码的图像。似乎products数组未初始化。return products是否应为return item?@PhillipMills这是我第一次尝试应用程序,感谢您提醒我使用打印语句,并且它不应该返回项,我忘记了在所有范围内进行编辑。根据文档,缺少此方法的实现会导致一个单独的部分。在这种情况下没关系。感谢您的帮助,当我使用collectionView.reloadItemsat:[indexPath]而不是collectionView.reloadData时,因为这不起作用。这是获得相同结果的另一种方法吗?我更改了将产品返回到项目的方法,但肯定忘记了更改它,我认为我应该在所有范围内使用edit。我将进一步关注样式指南和github样式指南,当您更新少数模型并需要更新集合视图中的相关单元格时,应使用thankscollectionView.reloadItemsat:[indexPath]。但是,当您更改数据源正在使用的整个阵列时,您可以选择collectionView.reloadData:您可以在文档中查找有关这些方法的更多信息