gpt4 book ai didi

ios - 如何使用后退按钮更新 tableviewcell?

转载 作者:行者123 更新时间:2023-11-28 14:48:05 25 4
gpt4 key购买 nike

我有这个 tableviewcontroller 和 detailviewcontroller,它有一个文本字段,当用户单击 tableviewcell 时,它会转到文本字段,用户可以编辑内容!!我应该如何通过后退按钮用文本字段中的新内容更新表格 View ?

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

最佳答案

我建议使用回调闭包来更新模型并重新加载行。回调在 viewDidDisappear 中调用。闭包中的代码检查值是否不同并更新模型并在必要时重新加载行。

  • 在 DetailViewController 中创建回调

    var callback : ((String)->())?
    • viewDidDisappear 中调用回调(或者可能已经在 viewWillDisappear 中)

      override func viewDidDisappear(_ animated: Bool) {
      super.viewDidDisappear(animated)
      callback?(detailDescription.text!)
      }
  • 在 MasterViewController 中添加闭包 prepare(for

    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.callback = { newValue in
    if object != newValue {
    self.objects[indexPath.section][indexPath.row] = newValue
    self.tableView.reloadRows(at: [indexPath], with: .automatic)
    }
    }
    controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
    controller.navigationItem.leftItemsSupplementBackButton = true
    }
    }
    }

关于ios - 如何使用后退按钮更新 tableviewcell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50086597/

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