删除行后UITableView不会更新(UITableView is not updating once the row is deleted)

我正在使用swift 3.0中的一个项目,我有两个UITableViews,我在其中设置从名为UserIncome的核心数据模块实体获取的数据。 由于这些数据将在单个UIViewController中的两个UItableView中填充(基于ViewWillAppear委托方法中的String值进行过滤),一旦在一个UITableView中删除了一行,其数组也会自动由其他tableView的对象更新。 但是,一旦我单击后退按钮并回到相同的UIViewController,一切似乎都很好。 我的要求是在删除行后更新UItableView以作为核心数据模块。 代码如下。 我在这里想念的是什么?

import UIKit import CoreData class MyIncomesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var recurringIncomeTableView: UITableView! @IBOutlet weak var otherIncomeTableView: UITableView! //var myIncomeType : String? var stores = [UserIncome] () var other = [UserIncome] () let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext var rowTbl : Int! var rowTbl2 : Int! override func viewDidLoad() { super.viewDidLoad() } override func viewDidAppear(_ animated: Bool) { stores.removeAll() other.removeAll() let request = NSFetchRequest <NSFetchRequestResult> (entityName: "UserIncome") request.returnsObjectsAsFaults = false do { let results = try context.fetch(request) as! [UserIncome] print("Results from the fetch request are : ", request) // check data existance if results.count>0 { print("results are :", results.count) for resultGot in results { //lets check if the data is available and whether the loop is working by printing out the "name" if let incName = resultGot.incomeName { print("expence name is :", incName) //set the value to the global variable as to filter the arrays let myIncomeType = resultGot.incomeType if myIncomeType == "Recurring Income"{ stores += [resultGot] print("my recurring income array is : \(stores)") }else if myIncomeType == "Other Income"{ other += [resultGot] print("my other income array is : \(other)") } } } self.recurringIncomeTableView.reloadData() self.otherIncomeTableView.reloadData() } }catch{ print("No Data to load") } } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if tableView.tag == 1 { let cell: RecuringIncomeTableViewCell = tableView.dequeueReusableCell(withIdentifier: "recurringIncomeCell") as! RecuringIncomeTableViewCell let store = stores [indexPath.row] cell.incomeNameLabel.text = store.incomeName cell.amountLabel.text = store.amount return cell } else { let cell: OtherIncomeTableViewCell = tableView.dequeueReusableCell(withIdentifier: "otherIncomeCell") as! OtherIncomeTableViewCell let otherIncomes = other [indexPath.row] cell.incomeNameLabel.text = otherIncomes.incomeName cell.amountLabel.text = otherIncomes.amount return cell } } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { //performSegue(withIdentifier: "editStore", sender: nil) if tableView.tag == 1 { rowTbl = tableView.indexPathForSelectedRow?.row print("current row in tbl 1 is : ",rowTbl) }else { rowTbl2 = tableView.indexPathForSelectedRow?.row print("current row in tbl 2 is : ",rowTbl2) } } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "editRecurringIncome"{ let v = segue.destination as! AddIncomeViewController let indexPath = self.recurringIncomeTableView.indexPathForSelectedRow let row = indexPath?.row v.store = stores[row!] }else if segue.identifier == "editOtherIncome" { let t = segue.destination as! AddIncomeViewController let indexPath = self.otherIncomeTableView.indexPathForSelectedRow let row = indexPath?.row t.store = other [row!] } } // func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { print("delete delegate being activated") return true } //For remove row from tableview & object from array. func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext if tableView.tag == 1 { if editingStyle == .delete { let task = stores [indexPath.row] context.delete(task) (UIApplication.shared.delegate as! AppDelegate).saveContext() do { stores = try context.fetch(UserIncome.fetchRequest()) print("Stores deleted from indexPath",stores) }catch{ print("fail") } recurringIncomeTableView.reloadData() } self.recurringIncomeTableView.reloadData() } else if tableView.tag == 2 { if editingStyle == .delete { let task = other[indexPath.row] print("task on otherTblView is : ",task) context.delete(task) (UIApplication.shared.delegate as! AppDelegate).saveContext() otherIncomeTableView.reloadData() do { other = try context.fetch(UserIncome.fetchRequest()) print("Stores deleted from indexPath",other) }catch{ print("fail") } } self.otherIncomeTableView.reloadData() } tableView.reloadData() } }

