Reputation: 5
I'm working with Core Data. I have a Core Data entity named Users. I'm listing Users in a UITableView. Multi-selection is enabled for UITableView. UITableViewCell subclass includes a UILable for username and UITextField for price. Also, I have a Core Data entity named Expenses.
The thing I'm trying to do is: I will enter prices for users, and will save to Expenses with the info of the user and price. For example:
I have 10 users listed in UITableView. (DONE)
All cells have UITextField for price. (DONE)
I enter prices in UITextField for 5 users which I selected. (DONE)
Get the info which includes userUID and price from selected cells. (FAIL)
Save userUID and price to Expenses with doneButton.
Here's what my Core Data entities look like:
Users:
@NSManaged public var userName: String
@NSManaged public var userUID: String
Expenses:
@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!
}
The thing I want to do is, get userUID
and price from UITextField from selected cells.
Upvotes: 0
Views: 65
Reputation: 41
From my point, you need something like one more layer for the cell data
class CellViewModel {
let user: User
var price: Int/Double/String
}
After you fetch your Users you create the list of CellViewModel and keep it. And your CustomCell
works with it (sets name and etc).
The cell should be a UITextFieldDelefate
and change price
in CellViewModel
.
After you did all your changes you take a list of selected cells from indexPathsForSelectedRows
. Now you have indexes of selected cells and list of your CellViewModel
. Now you iterate through them and create your Expenses
Check map
and filter
methods for the list to make your solution more readable.
Good luck
Upvotes: 0
Reputation: 320
The thing is you shouldn't use UITableViewCell as data storage because TableView can release them from memory if they are not present on the screen.
One option has data storage in ViewController and set a reference to it to cells. UITextField in this cell than set value directly into this data storage.
So with a done button, you can just read values from data storage.
Upvotes: 1