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!
}