![](/img/trans.png)
[英]How can I display a specific text related to a specific date using date picker?
[英]How can I save and display date picker value in detail text label using core data?
我有一个使用核心数据的简单提醒应用程序,其中包含2个实体,一个用于提醒的“详细信息”,另一个用于提醒的“日期”。 用户添加一个提醒并选择一个日期,然后将其存储在所有提醒的表视图中。
我已经将'details'实体值正确地从文本字段保存到了表格视图的标签中,但是我似乎无法从日期选择器和表格视图的明细文本标签中获取要保存的日期。
在EnterReminderViewController.m中:
-(void)insertReminderDetails {
CoreDataStack *coreDataStack = [CoreDataStack defaultStack];
ReminderEntry *entry = [NSEntityDescription insertNewObjectForEntityForName:@"ReminderEntry" inManagedObjectContext:coreDataStack.managedObjectContext];
entry.details = self.reminderTextField.text;
entry.date = self.datePicker;
[coreDataStack saveContext];
}
在UpcomingRemindersTableViewController.m中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
ReminderEntry *entry = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = entry.details;
cell.detailTextLabel.text = entry.date;
return cell;
}
我需要将值转换为字符串才能工作吗? 另外,我是否需要为该日期创建一个单独的提取请求?
在您的第一个代码段中, entry.date
属性似乎是Core Data的“ date”属性,等效于代码中的NSDate
。 您正在尝试为其分配一个UIDatePicker
。 这是不正确的,因为UIDatePicker
是一个UI控件,允许用户选择日期,它本身不是日期。 您需要向日期选择器询问其当前日期值-可能在代码中为self.datePicker.date
。
在第二个代码段中,您似乎正在将entry.date
分配给UILabel
文本属性。 这也是不兼容的,因为日期不是字符串。 一个简单的解决方法是使用entry.date.description
,但这将使用GMT而不是本地时间。 要获得更多将日期转换为字符串的控制权,请查看NSDateFormatter
。
您可以将属性值设置为要转换的日期,然后将日期选择器日期保存为init。我们必须将日期选择器日期作为NS Date而不是核心日期中的字符串。希望它将对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.