【问题标题】:Table view with button that changes label based on user input带有根据用户输入更改标签的按钮的表格视图
【发布时间】: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


【解决方案1】:

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

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

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

【讨论】:

  • 我意识到我应该添加示例代码,如果有时间我会稍后添加。
  • 能否贴出相关的代码示例?我想看看你在做什么。
  • @Marc 看起来您更新了进度,代码是否与您能够使用预定文本更新按钮名称的进度一致?
  • 是的,前 2 部分中的代码可以正常工作,因为它所做的只是将按钮名称替换为“按下”。但是,当我尝试用第 3 部分中的代码替换该命令以请求用户输入时,我收到 TableViewCell 中没有“当前”成员的错误。
  • 您可以在 TVC 上创建一个委托函数并在 tableview 控制器中设置该函数。在单元格类中,按钮的操作出口:public var cell_tapped = {} @IBAction func tapped_cell(_ sender: Any) { cell_tapped() } TableView 代码:cell.cell_tapped = { self.tapped_cell(at: index) } 然后 tapped_cell 是在视图控制器中声明的函数,它也会为您提供索引上下文。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-21
  • 2012-09-12
  • 2014-08-03
  • 1970-01-01
  • 1970-01-01
  • 2021-12-02
相关资源
最近更新 更多