How to add an "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 (from folders) to add a new cell (folder). The goal is to always have a button at the end to add new cells (folders).

This is what I do: 1st I return the number of items + 1 (to add an extra cell 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 am trying to initialize a 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 have implemented the selector like this:

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

      

but this one was never called since I never saw the kind of warning ...

And the result is one cell (since there is no data in it yet) that cannot be touched. Nothing happens when I touch the cell. Here's a screenshot ... Wait ... not enough reputation. I could ... guys ..

I will need a manual to make this button work ... I appreciate it! Best.

+3


source to share


2 answers


It looks like you forgot to call show

your alert.

Also, instead of using the button and method to use the target:



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

if the signpost from your last cell shows a warning.

+1


source


Try the following steps.

1) Also I could not find any show method in UIalertView. So write



2) Always write additional additional log inside addCellButtonPressed to check if this method was actually called or not.

I have not used swift, but have experience with objectC. Hope this helps you.

0


source







All Articles