激活滑动到删除时,从行中删除核心数据

时间:2016-10-25 07:34:56

标签: ios objective-c iphone swift swift3

我在swift 3.0中处理项目,并且我在表视图上填充数据,我从另一个视图控制器(来自两个文本字段)保存在核心数据实体上。我想从我的阵列和核心数据激活滑动到删除时删除数据。我在UITableView类上的代码如下所示。

import Foundation
import UIKit
import CoreData

class MyExpencesViewController: UIViewController,UITableViewDelegate, UITableViewDataSource {


@IBOutlet weak var expensesTableView: UITableView!

var myExpensesArray = [String] ()
var myAmountArray = [String] ()


override func viewDidLoad() {
    super.viewDidLoad()
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

   loadData()
   self.expensesTableView.reloadData()


}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return (myExpensesArray.count)
}

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

    cell.myExpenseName.text = myExpensesArray [indexPath.row]
    cell.myExpenseAmount.text = myAmountArray [indexPath.row]

 return cell 
}

func loadData (){
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let context = appDelegate.persistentContainer.viewContext

    let request = NSFetchRequest <NSFetchRequestResult> (entityName: "UserExpenses")
    request.returnsObjectsAsFaults = false

    do {

        let results = try context.fetch(request)


        // check data existance
        if results.count>0 {
            print(results.count)

            for resultGot in results as! [NSManagedObject]{

                if let expName = resultGot.value(forKey:"expenseName") as? String{

                    myExpensesArray += [expName]
                    DispatchQueue.main.async {
                        [unowned self] in self.expensesTableView.reloadData()
                        self.expensesTableView.reloadData()
                    }



                    print("myExp array is : \(myExpensesArray)")
                }
                if let amountVal = resultGot.value(forKey:"amount") as? String{

                    myAmountArray += [amountVal]
                    DispatchQueue.main.async {
                        [unowned self] in self.expensesTableView.reloadData()
                        self.expensesTableView.reloadData()
                    }



                    print("myAmount array is : \(myAmountArray)")
                }

            }





        }

    }catch{


        print("No Data to load in the array")
    }



  }


}

1 个答案:

答案 0 :(得分:0)

您必须在滑动

上添加另外两种删除行的方法
//For swipe access allow
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}

//For remove row from tableview & object from array.
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if (editingStyle == UITableViewCellEditingStyle.delete) {
        // delete data and row
        <YourArray>.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .fade)
    }
}

如果您需要更多详细信息,请与我们联系。