Mr_Vlasov
Mr_Vlasov

Reputation: 537

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

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?

Upvotes: 1

Views: 2138

Answers (4)

user15739304
user15739304

Reputation: 11

You can do something like this

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
    if indexPath.row < myArr.count {
        cell.configure(with: CollectionModelViewCell(label: myArr[indexPath.row].text))
    } else {
        cell.configure(with: CollectionModelViewCell(label: "+"))
    }
    return cell
}

Upvotes: 0

Nguyễn Hồng Quang
Nguyễn Hồng Quang

Reputation: 189

Here is my sulution : swift 4.2

 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       // +1 here for the extra add button
        // bottom of the collection view
        return item.count + 1
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        //initialization cell
        let cells : UICollectionViewCell?

        if indexPath.row < item.count {

         // if indexpath.row < item.count => assign value to cellPet 

            let cellPet = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell1", for: indexPath) as! CollectionViewCell1
          // setup data cell  or not
            cellPet.setupView(listPet: item[indexPath.row ])

          // assign values c​​ells
            cells = cellPet

        } else {
            // assign value to cellAdd 
            let cellAdd = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell2", for: indexPath) as! CollectionViewCell2
            // setup data cell or not setup

            // assign values c​​ells
            cells = cellAdd
        }
        return cells!
    }

Upvotes: 1

Zhang
Zhang

Reputation: 11607

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")
    }
}

Upvotes: 7

Josh Homann
Josh Homann

Reputation: 16327

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

Upvotes: 1

Related Questions