从表视图保存数据 [英] Saving data from Table View

查看:31
本文介绍了从表视图保存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在核心数据中设置了实体,我想在其中保存用户选择的日期和日期选择器中的时间:

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 :

例如

我想保存周一、周三、周五和 22:07.我可以将日期选择器连接到视图控制器以获取时间.但是,如何保存选择的日期?

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 ?

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

返回零

尝试 2

print("You selected \(indexPath.row.description)")

成功返回单元格的行号,但不返回 UIlabel 描述.

Successfully return the row number of the cell but not the UIlabel description.

更多信息

星期几存储为 UILabel 如下:

The days of the week are stored as UILabel as below :

尝试 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 在动态和静态单元格中的工作方式类似.

tableView:didSelectRowAt works in dynamic and static cells similarly.

使用方法indexPathsForSelectedRows(复数)获取所有当前选择的索引路径,然后使用数组获取工作日而不是从单元格中收集信息.

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.

代码将第 0 节的索引路径映射到它们的行,并将这些行映射到相应的工作日.

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

如果您还想捕获取消选择单元格,请使用 tableView:didDeselectRowAt 中的相同代码,例如

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

考虑在 Core Data 中保存工作日的索引而不是字符串值.然后就可以直接使用行了.

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

这篇关于从表视图保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