![](/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.