Swift 3:collectionview 上的按钮
Swift 3: button on collectionview
我在 UICollectionView 的原型单元格中添加了一个按钮,我想更新单元格标签中显示的 属性:
非常感谢
编辑:
可能需要创建一个函数,该函数也可以获得 indexPath.row 但不知道该怎么做。
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
//let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
//var cell :CounterCollectionViewCell!
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CounterCollectionViewCell
// Configure the cell
let myColor :UIColor = UIColor(red: 240/255, green: 179/255, blue: 28/255, alpha: 1.0)
let presentCounter = counters[indexPath.row]
cell.nameLabel.text = presentCounter.nameCD
cell.valueLabel.text = String(presentCounter.valueCD)
cell.backgroundColor = myColor
cell.layer.cornerRadius=10
return cell
}
@IBAction func masterCellAction() {
}
我认为您应该有 2 个这样的解决方案:
- 创建您自己的单元格 class。这意味着按钮将自己处理点击事件。这样,就需要新建一个UICollectionViewCell subclass.
- 假设当用户select单元格时(我的意思是用户可以点击单元格中的任意位置),你只需要实现didSelectAtCell函数,通过indexPath处理单元格。
希望对您有所帮助。
在 cellForItem 上,将操作作为选择器添加到您的自定义按钮中,您像这样添加到自定义单元格中
cell?.customButton.addTarget(self, action: #selector(masterCellAction), for: .touchUpInside)
编辑:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
cell.masterButton.addTarget(self,action: #selector(masterAction(_:)),for: .touchUpInside)
return cell
}
func masterAction(_ sender: UIButton)
{
let indexPath = collectionView?.indexPath(for: ((sender.superview?.superview) as! CounterCollectionViewCell))
let presentCounter = counters[indexPath.row] presentCounter.valueCD = presentCounter.valueCD + 1
}
您可以创建一个继承 UIButton 的自定义 class(例如 CustomButton)并添加一个 NSIndexPath 类型的 属性 indexPath。然后,将所有按钮设为 CustomButton 类型,您可以很容易地知道要更新哪个单元格。
我在 UICollectionView 的原型单元格中添加了一个按钮,我想更新单元格标签中显示的 属性:
非常感谢
编辑: 可能需要创建一个函数,该函数也可以获得 indexPath.row 但不知道该怎么做。
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
//let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
//var cell :CounterCollectionViewCell!
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CounterCollectionViewCell
// Configure the cell
let myColor :UIColor = UIColor(red: 240/255, green: 179/255, blue: 28/255, alpha: 1.0)
let presentCounter = counters[indexPath.row]
cell.nameLabel.text = presentCounter.nameCD
cell.valueLabel.text = String(presentCounter.valueCD)
cell.backgroundColor = myColor
cell.layer.cornerRadius=10
return cell
}
@IBAction func masterCellAction() {
}
我认为您应该有 2 个这样的解决方案:
- 创建您自己的单元格 class。这意味着按钮将自己处理点击事件。这样,就需要新建一个UICollectionViewCell subclass.
- 假设当用户select单元格时(我的意思是用户可以点击单元格中的任意位置),你只需要实现didSelectAtCell函数,通过indexPath处理单元格。
希望对您有所帮助。
在 cellForItem 上,将操作作为选择器添加到您的自定义按钮中,您像这样添加到自定义单元格中
cell?.customButton.addTarget(self, action: #selector(masterCellAction), for: .touchUpInside)
编辑:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
cell.masterButton.addTarget(self,action: #selector(masterAction(_:)),for: .touchUpInside)
return cell
}
func masterAction(_ sender: UIButton)
{
let indexPath = collectionView?.indexPath(for: ((sender.superview?.superview) as! CounterCollectionViewCell))
let presentCounter = counters[indexPath.row] presentCounter.valueCD = presentCounter.valueCD + 1
}
您可以创建一个继承 UIButton 的自定义 class(例如 CustomButton)并添加一个 NSIndexPath 类型的 属性 indexPath。然后,将所有按钮设为 CustomButton 类型,您可以很容易地知道要更新哪个单元格。