Im working on a project in swift 3.0 and I have two UITableViews where I set data fetched from a core-data module entity called UserIncome. As these data will be populated in two UItableViews in a single UIViewController (filtering based on a String value in the ViewWillAppear delegate method),once a row is been deleted in one UITableView, its array automatically gets updated by the other tableView's objects too. But once I click the back button and come back to the same UIViewController all seems fine. My requirement is to update the UItableView once a row is been deleted so as the core data module. The code as bellow. What am I missing here?

import UIKit import CoreData class MyIncomesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var recurringIncomeTableView: UITableView! @IBOutlet weak var otherIncomeTableView: UITableView! //var myIncomeType : String? var stores = [UserIncome] () var other = [UserIncome] () let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext var rowTbl : Int! var rowTbl2 : Int! override func viewDidLoad() { super.viewDidLoad() } override func viewDidAppear(_ animated: Bool) { stores.removeAll() other.removeAll() let request = NSFetchRequest <NSFetchRequestResult> (entityName: "UserIncome") request.returnsObjectsAsFaults = false do { let results = try context.fetch(request) as! [UserIncome] print("Results from the fetch request are : ", request) // check data existance if results.count>0 { print("results are :", results.count) for resultGot in results { //lets check if the data is available and whether the loop is working by printing out the "name" if let incName = resultGot.incomeName { print("expence name is :", incName) //set the value to the global variable as to filter the arrays let myIncomeType = resultGot.incomeType if myIncomeType == "Recurring Income"{ stores += [resultGot] print("my recurring income array is : \(stores)") }else if myIncomeType == "Other Income"{ other += [resultGot] print("my other income array is : \(other)") } } } self.recurringIncomeTableView.reloadData() self.otherIncomeTableView.reloadData() } }catch{ print("No Data to load") } } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if tableView.tag == 1 { let cell: RecuringIncomeTableViewCell = tableView.dequeueReusableCell(withIdentifier: "recurringIncomeCell") as! RecuringIncomeTableViewCell let store = stores [indexPath.row] cell.incomeNameLabel.text = store.incomeName cell.amountLabel.text = store.amount return cell } else { let cell: OtherIncomeTableViewCell = tableView.dequeueReusableCell(withIdentifier: "otherIncomeCell") as! OtherIncomeTableViewCell let otherIncomes = other [indexPath.row] cell.incomeNameLabel.text = otherIncomes.incomeName cell.amountLabel.text = otherIncomes.amount return cell } } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { //performSegue(withIdentifier: "editStore", sender: nil) if tableView.tag == 1 { rowTbl = tableView.indexPathForSelectedRow?.row print("current row in tbl 1 is : ",rowTbl) }else { rowTbl2 = tableView.indexPathForSelectedRow?.row print("current row in tbl 2 is : ",rowTbl2) } } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "editRecurringIncome"{ let v = segue.destination as! AddIncomeViewController let indexPath = self.recurringIncomeTableView.indexPathForSelectedRow let row = indexPath?.row v.store = stores[row!] }else if segue.identifier == "editOtherIncome" { let t = segue.destination as! AddIncomeViewController let indexPath = self.otherIncomeTableView.indexPathForSelectedRow let row = indexPath?.row t.store = other [row!] } } // func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { print("delete delegate being activated") return true } //For remove row from tableview & object from array. func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext if tableView.tag == 1 { if editingStyle == .delete { let task = stores [indexPath.row] context.delete(task) (UIApplication.shared.delegate as! AppDelegate).saveContext() do { stores = try context.fetch(UserIncome.fetchRequest()) print("Stores deleted from indexPath",stores) }catch{ print("fail") } recurringIncomeTableView.reloadData() } self.recurringIncomeTableView.reloadData() } else if tableView.tag == 2 { if editingStyle == .delete { let task = other[indexPath.row] print("task on otherTblView is : ",task) context.delete(task) (UIApplication.shared.delegate as! AppDelegate).saveContext() otherIncomeTableView.reloadData() do { other = try context.fetch(UserIncome.fetchRequest()) print("Stores deleted from indexPath",other) }catch{ print("fail") } } self.otherIncomeTableView.reloadData() } tableView.reloadData() } }

最满意答案

你需要像这样删除任务

let task = stores [indexPath.row] context.delete(task) stores.removeAtIndex(indexPath.row) // i think you forget this line (UIApplication.shared.delegate as! AppDelegate).saveContext()

试试这个,希望它会对你有所帮助

you need to delete task like this way

let task = stores [indexPath.row] context.delete(task) stores.removeAtIndex(indexPath.row) // i think you forget this line (UIApplication.shared.delegate as! AppDelegate).saveContext()

try this,hope it will help you

更多推荐