【发布时间】:2017-07-04 13:19:48
【问题描述】:
目标:
能够在表格视图的同一行中显示用户选择的日期和时间。时间应显示在顶部,所选日期应显示在底部,两者都在同一行中,就像闹钟一样。
工作:
这就是我如何保存从 UITable 中选择的日期以及在点击保存按钮时从 UIDatepicker 中选择的时间:
@IBAction func saveButnTapped(_ sender: AnyObject)
{
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext //creates an object of a property in AppDelegate.swift so we can access it
let bob = Bob(context: context)
//save the time from UIDatePicker to core data
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
bob.timeToPing = dateFormatter.string(from: timePicked.date)
// save the days selected to core data
for weekday in filteredWeekdays
{
var day = Days(context: context) //create new Days object
day.daysSelected = weekday as NSObject? //append selected weekday
bob.addToTimeAndDaysLink(day) //for every loop add day object to bob object
}
//Save the data to core data
(UIApplication.shared.delegate as! AppDelegate).saveContext()
//after saving data, show the first view controller
navigationController!.popViewController(animated: true)
}
现在数据已经保存,我得到了数据:
func getData()
{
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
do
{
bobs = try context.fetch(Bob.fetchRequest())
}
catch
{
print("Fetching failed")
}
}
尝试选择日期:
我尝试关注this、以下 cmets 和以前删除的对此问题的回答:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = UITableViewCell()
let bob = bobs[indexPath.row]
cell.textLabel?.text = bob.timeToPing?.description
// retrieve the days that are selected
var daysArray: [Days] = []
daysArray = bob.timeAndDaysLink?.allObjects as! [Days]
for days in daysArray
{
print (days.daysSelected?.description)
cell.textLabel?.text = days.daysSelected! as! String
}
return cell
}
编辑:
print(daysArray) 给出了这个:
[<Days: 0x6080000a5880> (entity: Days; id: 0xd000000000040000 <x-coredata://30B28771-0569-41D3-8BFB-D2E07A261BF4/Days/p1> ; data: <fault>)]
print(daysArray[0]) 给出了这个:
<Days: 0x6080000a5880> (entity: Days; id: 0xd000000000040000 <x-coredata://30B28771-0569-41D3-8BFB-D2E07A261BF4/Days/p1> ; data: <fault>)
如何节省时间
let weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
var filteredWeekdays: [String] = []
@NSManaged public var daysSelectedbyUser: NSSet
然后
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
selectedWeekdays()
}
override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath)
{
selectedWeekdays()
}
func selectedWeekdays()
{
if let selectedRows = tableView.indexPathsForSelectedRows
{
let rows = selectedRows.filter {$0.section == 0}.map{ $0.row}
filteredWeekdays = rows.map{ weekdays[$0] }
print(filteredWeekdays)
}
}
非常感谢!
【问题讨论】:
-
什么错误?而且您发布的代码不会出现在您的代码中。我猜想用
bob替换salawaat?bobs中的对象在哪里?bobs = try context.fetch(Bob.fetchRequest())是什么? -
嗨@Larme,我刚刚更新了代码。抱歉,打错字了。
-
您能否显示
bob的值以及您收到的确切错误或警告消息? -
嗨,我如何向你展示 bob 的价值?我已经更新了原始问题以添加确切的错误。我无法将它粘贴在这里,因为它对于 cmets 来说太长了哈哈。
-
bob.timeAndDaysLink那是Days对象的 NSSet,而不是 String 对象的NSSet,这就是为什么你有演员表问题。daysArray = bob.timeAndDaysLink?.allObjects.valueFor(key:"daysSelected")而不是?