我在swift 3.0的項目中工作,我有兩個UITableViews,我從一個名爲UserIncome的核心數據模塊實體設置數據。由於這些數據將填充到單個UIViewController中的兩個UItableViews中(基於ViewWillAppear委託方法中的字符串值進行過濾),一旦在一個UITableView中刪除了一行,其數組也會自動被其他tableView的對象更新。但是,一旦我點擊後退按鈕並回到相同的UIViewController似乎都很好。我的要求是更新UItableView,一旦行被刪除,作爲核心數據模塊。代碼如下。我在這裏錯過了什麼?一旦行被刪除,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()
}
}
後實現代碼如下代碼太,你寫在它的內容... –
正是在那裏爵士 – danu
嘗試調用viewDidAppear()不tableView.reloadData()FUNC的tableView內(_的tableView:UITableView的,提交editingStyle:UITableViewCellEditingStyle,forRowAt indexPath :IndexPath) – Rob