gpt4 book ai didi

ios - Swift - 保存核心数据后更新 Tableview

转载 作者:行者123 更新时间:2023-11-29 00:17:10 27 4
gpt4 key购买 nike

我有一个 mainVC,我在其中执行获取请求。然后我将获取的数据传递给第二个 View Controller (HomeVc),然后传递给第三个 (MyparamsVc)在第三个 VC 中,我有一个 tableView,其中列出了与我在主 VC 中获取的实体具有一对一关系的所有参数

在第四个 View Controller 中,我通过表单 (addParamVC) 添加了新参数。保存后我关闭 Controller 并弹出前一个(MyparamsVc)。我的问题是 tableview 没有更新新数据,除非我回到我的 HomeVC,然后再次进入 MyparamsVC

我已经实现了 func controllerDidChange 一个对象,但是因为我没有在同一个 Controller 中执行提取,所以该函数永远不会被调用...我如何用新数据更新 TableView ?

MainVc 获取请求:

 var controller: NSFetchedResultsController<SwimminPool>!

override func viewDidLoad() {
super.viewDidLoad()

tableView.delegate = self
tableView.dataSource = self

attemptFetch()

}
func attemptFetch() {

let fetchRequest: NSFetchRequest<SwimminPool> = SwimminPool.fetchRequest()
let dataSort = NSSortDescriptor(key: "poolCreatedAt", ascending: false)
fetchRequest.sortDescriptors = [dataSort]


let controller = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)

controller.delegate = self
self.controller = controller

do {
try controller.performFetch()
} catch {
let error = error as NSError
print("\(error.debugDescription)")
}

}

addParamVC 保存函数:

var swimmingPoolToConnect: SwimmingPool!
var parameterToEdit: Parameter?

@IBAction func saveBtnPressed(_ sender: Any) {

var parameter: Parameter

if parameterToEdit == nil {
parameter = Parameter(context:context)
} else {
parameter = parameterToEdit!
}

if let pH = phTextField.text?.characters.split(separator: ",").joined(separator: ".") {
if let pHnoComma = Double(String(pH)) {
parameter.paramPH = pHnoComma
}
}
parameter.createdAt = Date()

swimmingPoolToConnect.addToParameters(parameter)

ad.saveContext()
self.presentingViewController?.dismiss(animated: true, completion: nil)
}

我的参数VC:

 var parameters = [Parameter]()
var swimmingPool: SwimmingPool! {
didSet {
parameters = (swimmingPool.parameters?.allObjects as! [Parameter]).sorted(by: {$0.createdAt! > $1.createdAt!})
}
}

var controller: NSFetchedResultsController<Parameter>?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ParamCell", for: indexPath) as! ParamCell
configureCell(cell: cell, indexPath: indexPath)
return cell
}

func configureCell(cell: ParamCell, indexPath: IndexPath) {

let param = parameters[indexPath.row]
cell.configureCell(parameter: param)
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

let parameterToPass = parameters[indexPath.row]
DispatchQueue.main.async { () -> Void in
self.performSegue(withIdentifier: "MyParameterDetail", sender: parameterToPass)
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return parameters.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

//Never enter these func

func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
tableView.beginUpdates()
}

func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
tableView.endUpdates()
}

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {

switch(type){
case.insert:
if let indexPath = newIndexPath{
tableView.insertRows(at: [indexPath], with: .fade)

}

case.delete:
if let indexPath = indexPath{
tableView.deleteRows(at: [indexPath], with: .fade)
}

case.update:
if let indexPath = indexPath{
let cell = tableView.cellForRow(at: indexPath) as! ParamCell
configureCell(cell: cell, indexPath: indexPath)
}

case.move:
if let indexPath = indexPath{
tableView.deleteRows(at: [indexPath], with: .fade)
}
if let indexPath = newIndexPath{
tableView.insertRows(at: [indexPath], with: .fade)
}
}
}

最佳答案

由于您引用了呈现 View Controller ,因此将该项目添加到 parameters 数组中,并在 addParamVC 中的 MyparamsVC 中插入一行 addParamVC

ad.saveContext()
let paramsVC = self.presentingViewController as! MyparamsVC
let lastRow = paramsVC.parameters.count
paramsVC.parameters.append(parameter)
paramsVC.tableView.insertRows(at: [NSIndexPath(row: lastRow, section: 0)], with: .none)
paramsVC.dismiss(animated: true, completion: nil)

或者甚至在 dismiss 方法的完成处理程序中

ad.saveContext()
let paramsVC = self.presentingViewController as! MyparamsVC

paramsVC.dismiss(animated: true) {
let lastRow = paramsVC.parameters.count
paramsVC.parameters.append(parameter)
paramsVC.tableView.insertRows(at: [NSIndexPath(row: lastRow, section: 0)], with: .none)
}

关于ios - Swift - 保存核心数据后更新 Tableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45078960/

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