【发布时间】:2019-04-09 13:46:04
【问题描述】:
我正在使用 Core Data。我有一个名为 Users 的核心数据实体。我在 UITableView 中列出 Users。 UITableView 启用了多选。 UITableViewCell 子类包括 username 的 UILable 和 price 的 UITextField。另外,我有一个名为 Expenses 的核心数据实体。
我要做的是:我将为用户输入价格,并将用户信息和价格保存到费用。例如:
我在 UITableView 中列出了 10 个用户。 (完成)
所有单元格都有用于价格的 UITextField。 (完成)
我在 UITextField 中为我选择的 5 个用户输入价格。 (完成)
从选定的单元格中获取包含 userUID 和价格的信息。 (失败)
使用 doneButton 将 userUID 和 price 保存到 Expenses。
这是我的核心数据实体的样子:
用户:
@NSManaged public var userName: String
@NSManaged public var userUID: String
费用:
@NSManaged public var userUID: String
@NSManaged public var price: Int
class viewController : UIViewController, UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var fetchedResultsController :
NSFetchedResultsController<Users>!
override func viewDidLoad() {
super.viewDidLoad()
tableView.allowsMultipleSelection = true
}
override func viewWillAppear(_ animated: Bool) {
fetchUsers()
}
func fetchUsers() {
...... // FETCHING USERS FROM COREDATA
tableView.reloadData()
}
@IBAction func doneButton(_ sender: UIBarButtonItem) {
// ????????????
}
func numberOfSections(in tableView: UITableView) -> Int {
guard let fetchRes = fetchedResultsController , let sections
= fetchRes.sections else {
return 0
}
return sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
guard let fetchRes = fetchedResultsController , let
sectionInfo = fetchRes.sections?[section] else {
return 0
}
return sectionInfo.numberOfObjects
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:
"Cell", for: indexPath) as! CustomCell
let User = fetchedResultsController.object(at: indexPath)
cell.lblUserName.text = User.userName
return cell
}
}
class CustomCell : UITableViewCell {
@IBOutlet weak var lblUserName: UILabel!
@IBOutlet weak var txtPrice: UITextField!
}
我想做的是,从选定单元格的 UITextField 中获取 userUID 和价格。
【问题讨论】:
标签: swift uitableview core-data