【发布时间】:2020-10-09 23:13:02
【问题描述】:
我想从 AddViewController 更新我的 itemViewController 中的行名称。在我的代码中,我将包含名称和描述的项目对象传递给我的 AddViewController 并更改值,但是行名称永远不会更新。
我浏览了互联网并找到了有关执行此操作的所谓委托模式的信息。我只是不确定如何实现该模式。提前感谢您的帮助。
// ItemsViewController.swift
// Project 3-ToDo
//
// Created by Gracias Claude on 10/9/20.
//
import UIKit
class ItemsViewController : UITableViewController {
var itemStore: ItemStore!
@IBAction func addNewItem(_ sender: UIButton) {
// Create a new item and add it to the store
let newItem = itemStore.createItem()
// Figure out where that item is in the array
if let index = itemStore.allItems.firstIndex(of: newItem) {
let indexPath = IndexPath(row: index, section: 0)
// Insert this new row into the table
tableView.insertRows(at: [indexPath], with: .automatic)
}
}
@IBAction func toggleEditingMode(_ sender: UIButton) {
// If you are currently in editing mode...
if isEditing {
// Change text of button to inform user of state
sender.setTitle("Edit", for: .normal)
// Turn off editing mode
setEditing(false, animated: true)
} else {
// Change text of button to inform user of state
sender.setTitle("Done", for: .normal)
// Enter editing mode
setEditing(true, animated: true)
}
}
override func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return itemStore.allItems.count
}
// returning table view cell
override func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Get a new or recycled cell
let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell",
for: indexPath)
// Set the text on the cell with the description of the item
// that is at the nth index of items, where n = row this cell
// will appear in on the table view
let item = itemStore.allItems[indexPath.row]
cell.textLabel?.text = item.name
cell.detailTextLabel?.text = "\(item.dateCreated)"
return cell
}
override func tableView(_ tableView: UITableView,
commit editingStyle: UITableViewCell.EditingStyle,
forRowAt indexPath: IndexPath) {
// If the table view is asking to commit a delete command...
if editingStyle == .delete {
let item = itemStore.allItems[indexPath.row]
// Remove the item from the store
itemStore.removeItem(item)
// Also remove that row from the table view with an animation
tableView.deleteRows(at: [indexPath], with: .automatic)
}
}
override func tableView(_ tableView: UITableView,
moveRowAt sourceIndexPath: IndexPath,
to destinationIndexPath: IndexPath) {
// Update the model
itemStore.moveItem(from: sourceIndexPath.row, to: destinationIndexPath.row)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// If the triggered segue is the "showItem" segue
switch segue.identifier {
case "showItem":
print("Hello World")
// Figure out which row was just tapped
if let row = tableView.indexPathForSelectedRow?.row {
// Get the item associated with this row and pass it along
let item = itemStore.allItems[row]
let detailViewController
= segue.destination as! DetailsViewController
detailViewController.item = item
}
break;
case "addItem":
let row = tableView.indexPathForSelectedRow?.row ?? 0
// // Get the item associated with this row and pass it along
let item = itemStore.allItems[row]
let addViewController
= segue.destination as! AddViewController
addViewController.item = item
// }
break;
default:
preconditionFailure("Unexpected segue identifier.")
}
}
}
//AddViewController
//
// AddViewController.swift
// Project 3-ToDo
//
// Created by Gracias Claude on 10/9/20.
//
import UIKit
class AddViewController : UIViewController {
var item: Item!;
@IBOutlet weak var itemNameField: UITextField!
@IBOutlet weak var priorityField: UITextField!
@IBOutlet weak var descriptionField: UITextField!
@IBAction func submitItem(_ sender: UIButton) {
//how to update the existing Row name
item.name = itemNameField.text ?? "Default Text"
item.description = descriptionField.text ?? "default Description"
return;
}
}
【问题讨论】: