【发布时间】:2016-06-10 15:50:24
【问题描述】:
我一直试图弄清楚如何使用UICollectionView 来显示UIButtons 的数组。每个按钮都有自己的(不同的)动作、图像、标题等。当按下一个按钮(覆盖集合视图中的整个单元格)时,该按钮将被删除,并执行该动作。我是 Swift 新手,不知道从哪里开始,我知道我的代码是错误的,但我还是会添加它:
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
}
细胞类
import UIKit
class CollectionViewCell: UICollectionViewCell {
@IBOutlet var button: UIButton!
}
【问题讨论】:
-
当你点击按钮时,只有按钮消失,还是整个单元格消失?
-
只有按钮,我正在考虑从动作函数中的数组中删除按钮。还是删除单元格是更好的方法?
标签: swift uibutton uicollectionview uicollectionviewcell