gpt4 book ai didi

ios - 带有根据用户输入更改标签的按钮的 TableView

转载 作者:行者123 更新时间:2023-11-30 11:43:23 26 4
gpt4 key购买 nike

我正在尝试在原型(prototype)单元格中创建一个仅包含 1 个标签和 1 个按钮的表格 View 。

当您单击该按钮时,它会要求用户输入文本,然后用该文本替换同一单元格中的标签。我已经能够创建一个版本,其中按下按钮会使用预先确定的文本更新相应的标签,但不会更新输入文本。

问题是:

(a) 似乎无法在我创建的 TableViewCell 类中运行要求用户输入的警报 - 似乎必须在 ViewController 中才能执行此操作?

(b) 已设置 TableCellDelegate 协议(protocol),并且可以检测按钮按下,然后传回 ViewController 来运行警报,但找不到发送文本输入的方法返回到要更新的 TableViewCell

如有任何帮助,我们将不胜感激。

这是 ViewController 代码:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}

extension ViewController: TableCellDelegate {
func didTapButton(label: String) {
print(label)
}
}

extension ViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell") as! TableViewCell
let label = "Label " + String(indexPath.row)
cell.setLabel(label: label)
cell.delegate = self
return cell
}
}

这是 TableViewCell(没有警报编码):

import UIKit

protocol TableCellDelegate {
func didTapButton (label: String)
}

class TableViewCell: UITableViewCell {

@IBOutlet weak var tableCellLabel: UILabel!

var delegate: TableCellDelegate?
func setLabel(label: String) {
tableCellLabel.text = label
}

@IBAction func buttonTapped(_ sender: Any) {
delegate?.didTapButton(label: tableCellLabel.text!)
tableCellLabel.text = "pressed"
}
}

最后,这是我尝试插入的警报代码来代替上面的 tableCellLabel.text = "pressed" 代码:

        // Create the alert window
let buttonAlert = UIAlertController(title: "Label", message: "Enter Label", preferredStyle: UIAlertControllerStyle.alert)

// Add text input field to alert controller
buttonAlert.addTextField { (buttonLabel:UITextField!) -> Void in
buttonLabel.placeholder = "Enter Label"
}

// Create alert action for OK button
let okButtonAction = UIAlertAction.init(title: "OK", style: UIAlertActionStyle.default) { (alert:UIAlertAction!) -> Void in

// Get the button name from the alert text field
let buttonLabel = buttonAlert.textFields![0]
let buttonLabelString = buttonLabel.text
self.tableCellLabel.text = buttonLabelString

// Dismiss alert if ok pressed
buttonAlert.dismiss(animated: true, completion: nil)
}

// Add ok Button alert actions to alert controller
buttonAlert.addAction(okButtonAction)

// Create alert action for a cancel button
let cancelAction = UIAlertAction.init(title: "Cancel", style: UIAlertActionStyle.cancel) { (alert:UIAlertAction!) -> Void in

// Dismiss alert if cancel pressed
buttonAlert.dismiss(animated: true, completion: nil)
}

// Add Cancel Button alert actions to alert controller
buttonAlert.addAction(cancelAction)

// Display the alert window
self.present(buttonAlert, animated: true, completion: nil)

出错的地方是 self.present(buttonAlert,animated: true,completion: nil),您似乎无法从表格单元格调用它。

最佳答案

根据您设置 UItableview 的方式,听起来您会想要获取正在注册警报的单元格的数据 ID,然后更新按钮文本在该单元格中基于 TableView 渲染语句中的单元格。请记住,设置按钮标签具有动画效果,如果您同时为其他内容设置动画,它可能会导致发生一些奇怪的事情。

您可能希望在表格 View 中查看下面链接中的重新加载行。但请注意,如果您使用 dequeueReusableCell,则 indexPath 并不总是与调用用户交互时相同。 UITableView Reference

参见UIButton Reference为了快速实现,您必须将其设置为按钮的当前状态。

关于ios - 带有根据用户输入更改标签的按钮的 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49092003/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com