gpt4 book ai didi

ios - 从 tableView 上的核心数据填充数据时如何避免行重复

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

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

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)
}

最佳答案

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

关于ios - 从 tableView 上的核心数据填充数据时如何避免行重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40339100/

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