Add “Add New” button in the last cell of UICollectionCell. (CoreData)

喜夏-厌秋 提交于 2020-01-03 05:20:53

问题


Trying to tackle this issue from different way. Current implementation gives me an error: 'NSInvalidArgumentException', reason: 'no object at index 3 in section at index 0' It occurs at class AppDelegate: UIResponder...

My code is

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionCell
    configureCell(cell: cell, indexPath: indexPath)

    var numberOfItems = self.collectionView(self.collectionView, numberOfItemsInSection: 0)

    if (indexPath.row == numberOfItems - 1) {

        var addCellButton = UIButton(frame: cell.frame)
        addCellButton.setTitle("Add", for: UIControlState.normal)
        cell.addSubview(addCellButton)
    } 
    return cell
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    if let sections = controller.sections {
        let sectionInfo = sections[section]
        return sectionInfo.numberOfObjects + 1
    }
    return 0
}

func numberOfSections(in collectionView: UICollectionView) -> Int {
    if let sections = controller.sections {
        return sections.count
    }
    return 0
}

Any advice?


回答1:


You can write it like this to dequeue two separate cells:

import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    var collectionView:UICollectionView!
    var items = ["Apple", "Banana", "Orange", "Watermelon", "Coconut"]


    ...

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        // ------------------------------------------
        // +1 here for the extra add button
        // at the bottom of the collection view
        // ------------------------------------------
        return items.count + 1
    }

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

        // --------------------------------
        // IndexPath row vs Items Count
        // --------------------------------
        // [0] = Apple
        // [1] = Banana
        // [2] = Orange
        // [3] = Watermelon
        // [4] = Coconut
        //
        // [5] = special cell
        //
        // ---------------------------------
        let cellID = indexPath.row < items.count ? "normalCell" : "specialCell"

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath)

        setupCell(cell: cell, indexPath: indexPath, type: cellID)

        return cell
    }

    func setupCell(cell: UICollectionViewCell, indexPath: IndexPath, type: String) {
        switch(type) {
        case "normalCell":
            setupFruitCell(cell: cell as! FruitCell, indexPath: indexPath)
        case "specialCell":
            setupSpecialCell(cell: cell as! SpecialCell, indexPath: indexPath)
        default:
            break
        }
    }

    func setupFruitCell(cell: FruitCell, indexPath: IndexPath) {
        cell.label.text = items[indexPath.row]
    }

    func setupSpecialCell(cell: SpecialCell, indexPath: IndexPath) {
        cell.btnAdd.addTarget(self, action: #selector(addButtonTapped), for: UIControlEvents.touchUpInside)
    }

    func addButtonTapped(sender: UIButton) {
        print("Show UI to add new fruit")
    }
}



回答2:


Do not mess with your cells by adding views to them. Cells get recycled when they go off screen (thats why its called dequeueReusableCell). Unless you are removing the extra view in prepare for reuse, they stay on the cell and get added over an over again. Instead you have 2 other options:

1) Make a second collectionViewCell in interface builder with its own class and reuseIdentifier and deque an instance of this cell only for the last cell.

2) Make the button part of a footer view and return it in viewForSupplementaryElementOfKind



来源:https://stackoverflow.com/questions/41629963/add-add-new-button-in-the-last-cell-of-uicollectioncell-coredata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!