在Swift中更新按钮事件?

时间:2017-12-08 10:17:27

标签: swift

我正在创建一个Check List应用程序并尝试更新按钮事件,该事件在单击后自动设置图像。

这是我的代码:

protocol CustomeCellDelegate { 
   func cell(cell: UITableViewCell, updateTheButton button: UIButton)
}

class Cells: UITableViewCell, UITextFieldDelegate {

@IBOutlet weak var checkBox: UIButton!
    var buttonPressed = true
@IBAction func checkBoxAction(_ sender: UIButton) {
    if buttonPressed {
        sender.setImage(UIImage(named: "checkBoxB"), for: UIControlState.normal)
        buttonPressed = false
    } else {
        sender.setImage(UIImage(named:""), for: UIControlState.normal)
        buttonPressed = true
    }
}
 @objc func recordButtonImage(_ button: UIButton) {
    cellDelegate?.cell(cell: self, updateTheButton: button)  // cell notify the button that touched.
}
override func awakeFromNib() {
    checkBox.addTarget(self, action: #selector(recordButtonImage(_:)), for: UIControlEvents.touchUpInside)  
    }
}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, CustomeCellDelegate {

@IBOutlet weak var tableView: UITableView!

var myImages: [UIImage?]!

override func viewDidLoad() {
  super.viewDidLoad()
    self.tableView.delegate   = self
    self.tableView.dataSource = self
    myImages = Array(repeatElement(nil, count: 50))
    let nib = UINib(nibName: "Cells", bundle: nil)
    tableView.register(nib, forCellReuseIdentifier: "Cells")
 }

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 50
}

internal func tableView(_ tableView:UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "theCells") as? Cells
        cell?.theTextField.delegate = self
        cell?.cellDelegate = self
        cell?.checkBox.setImage(myImages[indexPath.row], for: UIControlState.normal)

    return cell!        
}

// This function from protocol and it helps to update button images.
func cell(cell: UITableViewCell, updateTheButton button: UIButton) {
    print("Delegate method Update Button Images.")
    if let indexPath = tableView.indexPath(for: cell), let buttonImage = button.image(for: UIControlState.normal) {
        myImages[indexPath.row] = buttonImage
    }
}

我想更新在选中未选中之后设置图片的按钮的两个事件。因此,我的表视图可以使可重用单元格出列并更新这些事件。

但结果只是一个按钮事件被检查更新但不是未经检查的按钮事件。选中的按钮仍然会重复我取消选中的任何操作。

2 个答案:

答案 0 :(得分:1)

我认为您不需要recordButtonImage方法,您应该从按钮点击操作调用委托方法,即checkBoxAction,如下所示

class Cells: UITableViewCell, UITextFieldDelegate {
  @IBOutlet weak var checkBox: UIButton!
  var buttonPressed = true
  @IBAction func checkBoxAction(_ sender: UIButton) {
    if buttonPressed {
      sender.setImage(UIImage(named: "checkBoxB"), for: UIControlState.normal)
      buttonPressed = false
    } else {
      sender.setImage(UIImage(named:""), for: UIControlState.normal)
      buttonPressed = true
    }
    // cell notify the button that touched.
    cellDelegate?.cell(cell: self, updateTheButton: button)
  }
}

此外,您的数据源方法不会告诉单元格是否按下了按钮。它只是设置图像,但不设置变量buttonPressed

 internal func tableView(_ tableView:UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  let cell = tableView.dequeueReusableCell(withIdentifier: "theCells") as? Cells
  cell?.theTextField.delegate = self
  cell?.cellDelegate = self
  cell?.checkBox.setImage(myImages[indexPath.row], for: UIControlState.normal)

  return cell!
}

答案 1 :(得分:0)

处理 UIButton 的最佳方法是使用自己的状态。

您需要在一个数组中保存当前状态值。所以你可以把状态保存在cellForRow和其他东西中。默认情况下,将所有状态设置为 false

在CellForRow中,只需输入以下代码:

  YOUR_BUTTON.isSelected = aryState[indexPath.row] as! Bool

设置按钮的图像

YOUR_BUTTON.setImage(UIImage(named: NORMAL_IMAGE), for: .normal)
YOUR_BUTTON.setImage(UIImage(named: SELECTED_IMAGE), for: .selected)

点击时只需更改按钮状态:

@IBAction func checkBoxAction(_ sender: UIButton) {
    let button = sender
    button.isSelected = !button.isSelected
}