3

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.

Andrea.cabral
  • 338
  • 3
  • 16
  • This will make it difficult to work with selection. Better use a footer section described in this answer: https://stackoverflow.com/questions/31577984/add-button-at-the-end-of-collection-view-in-storyboard – mahal tertin Sep 25 '17 at 16:08

2 Answers2

1

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.

Or Ron
  • 2,313
  • 20
  • 34
  • Not sure I understand why your proposed method would replace the implementation of the button and the target...the goal of the last cell being able to add a new cell... – Andrea.cabral Jan 13 '15 at 13:17
  • I get why you would do that. I tried just by implementing the proposed method and a basic if statement to show the alerview if the indexpath is the last...but the method is never called...am i missing smg ? Thx.. – Andrea.cabral Jan 13 '15 at 13:37
  • didHighlight and didSelect are called when the cell is first clicked. On the second the two methods are called again. However, "shouldDeselect" & "didDeselect" are not called. – Andrea.cabral Jan 13 '15 at 13:45
  • well I think I got it...a cell is deselected when another one is selected. Having only one cell, this does not seem to work... – Andrea.cabral Jan 13 '15 at 14:01
  • @Andrea.cabral you want to make the last cell as add, meaning you need to recognize when the cell is being clicked on. you can do that with a button but the collection view already has a method for that. you can recognize when this specific cell was selected ( clicked on ) and activate the add functionality. As I wrote you can recognize that with `optional func collectionView(_ collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath)` this will return the index path of the cell the user clicked on – Or Ron Jan 13 '15 at 14:53
  • thx for your help. Why would you use "didDeselect" instead of didSelect ? I tried yours but did not work. DidDeselect is never called when I click on the cell. However it is when two cells are present and I select a second cell, the first one being deselected...again as I wrote at start when no cells (folders) are added and only that one cell "add" is present it does not work...Anyway works with didselect and you put me on the right track thx again! – Andrea.cabral Jan 14 '15 at 13:37
0

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.

Ganesh
  • 101
  • 1
  • 11
  • Thx, indeed, I forgot the show method. It works, the selector "addCellButtonPressed" is called. However the title "Add" is not shown on the button...any idea why ? – Andrea.cabral Jan 13 '15 at 13:14