在tableView

时间:2016-10-31 09:01:34

标签: ios objective-c uitableview core-data swift3

我正在开发一个用swift 3.0编写的项目。我的要求是检索并填充表格视图上的数据,该表格视图保存在我在某些文本字段中输入的CoreData上,因此一旦选择了一行,我想更新该记录(在我的文本字段上重新分配值并保存) 。 基本上我有一个名为" UserIncome"它的属性很少。因此,当我想编辑我输入的数据时,我点击一行,它会将我引导到我最初输入这些数据的ViewController。但是,当我单击添加按钮并保存数据时,我会获得重复的数据,并在我的表视图中填充它们。我怎么能阻止这个。我保存数据的类的代码如下。

import UIKit
import CoreData

class AddIncomeViewController:UIViewController,UITableViewDelegate,UITableViewDataSource {

@IBOutlet weak var toDateView: UIView!
@IBOutlet weak var setDateFromView: UIView!
@IBOutlet weak var datePickerTo: UIDatePicker!
@IBOutlet weak var datePickerFrom: UIDatePicker!
@IBOutlet weak var dropDowntableView: UITableView!
@IBOutlet weak var fromTextField: UITextField!
@IBOutlet weak var toTextField: UITextField!
@IBOutlet weak var amountTextField: UITextField!

@IBOutlet weak var incomeTypeLabel: UILabel!
@IBOutlet weak var incomeNameTextField: UITextField!

var myArray = ["Recurring Income", "Other Income"]

let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
var store : UserIncome?
var otherInc : UserIncome?

override func viewDidLoad() {
super.viewDidLoad()

 if let s = store {
    incomeNameTextField.text = s.incomeName
    amountTextField.text = s.amount
    fromTextField.text = s.fromDate
    toTextField.text = s.toDate
    incomeTypeLabel.text = s.incomeType

 }
 if let o = otherInc {
    incomeNameTextField.text = o.incomeName
    amountTextField.text = o.amount
    fromTextField.text = o.fromDate
    toTextField.text = o.toDate
    incomeTypeLabel.text = o.incomeType


  }


 }


 @IBAction func cancelButtonPressed(_ sender: AnyObject) {
 navigationController!.popViewController(animated: true)

 }


 @IBAction func addButtonPressed(_ sender: AnyObject) {

 if store == nil{

  let storeDesciption = NSEntityDescription.entity(forEntityName: "UserIncome", in: context)

    store = UserIncome(entity:storeDesciption! , insertInto: context)
    //otherInc = UserIncome(entity:storeDesciption! , insertInto: context)

 }
 if otherInc == nil{
    let storeDesciption = NSEntityDescription.entity(forEntityName: "UserIncome", in: context)

    otherInc = UserIncome(entity:storeDesciption! , insertInto: context)


 }


store?.incomeName = incomeNameTextField.text
store?.amount = amountTextField.text
store?.fromDate = fromTextField.text
store?.toDate = toTextField.text
store?.incomeType = incomeTypeLabel.text

otherInc?.incomeName = incomeNameTextField.text
otherInc?.amount = amountTextField.text
otherInc?.fromDate = fromTextField.text
otherInc?.toDate = toTextField.text
otherInc?.incomeType = incomeTypeLabel.text

var error : NSError?

(UIApplication.shared.delegate as! AppDelegate).saveContext()
 if let err = error{

    let a = UIAlertView(title: "Success", message: err.localizedFailureReason, delegate: nil, cancelButtonTitle: "OK")
    a.show()
 }else {
    let a =  UIAlertView(title: "Success", message: "Successfully saved", delegate: nil, cancelButtonTitle: "OK")
    a.show()
    //  navigationController!.popViewController(animated: true)
 }

1 个答案:

答案 0 :(得分:1)

您添加了两个不同的UserIncome个对象。您应该添加一个,并为您的表视图共享它。