【发布时间】:2018-03-04 04:33:31
【问题描述】:
我正在尝试在原型单元格中创建一个只有 1 个标签和 1 个按钮的表格视图。
当您单击按钮时,它会要求用户输入文本,然后将同一单元格中的标签替换为该文本。我已经能够创建一个版本,其中按下按钮会使用预先确定的文本更新相应的标签,而不是输入文本。
问题是:
(a) 似乎无法在我创建的 TableViewCell 类中运行询问用户输入的警报 - 似乎必须在 ViewController 中才能做到这一点?
(b) 已经设置了TableCellDelegate 协议,并且可以检测到按钮按下,然后传回 ViewController 以运行警报,但找不到将文本输入发送回@987654324 的方法@ 待更新。
任何帮助将不胜感激。
这里是 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),您似乎无法从表格单元格中调用它。
【问题讨论】:
-
将 UITextField 添加到 tableview 单元格或使用文本输入查找 UIAlertController
标签: ios swift uitableview delegates uialertcontroller