我有这个tableviewcontroller和detailviewcontroller,它有一个文本字段,当用户点击tableviewcell时,它会转到文本字段,用户可以编辑内容!!
如何通过后退按钮在文本字段中使用新内容更新表格视图?
let sectionTitles = ["ongoing","Done"]
var list = [String]()
class MasterViewController: UITableViewController {
var detailViewController: DetailViewController? = nil
var objects = sectionTitles.map({_ in return [Any]()})
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
navigationItem.leftBarButtonItem = editButtonItem
let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(insertNewObject(_:)))
navigationItem.rightBarButtonItem = addButton
if let split = splitViewController {
let controllers = split.viewControllers
detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController
}
}
@objc
func insertNewObject(_ sender: Any) {
objects[0].insert("Task 1", at: 0)
let indexPath = IndexPath(row: 0, section: 0)
tableView.insertRows(at: [indexPath], with: .automatic)
}
// MARK: - Segues
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
if let indexPath = tableView.indexPathForSelectedRow {
let object = objects[indexPath.section][indexPath.row] as! String
let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController
controller.detailItem = object
controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
}
}
class DetailViewController: UIViewController,UINavigationControllerDelegate {
@IBOutlet weak var detailDescription: UITextField!
func configureView() {
// Update the user interface for the detail item.
if let detail = detailItem {
if let label = detailDescription {
label.text = detail.description
list.append(label.text!)
}
}
}
}
答案 0 :(得分:1)
我建议使用回调闭包来更新模型并重新加载行。回调在module Core
require "initial_create_helper"
describe Member do
describe "automerge" do
before(:each) do
@organization = create(:organization)
@department1 = create(:organization_department,organization: @organization)
@department2 = create(:organization_department,organization: @organization)
@user = create(:user)
@user_with_many_employments = create(:user)
@department1.employments.create!(user: @user)
@department1.employments.create!(organization: @organization, user: @user_with_many_employments)
@department2.employments.create!(organization: @organization, user: @user_with_many_employments)
@project = create_project
@project.members.create!(user: @user,
organization: @organization)
@project.members.create!(user: @user_with_many_employments,
organization: @organization)
end
it "::can_not_automerged" do
expect(Member.can_not_automerged(@department1).to_a.map(&:user)).to match_array [@user_with_many_employments]
end
it "::can_automerged" do
expect(Member.can_automerged(@department1).to_a.map(&:user)).to match_array [@user]
end
end
end
end
中调用。闭包中的代码检查值是否不同并更新模型并在必要时重新加载行。
在DetailViewController中创建一个回调
viewDidDisappear
在var callback : ((String)->())?
调用回调(或者可能已在viewDidDisappear
中)
viewWillDisappear
在MasterViewController中添加override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
callback?(detailDescription.text!)
}
prepare(for
答案 1 :(得分:0)
您可以使用委托模式:
protocol DetailViewDelegate: class {
func detailViewDidUpdate(from: String?, to: String?)
}
class DetailViewController : UIViewController {
var detailItem: String?
weak var delegate: DetailViewDelegate?
@IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
self.textField.text = detailItem
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.delegate?.detailViewDidUpdate(from: detailItem, to: textField.text)
}
}
然后在 MasterViewController :
中extension MasterViewController: DetailViewDelegate {
func detailViewDidUpdate(from: String?, to: String?) {
guard let originalValue = from, let newValue = to else {
return
}
if let indexPath = self.objects.index(of: originalValue) {
self.objects[indexPath] = newValue
// We assume here that there is only one section
let newIndexPath = IndexPath(row: indexPath, section: 0)
self.tableView.reloadRows(at: [newIndexPath], with: .automatic)
}
}
}
不要忘记在MasterController中配置DetailViewController的委托变量 prepare(对于segue:UIStoryboardSegue,sender:Any?)方法
答案 2 :(得分:0)
当您点按后退按钮时,将会调用 viewWillAppear ,其中一个View Controller Lifecycle
例如
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tableView.reloadData()
}