如何在UIButton单击后更改UITableview单元格标签文本



我在UITableViewCell中有一个复选框(UIButton)和一个标签。我想在单击复选框时更改标签的文本(颜色 + 删除线)。

这是针对配方应用程序的。烹饪步骤完成后,用户可以按原样"检查"它。

这是我当前用于表视图的 cellForRowAt 函数:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if tableView == groceryTableView {
        let cell = tableView.dequeueReusableCell(withIdentifier: groceryTableViewCell, for: indexPath) as! GroceryItemTableViewCell
        cell.amoutLabel.text = indexPath.item % 2 == 0 ? "50 g" : "500 ml"
        cell.itemLabel.text = indexPath.item % 2 == 0 ? "Cheese" : "Milk"
        cell.selectionStyle = .none
        return cell
    }
    else {
        let cell = tableView.dequeueReusableCell(withIdentifier: cookingStepTableViewCell, for: indexPath) as! CookingStepTableViewCell
        cell.cookingStepDescription.text = indexPath.item % 2 == 0 ? "Test 123..." : "Test 321..."
        cell.selectionStyle = .none
        cell.delegate = self
        return cell
    }
}

这是我的 Button addTarget 函数,它从 TableViewCell 类委托给实际的 ViewController 类:

func cookingStepDone(description: String, isDone: Bool) {
    // if isDone == true
    // label textcolor is gray + strikethrough
    // if isDone == false
    // no change...
}

我希望如果"isDone"为真,则更改cell.cookStepDescription标签(=单击复选框)

假设按钮出口是在单元格类中采用的。 所以在cellForRowAtIndexpath中声明一个操作方法,即像这样。

 cell.yourDoneBtn?.addTarget(self, action: #selector(self.cookingStepDone), for: .touchUpInside)

现在在您的操作函数中:

@objc func cookingStepDone(sender: UIButton)
 {
    let location = self.yourTableViewName?.convert(sender.bounds.origin, from:sender)
    let indexPath = self.yourTableViewName?.indexPathForRow(at: location!)
    if let cell = self.yourTableViewName.cellForRow(at: indexPath!) as? yourTableViewCell  // i.e groceryTableViewCell or CookingStepTableViewCell
    {
      if isDone == true
      {
        // Set your cell label textcolor to gray + strikethrough 
      }
      else
      {
       // no change
      }
    }
    DispatchQueue.main.async
    {
       self.yourTableView.reloadData() // reload your table view 
    }
 }

根据需要设置布尔值。

您可以使用

以下方法执行此操作

定义一个数组,在你 cookStepDone 方法中添加indexPath数组,如果 indexPath 已经在数组中,请将其删除并重新加载表视图。 并在cellForRowAtIndexpath方法中,检查数组是否包含indexPath。 如果包含使文本删除线通过否则使正常。

如果你创建一个新类,其超类将是UITableViewCell,并在该类中添加@IBOutlets(UIButton和UILabel)和一个@IBAction(buttonWasTapped)会怎样?

像这样:

class RecipeTableViewCell: UITableViewCell {
    @IBOutlet var myButton : UIButton!
    @IBOutlet var myLabel : UILabel!
    @IBAction func didTouchButton(sender : UIButton)
    {
         myLabel.textColor = UIColor.green;
    }
}

查看此代码: RecipeTableViewCell

class RecipeTableViewCell: UITableViewCell {
@IBOutlet var myButton : UIButton!
@IBOutlet var myLabel : UILabel!
var buttonClick : (() -> Void)? = nil
override func awakeFromNib() {
    myButton.addTarget(self, action: #selector(didTouchButton(sender:)), for: .touchUpInside)
}
@IBAction func didTouchButton(sender : UIButton)
{
    if let action = buttonClick {
        action()
    }
}
}

单元格中ForRowat

let cell = tableView.dequeueReusableCell... 
// Your code ...
cell.buttonClick = {
  //access your label and data from here
  cell.yourLbl.text = yourModel[indexPath.row].text
}

最新更新