【问题标题】:Trying to use delegate to share text input from custom tableViewCell and Data is not sharing尝试使用委托来共享来自自定义 tableViewCell 的文本输入并且数据未共享
【发布时间】:2021-01-02 06:51:18
【问题描述】:

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

按钮运行良好,如果 let 运行良好但数据没有像我希望的那样从自定义单元按钮按下传输到 ViewController 数组...

我对编码还是很陌生,所以我知道我做错了什么很明显。我从 SO 应用以下内容:

Getting Textfield Values from Custom Table View Cell

How to get user input from UItextfield and add it to an array?

我的自定义单元格:

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
    }
}
}

【问题讨论】:

    标签: ios swift xcode delegates custom-cell


    【解决方案1】:

    您尚未在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
        }
    }
    

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

    用这个更新你的单元类并删除其他闭包。

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

    在 cellForRow 方法中,而不是 set delegate 添加这个

    
    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
    
    

    【讨论】:

    • 解决了。谢谢拉贾。这些代表一开始很难理解
    • 查看我更新的答案,你也可以使用闭包。
    • 那就更简洁了。太棒了。
    猜你喜欢
    • 2015-11-15
    • 2010-11-18
    • 2019-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多