UICollection NumberOfItemsInSection 向 vars 添加常量的问题

Posted

技术标签:

【中文标题】UICollection NumberOfItemsInSection 向 vars 添加常量的问题【英文标题】:UICollection NumberOfItemsInSections issue with adding a constant to vars 【发布时间】:2017-01-02 04:02:21 【问题描述】:

我有两个不同的单元格类型类,我正在尝试使用 PayNowCell 类创建第一个索引路径,让它保持显示。而其他单元格属于 CheckOutCell 类。现在问题出在我的 func numberofitemsinsection 中。目前我正在使用 return checkout.count 但它在视图加载时缺少 PayNowCell。如果我让它返回 checkout.count+1 以始终使 PayNowCell 可用;我的程序崩溃给我错误索引超出范围。数组检出是一个全局变量。有人可以解释原因并提供修复吗?被困在这个问题上一段时间了。代码如下。

class CheckoutController: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout  

    var inventoryTabController: InventoryTabController?

    override init(frame: CGRect)
        super.init(frame: frame)
        setupViews()

        NotificationCenter.default.addObserver(forName: .arrayValueChanged, object: nil, queue: OperationQueue.main)  [weak self] (notif) in
            self?.collectionView.reloadData()
        

    

    deinit 
        NotificationCenter.default.removeObserver(self)
    

    required init?(coder aDecoder: NSCoder) 
        fatalError("init(coder:) has not been implemented")
    

    func loadItems() -> [Item]? 
        return NSKeyedUnarchiver.unarchiveObject(withFile: Item.ArchiveURL.path) as? [Item]
    

    func saveItems() 
        let isSuccessfulSave = NSKeyedArchiver.archiveRootObject(checkout, toFile: Item.ArchiveURL.path)
        if !isSuccessfulSave 
            print("Failed to save items...")
        
    

    func addItem(item: Item) 
        items.append(item)
        collectionView.reloadData()
    

    func editItem(item: Item, index: Int) 
        items[index] = item
        collectionView.reloadData()
    

    func prepare(for segue: UIStoryboardSegue, sender: Any?) 
        // Identify which segue is occuring.
        if segue.identifier == "ShowDetail" 
            let itemDetailViewController = segue.destination as! AddItemController

            // Get the cell that generated this segue.
            if let selectedItemCell = sender as? InventoryCell 
                let indexPath = collectionView.indexPath(for: selectedItemCell)!
                let selectedItem = items[indexPath.row]
                itemDetailViewController.item = selectedItem
            
        
        else if segue.identifier == "AddItem" 
            print("Adding new meal.")
        
    

    lazy var collectionView: UICollectionView = 
        let layout = UICollectionViewFlowLayout()
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)

        cv.dataSource = self
        cv.delegate = self

        cv.backgroundColor = UIColor.rgb(r: 247, g: 247, b: 247)
        return cv
    ()

    let cellId = "cellId"
    let paynow = "paynow"
    func setupViews() 
        backgroundColor = .brown

        addSubview(collectionView)

        addConstraintsWithFormat("H:|[v0]|", views: collectionView)
        addConstraintsWithFormat("V:|[v0]|", views: collectionView)
        collectionView.indicatorStyle = UIScrollViewIndicatorStyle.white

        collectionView.register(PayNowCell.self, forCellWithReuseIdentifier: paynow)

        collectionView.register(CheckoutCell.self, forCellWithReuseIdentifier: cellId)
    

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 
        return checkout.count //init number of cells
    

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 

        if indexPath.item == 0 
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "paynow", for: indexPath) as! PayNowCell //init cells
        return cell
        else
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! CheckoutCell //init cells
        print("Printing this \(checkout.count)")
        cell.item = checkout[indexPath.item]
        return cell
        

    

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) 
        //let item = items[indexPath.item]
        //inventoryController?.showItemDetailForItem(item: item, index: indexPath.item)
        print("selected")
        print(indexPath.item)
        collectionView.reloadData()
    

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize 
        if indexPath.item == 0 
            return CGSize(width:frame.width, height: 100) //each cell dimensions
        else
            return CGSize(width:frame.width, height: 150) //each cell dimensions
        
    

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat 
        return 0
    

【问题讨论】:

【参考方案1】:

最好将您的收藏视图重新排列为具有 2 个部分:

    第 0 部分:PayNow 单元格(仅 1 个单元格) 第 1 部分:结帐单元格(使用结帐数组列表)

那么您对 ​​indexPath.item 问题没有任何困惑。

func numberOfSections(in collectionView: UICollectionView) -> Int 
    return 2


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 
    return section == 0 ? 1 : checkout.count


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 

    if indexPath.section == 0 
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "paynow", for: indexPath) as! PayNowCell //init cells
        return cell
     else 
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! CheckoutCell //init cells
        print("Printing this \(checkout.count)")
        cell.item = checkout[indexPath.item]
        return cell
    


【讨论】:

完美解决方案。非常感谢!【参考方案2】:

您需要从 cellForRow 函数中减去 1。由于您正在添加 PayNowCell,因此您正在添加一个额外的 indexPath,但目前,您正在使用 dataSource 函数给出的默认 indexPath。该索引将始终比您的项目数高一。通过从 indexPath 中减去 1,您将恢复与 itemsArray 的同步,同时考虑到 PayNowCell。

cell.item = checkout[indexPath.item - 1]

【讨论】:

以上是关于UICollection NumberOfItemsInSection 向 vars 添加常量的问题的主要内容,如果未能解决你的问题,请参考以下文章

UICollection 的镜像滚动

视频库的UICollection

UICollection 单元格快速混合

UICollection类似TableView的标题

如何在 iOS swift 中的 ARSCNView 中显示 uicollection 视图

UICollection 的 IndexPath 到底是啥