【问题标题】:Swift 3: button on collectionviewSwift 3:collectionview 上的按钮
【发布时间】:2017-05-31 14:51:42
【问题描述】:

我在 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() {

}

【问题讨论】:

    标签: swift button uicollectionview swift3 uicollectionviewcell


    【解决方案1】:

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

    【讨论】:

    • 如果需要的话,将 indexPath 添加到 masterCellAction
    • 非常感谢,请问如何将 indexPath 添加到 masterAction 中?
    • @IBAction func masterCellAction(indexpath : IndexPath) { }
    • 因为这是一个选择器,它知道从哪里获取 indexPath,但是#selector 代码也会更改为 #selector(masterCellAction(:)) 或 (_:),请先更改 func,自动完成将完成其余的工作
    • 控制台报错“无法识别的选择器发送到实例”override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { [...] cell.masterButton.addTarget(self,action: #selector(masterAction(indexPath:)),for: .touchUpInside) return cell } func masterAction(indexPath: IndexPath) { let presentCounter = counters[indexPath.row] presentCounter.valueCD = presentCounter.valueCD + 1 }
    【解决方案2】:

    您可以创建一个继承 UIButton 的自定义类(例如 CustomButton)并添加一个 NSIndexPath 类型的属性 indexPath。然后,将所有按钮设置为 CustomButton 类型,您可以轻松知道要更新哪个单元格。

    【讨论】:

      【解决方案3】:

      我认为你应该有两个这样的解决方案:

      1. 创建您自己的单元格类。这意味着按钮将自己处理点击事件。通过这种方式,您需要创建一个新的 UICollectionViewCell 子类。
      2. 假设当用户选择单元格时(我的意思是用户可以点击单元格中的任何位置),您只需要实现 didSelectAtCell 函数,通过 indexPath 处理单元格。

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-28
        • 2017-05-04
        • 1970-01-01
        • 2018-01-11
        • 2023-04-01
        相关资源
        最近更新 更多