如何从模态 UIView 更新自定义单元格中的 UIButton 标题

Posted

技术标签:

【中文标题】如何从模态 UIView 更新自定义单元格中的 UIButton 标题【英文标题】:How do I update a UIButton title in a custom cell from a modal UIView 【发布时间】:2019-01-12 03:12:35 【问题描述】:

我正在为我的一个朋友编写一个旧应用程序的克隆。应用程序中的一件事是点击自定义单元格中的“优先级”按钮,它会显示一个选择器视图,并更改按钮的标题以显示任务的优先级。

选择器:

选择器在 UIView 中作为模式。我想将选择器设置为我想要的数字,然后关闭模式并将按钮标题更改为我在选择器中选择的数字。我已经尝试了各种各样的事情,但我无法弄清楚如何去做。除了按钮是自定义表格视图单元格的一部分之外,它不会那么糟糕。我如何让它做到这一点?

MainListViewController:

import UIKit

var defaultCell = "defaultCell"
var itemIndex = 0
var testData: [String] = []
var listItem: [(priorityLevel: Int, itemText: String)] = []

class MainListViewController: UITableViewController, PriorityDelegate 
@IBOutlet weak var addButton: UIBarButtonItem!

var itemTF = UITextField()
var setIndex = 0

override func viewDidLoad() 
    super.viewDidLoad()
    self.tableView.tableFooterView = UIView()


override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    return testData.count


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    let cell = tableView.dequeueReusableCell(withIdentifier: defaultCell, for: indexPath) as! ListItemCell
    cell.itemLabel.text = testData[indexPath.row]
    cell.priorityButton.addTarget(self, action: #selector(priorityButtonClicked(sender:)), for: .touchUpInside)
    return cell


override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
    itemIndex = indexPath.row
    performSegue(withIdentifier: "segueID", sender: self)


override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat 
    return CGFloat(75)


override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) 
    guard editingStyle == .delete else return
    testData.remove(at: indexPath.row)
    tableView.deleteRows(at: [indexPath], with: .automatic)


@IBAction func addButton(_ sender: UIBarButtonItem) 

    let alertController = UIAlertController(title: "Alert title", message: "Message to display", preferredStyle: .alert)

    alertController.addTextField  (itemTF) in itemTF.placeholder = "Enter your item" 

    // Create OK button
    let OKAction = UIAlertAction(title: "OK", style: .default)  (action:UIAlertAction!) in

        // Code in this block will trigger when OK button tapped.
        guard let itemText = alertController.textFields?.first?.text else return
        self.add(itemText)

    
    alertController.addAction(OKAction)

    // Create Cancel button
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) /* 
        (action:UIAlertAction!) in print("Cancel button tapped"); // Uncomment to make Cancel button do stuff
     */
    alertController.addAction(cancelAction)

    // Present Dialog message
    self.present(alertController, animated: true, completion:nil)


func add(_ newItem: String) 
    testData.insert(newItem, at: 0)
    let indexPath = IndexPath(row: 0, section: 0)
    tableView.insertRows(at: [indexPath], with: .left)


@objc func priorityButtonClicked(sender: UIButton) 
    print("Button pressed")
//        performSegue(withIdentifier: "segue", sender: self)


override func prepare(for segue: UIStoryboardSegue, sender: Any?) 
    if segue.identifier == "segue" 
        let vc : PriorityViewController = segue.destination as! PriorityViewController
        vc.delegate = self
    


func setPriorityLevel(level: Int) 
    print(level)
    // Need to refigure protocol


PriorityViewController(这有选择器):

import UIKit

protocol PriorityDelegate 
func setPriorityLevel(level: Int)


class PriorityViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource 

@IBOutlet weak var priorityPicker: UIPickerView!
@IBOutlet weak var savePriorityButton: UIButton!

var priorityChosen = ""
var priorityIndex = 0
var delegate : PriorityDelegate?

let priorityLevel = ["1", "2", "3", "4", "5"]

override func viewDidLoad() 
    super.viewDidLoad()


func numberOfComponents(in pickerView: UIPickerView) -> Int 
    return 1


func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
    return priorityLevel.count


func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? 
    return priorityLevel[row]


func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 
//        priorityChosen = priorityLevel[row]
    priorityIndex = row


@IBAction func savePriority(_ sender: UIButton) 
    if delegate != nil 
        delegate?.setPriorityLevel(level: priorityIndex)
    

    dismiss(animated: true)


ListItemCell:

import UIKit


class ListItemCell: UITableViewCell 

@IBOutlet weak var priorityButton: UIButton!
@IBOutlet weak var itemLabel: UILabel!

override func awakeFromNib() 
    super.awakeFromNib()
    // Initialization code


override func setSelected(_ selected: Bool, animated: Bool) 
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state



【问题讨论】:

最简单的方法就是***.com/questions/5418655/… 【参考方案1】:

好的,我终于让它工作了!我从几个不同的来源拼凑了一些东西,这就是我想出的。我希望这可以帮助其他人,因为我找不到如何做到这一点。

My GitHub for this project

【讨论】:

以上是关于如何从模态 UIView 更新自定义单元格中的 UIButton 标题的主要内容,如果未能解决你的问题,请参考以下文章

如何在 cellforrowatindexpath 之后更新自定义表格单元格中的 UIButton 图像?

将数据从自定义表格视图单元格传递到 UIView 控制器

自定义单元格中的 MPMoviePlayerViewController

iOS:如何从 tableview 的自定义单元格中的 UITextField 访问值

如何从集合视图中的不可见单元格中删除自定义视图

calayer 没有根据 tableview 单元格中的 UIView 大小而改变