Saving data from Table View

2019-08-28 05:34发布

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 :

For e.g. enter image description here

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 : enter image description here

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.

标签: ios swift3
2条回答
仙女界的扛把子
2楼-- · 2019-08-28 06:05

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.

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
     let weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
     if let selectedRows = tableView.indexPathsForSelectedRows {
         let rows = selectedRows.filter {$0.section == 0}.map{ $0.row}
         let filteredWeekdays = rows.map{ weekdays[$0] }
         print(filteredWeekdays)
     }
}

If you want to catch also deselecting a cell use the same code in tableView:didDeselectRowAt for example

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    selectedWeekdays()
}

override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    selectedWeekdays()
}

func selectedWeekdays()
{
    let weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
    if let selectedRows = tableView.indexPathsForSelectedRows {
        let rows = selectedRows.filter {$0.section == 0}.map{ $0.row}
        let filteredWeekdays = rows.map{ weekdays[$0] }
        print(filteredWeekdays)
    }
}

Consider to save the indices of the weekdays in Core Data rather than the string values. Then you can use the rows directly.

查看更多
Animai°情兽
3楼-- · 2019-08-28 06:27

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.

import UIKit

class NewTableViewController: UITableViewController {
    @IBOutlet weak var monday: UILabel!
    @IBOutlet weak var tuesday: UILabel!
    @IBOutlet weak var wednesay: UILabel!

    var selectedDays: [String] = []

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

       override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let identifier = tableView.cellForRow(at: indexPath)?.reuseIdentifier as! String
            print(identifier)
            switch identifier {
            case "Monday":
                selectedDays.append(identifier)
            case "Tuesday":
                 selectedDays.append(identifier)
            case "Wednesday":
                selectedDays.append(identifier)
            default:
                return
            }
        print(selectedDays.count)
    }

    override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        let identifier = tableView.cellForRow(at: indexPath)?.reuseIdentifier as! String
        print(identifier)
        switch identifier {
        case "Monday":
            selectedDays.remove(at: indexPath.row)
        case "Tuesday":
            selectedDays.remove(at: indexPath.row)
        case "Wednesday":
            selectedDays.remove(at: indexPath.row)
        default:
            return
        }
        print(selectedDays.count)
    }
}
查看更多
登录 后发表回答