我是编程新手,刚刚开始深入研究SWIFT和iOS开发。
我现在已经坚持了几个小时的问题,如果有人可以回答它,我将非常感激。分配是桶列表,我们必须构建一个基本应用程序,它使用两个表视图(一个动态和一个静态)来显示使用导航控制器和模态显示的“桶列表”项列表。在我们的第一个任务中,我们被允许使用两个segue进行编辑和保存功能,但在新的任务中,我们被要求重构并使用一个segue,其中提示我们应该使用发送者信息。本代码使用两个segue,因为我还没有想出如何重构。作为注释,我们使用附件详细信息披露来调出列表项以进行编辑。这是我陷入困境的地方。从我能够阅读的内容来看,我理解Disclosure Detail按钮不是“真正的按钮”,而是它是索引路径的一部分。虽然我认为我理解从概念层面需要做什么,但我真的有一段时间来解决这个问题。提前致谢。
import UIKit
class BucketListViewController: UITableViewController,
AddItemTableViewControllerDelegate {
var items = ["awesome","lessawesome","great"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath)
cell.textLabel?.text = items[indexPath.row]
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("selected")
}
override func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
performSegue(withIdentifier: "EditItemSegue", sender: indexPath)
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
items.remove(at: indexPath.row)
tableView.reloadData()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "AddItemSegue"{
let navigationController = segue.destination as! UINavigationController
let addItemTableViewController = navigationController.topViewController as! AddItemTableViewController
addItemTableViewController.delegate = self
}else if segue.identifier == "EditItemSegue"{
let navigationController = segue.destination as! UINavigationController
let AddItemTableViewController = navigationController.topViewController as! AddItemTableViewController
AddItemTableViewController.delegate = self
let indexPath = sender as! NSIndexPath
let item = items[indexPath.row]
AddItemTableViewController.item = item
AddItemTableViewController.indexPath = indexPath
}
}
func cancelButtonPressed(by controller: AddItemTableViewController) {
dismiss(animated: true, completion: nil)
}
func itemSaved(by controller: AddItemTableViewController, with text:String, at indexPath: NSIndexPath?) {
if let ip = indexPath {
items[ip.row] = text
}else{
items.append(text)
}
tableView.reloadData()
dismiss(animated: true, completion: nil)
}
}
AddItemTableViewControllerDelegate
import Foundation
import UIKit
protocol AddItemTableViewControllerDelegate: class {
func itemSaved(by controller: AddItemTableViewController, with text:String, at indexPath: NSIndexPath?)
func cancelButtonPressed(by controller: AddItemTableViewController)
}
AddItemTableViewController
import UIKit
class AddItemTableViewController: UITableViewController {
weak var delegate: AddItemTableViewControllerDelegate?
var item: String?
var indexPath: NSIndexPath?
@IBOutlet weak var itemTextField: UITextField!
@IBAction func cancelButtonPressed(_ sender: UIBarButtonItem) {
delegate?.cancelButtonPressed(by: self)
}
@IBAction func saveButtonPressed(_ sender: UIBarButtonItem) {
let text = itemTextField.text!
delegate?.itemSaved(by: self, with: text, at: indexPath)
}
override func viewDidLoad() {
super.viewDidLoad()
itemTextField.text = item
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}