Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Swift中更新按钮事件?_Swift - Fatal编程技术网

在Swift中更新按钮事件?

在Swift中更新按钮事件?,swift,Swift,我正在创建一个检查列表应用程序,并尝试更新按钮事件,该事件在单击后设置为图像本身 这是我的代码: protocol CustomeCellDelegate { func cell(cell: UITableViewCell, updateTheButton button: UIButton) } class Cells: UITableViewCell, UITextFieldDelegate { @IBOutlet weak var checkBox: UIButton!

我正在创建一个检查列表应用程序,并尝试更新按钮事件,该事件在单击后设置为图像本身

这是我的代码:

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
    }
}
我想更新选中和未选中后设置图像的按钮的两个事件。因此,我的表视图可以将可重用单元出列,并更新这些事件


但是结果只是按钮的一个事件,它被选中并更新,而不是未选中的事件。选中的按钮仍然重复我取消选中它所做的任何操作。

处理ui按钮的最佳方法是使用它自己的状态

您需要在一个数组中保存当前状态值。所以你可以把状态保存在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
}

我认为您不需要
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)
  }
}
另外,数据源方法不会告诉单元格是否按下了按钮。它只设置图像,但不设置变量
按钮pressed

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

请您解释一下,
在选中和取消选中后,我想记录设置图像的按钮的两个事件。
的意思是什么?@inderkumarratore您好,我刚刚编辑了该语句,希望它能让我的问题更清楚。感谢您抽出时间。您的
复选框动作
动作是否与
复选框
按钮相连?它工作正常,太棒了。感谢Rathore的帮助,我再次检查了您上面提到的步骤,也是错误的,我没有为我的函数中的未检查按钮提供任何图像数据,因此我制作了一个。感谢Nirmalsinh的帮助,我检查了您的方法,我会说它比我的方法好得多,但仍然有一个问题我无法发现,协议没有将我的按钮状态更新到单元格。我没有找到一个方法来做这件事,但如果你可以,我想知道更多的细节。