gpt4 book ai didi

ios - 如何使用委托(delegate)和协议(protocol)快速编辑和传回单元格数据

转载 作者:搜寻专家 更新时间:2023-11-01 06:55:07 25 4
gpt4 key购买 nike

我有两个 VC,第一个是 tableView,第二个是 detailedView VC,您可以在其中向 tableView 添加新项目。我已经实现了使用 segues 向前传递数据(从第一个 VC 中的加号按钮),并在向 tableView 添加新项目时使用委托(delegate)和协议(protocol)向后传递数据(在第二个 VC 上点击保存按钮时触发)。

我从原型(prototype)单元添加了一个 segue 到第二个 VC(详细 View ),我还设法在第一个 VC 中测试了哪个 segue 被触发,即:添加新项目或转到该项目的 detailedView。我面临的问题是,第二个 VC 中的保存按钮不再起作用(取消按钮也不起作用),我希望能够在第二个 VC 中编辑文本字段并点击保存按钮将编辑的项目保存回来在第一个。我找到了一种使用 unwind segues 来实现它的方法,但是我想知道如何使用 delegate 来实现它?

我的第一个 VC 代码:

class ThingsTableViewController: UITableViewController, CanReceive {

var myThings = [Thing]()

override func viewDidLoad() {
super.viewDidLoad()

}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

cell.textLabel?.text = myThings[indexPath.row].name
cell.detailTextLabel?.text = myThings[indexPath.row].type

return cell
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

if segue.identifier == "addNewThing" {

let secondVC = segue.destination as! UINavigationController

let ThingsViewController = secondVC.topViewController as! ThingsViewController

ThingsViewController.delegate = self

} else if segue.identifier == "showDetail" {

guard let thingDetailViewController = segue.destination as? ThingsViewController else {fatalError("Unknown Destination")}

guard let selectedCell = sender as? UITableViewCell else {
fatalError("Unexpected sender: \(sender)")
}

guard let indexPath = tableView.indexPath(for: selectedCell) else {
fatalError("The selected cell is not being displayed by the table")
}

let selectedThing = myThings[indexPath.row]
thingDetailViewController.thing = selectedThing

}
}

func dataReceived(data: Thing) {

if let selectedIndexPath = tableView.indexPathForSelectedRow {

myThings[selectedIndexPath.row] = data
tableView.reloadRows(at: [selectedIndexPath], with: .none)

} else {

myThings.append(data)
tableView.reloadData()



}

}

第二个 vc 中的代码如下:

 protocol CanReceive {

func dataReceived(data: Thing)

}

}

class ThingsViewController: UIViewController, UITextFieldDelegate {

var delegate : CanReceive?
var thing : Thing?

@IBOutlet weak var thingNameTextField: UITextField!

@IBOutlet weak var thingTypeTextfield: UITextField!

@IBAction func saveThingButton(_ sender: UIBarButtonItem) {
let newThing = Thing(name: thingNameTextField.text!, type: thingTypeTextfield.text!)

delegate?.dataReceived(data: newThing)

self.dismiss(animated: true, completion: nil)
self.navigationController?.popViewController(animated: true)

}

@IBAction func cancelButton(_ sender: UIBarButtonItem) {
self.dismiss(animated: true, completion: nil)
self.navigationController?.popViewController(animated: true)
}

override func viewDidLoad() {
super.viewDidLoad()

thingNameTextField.delegate = self

updateSaveButtonState()

if let thing = thing {
navigationItem.title = thing.name
thingNameTextField.text = thing.name
thingTypeTextfield.text = thing.type

}
}

// MARK: UITextField Delegate

// get triggered when the user hit the return key on the keyboard
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
thingNameTextField.resignFirstResponder()
self.navigationItem.rightBarButtonItem?.isEnabled = true
return true
}

//gives chance to read info in text field and do something with it
func textFieldDidEndEditing(_ textField: UITextField) {
updateSaveButtonState()
navigationItem.title = thingNameTextField.text
}

func updateSaveButtonState() {

let text = thingNameTextField.text
self.navigationItem.rightBarButtonItem?.isEnabled = !text!.isEmpty
}

}

最佳答案

您正在为 segue 的标识符为 addNewThing 的情况设置 delegate,但是标识符为 showDetail 的情况呢?

如果segue的标识符是showDetail,则设置segue目的地的delegate

if segue.identifier == "addNewThing" {
...
} else if segue.identifier == "showDetail" {
...
thingDetailViewController.delegate = self
...
}

然后当你需要关闭导航 Controller 中嵌入的 ViewController 时,只需关闭它然后关闭导航 Controller

关于ios - 如何使用委托(delegate)和协议(protocol)快速编辑和传回单元格数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53976858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com