尝试使用委托共享来自自定义 tableViewCell 的文本输入并且数据不共享

Trying to use delegate to share text input from custom tableViewCell and Data is not sharing

我正在尝试使用委托方法从自定义单元格按钮中获取数据并将其按到 ViewController 中的数组中。

该按钮运行良好,if let 运行良好,但数据未按我希望的那样从自定义单元格按钮传输到 ViewController 数组...

我对编码还很陌生,所以我知道我做错了一些显而易见的事情。我正在应用来自 SO 的以下内容:

Getting Textfield Values from Custom Table View Cell

我的自定义单元格:

import UIKit

class AddEntrantCell: UITableViewCell, UITextFieldDelegate {

var entrantName: ((String) -> Void)?
var entrantCompany: ((String) -> Void)?

@IBOutlet weak var entrantNameTextField: UITextField!
@IBOutlet weak var entrantCompanyTextField: UITextField!
@IBOutlet weak var addEntrant: UIButton!

var delegate: CellDelegate?

override func awakeFromNib() {
    super.awakeFromNib()
    entrantNameTextField.delegate = self
    entrantCompanyTextField.delegate = self
            
    if Global.uiMode == true {
        overrideUserInterfaceStyle = .light
    } else {
        overrideUserInterfaceStyle = .dark
    }
}

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

// Button press
@IBAction func addEntrantButton(_ sender: UIButton) {
    if let name = entrantNameTextField.text, let company = entrantCompanyTextField.text {
        delegate?.addEntrants(name: name, company: company)
    }
}
}

我的 VC 与 tableView:

import UIKit

protocol CellDelegate {
func addEntrants(name: String, company: String)
}

class EntrySec: UIViewController, CellDelegate {

@IBOutlet weak var spaceStatus: UIButton!
@IBOutlet weak var entrantsTable: UITableView!

var entryGrid: [Entrant] = [
    Entrant(name: "", company: ""),
    Entrant(name: "Bob Lob", company: "Blue"),
    Entrant(name: "John Lon", company: "Red")
]

override func viewDidLoad() {
    super.viewDidLoad()
    entrantsTable.dataSource = self
    entrantsTable.register(UINib(nibName: Global.headerCellNibName, bundle: nil), forCellReuseIdentifier: Global.headerCellIdentifier)
    entrantsTable.register(UINib(nibName: Global.bodyCellNibName, bundle: nil), forCellReuseIdentifier: Global.bodyCellIdentifier)
    
    if Global.uiMode == true {
        overrideUserInterfaceStyle = .light
    } else {
        overrideUserInterfaceStyle = .dark
    }
}

func addEntrants(name: String, company: String) {
    entryGrid.append(Entrant(name: name, company: company)) // This does not work
    print("\(name)  \(company)")                            // This does not work
}
    
@IBAction func pauseSession(_ sender: UIButton) {
}
@IBAction func closeSession(_ sender: UIButton) {
}
@IBAction func menu(_ sender: UIButton) {
}
}

extension EntrySession: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return entryGrid.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.row == 0 {
        let addEntrantCell = entrantsTable.dequeueReusableCell(withIdentifier: Global.headerCellIdentifier, for: indexPath) as! AddEntrantCell
        addEntrantCell.entrantNameTextField.text = entryGrid[indexPath.row].name
        addEntrantCell.entrantCompanyTextField.text = entryGrid[indexPath.row].company
        return addEntrantCell
    } else {
        let entrantCell = entrantsTable.dequeueReusableCell(withIdentifier: Global.bodyCellIdentifier, for: indexPath) as! EntrantCell
        entrantCell.textLabel?.text = entryGrid[indexPath.row].name
        if entrantCell.textLabel?.text == "" {
            entrantCell.backgroundColor = #colorLiteral(red: 0.9764705882, green: 0.4274509804, blue: 0, alpha: 1)
            entrantCell.alpha = 0.5
        }
        return entrantCell
    }
}
}

您尚未在 cellForRowAt 中设置单元格委托。所以你必须在 cellForRowAt 方法中设置 CellDelegate 。像这样。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.row == 0 {
        let addEntrantCell = entrantsTable.dequeueReusableCell(withIdentifier: Global.headerCellIdentifier, for: indexPath) as! AddEntrantCell
        addEntrantCell.delegate = self // <--- Add delegate here
        addEntrantCell.entrantNameTextField.text = entryGrid[indexPath.row].name
        addEntrantCell.entrantCompanyTextField.text = entryGrid[indexPath.row].company
        return addEntrantCell
    } else {
        let entrantCell = entrantsTable.dequeueReusableCell(withIdentifier: Global.bodyCellIdentifier, for: indexPath) as! EntrantCell
        entrantCell.textLabel?.text = entryGrid[indexPath.row].name
        if entrantCell.textLabel?.text == "" {
            entrantCell.backgroundColor = #colorLiteral(red: 0.9764705882, green: 0.4274509804, blue: 0, alpha: 1)
            entrantCell.alpha = 0.5
        }
        return entrantCell
    }
}

您也可以使用闭包代替委托。 (您已经添加了代码但需要更正)

用这个更新您的单元格 class 并删除其他闭包。

var entrants: ((String, String) -> Void)?

@IBAction func addEntrantButton(_ sender: UIButton) {
    if let name = entrantNameTextField.text, let company = entrantCompanyTextField.text {
        entrants?(name, company)
    }
}

并在 cellForRow 方法内部,而不是设置委托添加此


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.row == 0 {
        let addEntrantCell = entrantsTable.dequeueReusableCell(withIdentifier: Global.headerCellIdentifier, for: indexPath) as! AddEntrantCell
        addEntrantCell.entrants = { name, company in
            entryGrid.append(Entrant(name: name, company: company))
        } // <--- Here
    
// Rest of the code here