Andrea.cabral
Andrea.cabral

Reputation: 338

How can I add a "add" button at the end of the visible collection view in iOS

I am trying to add a button at the end of my collection view (of folders) to add a new cell (folder). The goal is to have always at the end a button to add new cells (folders).

Here is what I am doing: 1st I return the number of items + 1 (to have an additional cell to be used as a button..)

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    //#warning Incomplete method implementation -- Return the number of items in the section
    let sections = self.ICEFolderFetchedResultsController!.sections
    let sectionInfo: NSFetchedResultsSectionInfo = sections![section] as NSFetchedResultsSectionInfo
    println("ICEFoldersCVC - numberOfItems: left")
    return sectionInfo.numberOfObjects + 1
}

2nd I try to initialize the button in this method:

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    var cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifierFolderCell, forIndexPath: indexPath) as ICEFolderCell
    var numberOfItems = self.collectionView(self.ICEFolderCollectionView, numberOfItemsInSection: 0)
    println("index path row is: \(indexPath.row)")
    println("number of items is: \(numberOfItems-1)")
    if (indexPath.row == numberOfItems - 1) {
        println("initializing button!")
        var addCellButton = UIButton(frame: cell.frame)
        addCellButton.setTitle("Add", forState: UIControlState.Normal)
        addCellButton.addTarget(self, action: "addCellButtonPressed", forControlEvents: UIControlEvents.TouchUpInside)
        cell.addSubview(addCellButton)
    }

    println("ICEFoldersCVC - cellForItemAtIndexPath: left")
    return cell
}

3rd I implemented the selector like this:

    func addCellButtonPressed() {
    UIAlertView(title: "you did it!", message: "Add button was pressed :)", delegate: nil, cancelButtonTitle: "Great!")
}

but this one s never called as I never see the alert view...

And the result is one cell (since no added data in the persistent store yet) that cannot be touched. Nothing happens when I touch the cell..Here is a screenshot..of wait..cant..not enough reputation..wish i could..sry guys..

I would need some guidance to get that button working...I appreciate it! Best.

Upvotes: 3

Views: 1166

Answers (2)

Or Ron
Or Ron

Reputation: 2343

It seems like you forgot to call show on your alert

also, Instead of implementing a button and a method for the target use :

optional func collectionView(_ collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)

if the index path is from your last cell show the alert.

Upvotes: 1

Ganesh
Ganesh

Reputation: 101

Try these steps

1) Also I couldn't find any show method on the UIalertView. So write it

2) Always write some additional simple log inside the addCellButtonPressed to check whether the method is actually called or not.

I haven't used swift but have experience with objectiveC. Hope it will help you.

Upvotes: 0

Related Questions