I have entities setup in core data where I would like to save the days that the user has selected and the time from the date picker :
I want to save Monday, Wednesday, Friday and 22:07. I can hook up the date picker to the viewcontroller to get the time. However how can I save the days that are selected ?
Attempt 1 :
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
let indexPath = tableView.indexPathForSelectedRow
let currentCell = tableView.cellForRow(at: indexPath!)! as UITableViewCell
print(currentCell.textLabel!.text)
}
return nil
Attempt 2
print("You selected \(indexPath.row.description)")
Successfully return the row number of the cell but not the UIlabel description.
More info
The days of the week are stored as UILabel as below :
Attempt 3 :
print(currentCell.contentView.description)
I also tried to add a navigation controller to add a save button to the top right and that doesn't seem to work either (the save button is going to the bottom left for some reason).
Would appreciate any help please! Thanks.
tableView:didSelectRowAt
works in dynamic and static cells similarly.Use the method
indexPathsForSelectedRows
(plural) to get all currently selected index paths, then use an array to get the weekdays rather than gathering the information from the cells.The code maps the index paths of section 0 to their rows and maps the rows to the corresponding weekdays.
If you want to catch also deselecting a cell use the same code in
tableView:didDeselectRowAt
for exampleConsider to save the indices of the weekdays in Core Data rather than the string values. Then you can use the rows directly.
I'm using Reuse Identifiers.. Monday, Tuesday, etc. I've also created an array to hold the selected values. Finally be sure to implement didDeselectRow, to remove values from the array.