3

I've been trying to figure out how i can use a UICollectionView to display an array of UIButtons. Each button has its own (different) action, image, title etc. When a button (which covers the entire cell in the collection view) is pressed that button is then deleted, and the action is executed. I'm new to Swift, not sure where to start, I know my code is wrong but I'll add it anyway:

import UIKit

let reuseIdentifier = "cell"

class CollectionViewController: UICollectionViewController {


var buttons = [UIButton]()
override func viewDidLoad() {
    super.viewDidLoad()


    let button1 = UIButton()
    let button2 = UIButton()
    let button3 = UIButton()
    buttons = [button1,button2,button3]
    button1.addTarget(self, action: #selector(target), forControlEvents: .TouchUpInside)
    button2.addTarget(self, action: #selector(target2), forControlEvents: .TouchUpInside)
    button3.addTarget(self, action: #selector(target3), forControlEvents: .TouchUpInside)
    //haven't added images and titles yet...

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}



override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}


override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of items
    return buttons.count
}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {



    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CollectionViewCell

    cell.button = buttons[indexPath.row]


    return cell
}

Cell Class

import UIKit

class CollectionViewCell: UICollectionViewCell {


@IBOutlet var button: UIButton!

}
2
  • When you click the button, only the button should disappear, or the entire cell? Commented Jun 10, 2016 at 15:53
  • only the button, I was thinking of deleting the button from the array in the action function. Or would deleting the cell be a better approach? Commented Jun 10, 2016 at 16:01

2 Answers 2

3
  1. I don't think it's a good idea to add a UIButton to the cell to trigger some action since that could easily be done through collectionView(_:, didSelectItemAtIndexPath:). Each cell will have an extra button that is unnecessary. If the cell count gets big, you will have a slow scrolling collection view.

Instead of embedding a button, in the implementation of collectionView(_:, didSelectItemAtIndexPath:), check the section and item, and call the function that would have been the action for the button.

  1. My understanding is that be it collection view or table view, the cells should be a graphic representation of some data. So if you want to make the cells go away, you should have the corresponding data go away first, and use deleteItemsAtIndexPaths.
Sign up to request clarification or add additional context in comments.

Comments

1

Your code is really close I think. There are lots of ways to accomplish something like this.

If you want the button (and the collectionViewCell) to disappear, you can just remove that button from buttons and call reloadData() on the collectionView.

If you want to keep the collectionView cell, and just remove the button, you could try changing your buttons definition to hold optional buttons (original declaration is not shown in your code), but like this:

var buttons : [UIButton?]

then when the button is clicked, change that button to nil. When cellForItemAtIndexPath is called, check if the button is in the array, if it is not, just skip it (or hide it or whatever you need to do!)

Let me know if you need to see more sample code for this!

1 Comment

the line of code "cell.button = buttons[indexPath.row]" is wrong i think. Because button1's action is not being called when the first (or any) of the buttons are pressed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.