我正在尝试使用从更改UIDatepicker
收到的参数重新加载我的tableview。目前,我有以下方法捕获数据并将其发送到UIButton
(有效)。我正在尝试重置容纳UITableViewController的容器视图,该视图名为DisplayTableViewController
,但它没有重新加载NSFetchController
。有关如何调试或修复此问题的任何建议吗?
-(void)changeDate:(UIDatePicker *)sender {
//NSDate Formatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"ddMMyyyy"];
//Convert to string
NSString *stringFromDate = [dateFormatter stringFromDate:sender.date];
//Send to UIButton
[dateLabel setTitle:stringFromDate forState:UIControlStateNormal];
DisplayTableViewController *tbc = (DisplayTableViewController *)self.childViewControllers[0];
[tbc.tableView reloadData];
}
修改
if ([[segue identifier] isEqualToString:@"ShowDisplayResults"])
{
//Format Date for Seague
NSDateFormatter *todaysdate = [[NSDateFormatter alloc] init];
[todaysdate setDateFormat:@"ddMMyyyy"];
//Convert to string for Seague
NSString *stringFromDate = [todaysdate stringFromDate:[NSDate date]];
// Store the text we entered to dateSeague
dateSeague =stringFromDate;
// Get reference to the destination view controller
DisplayTableViewController *targetVC = [segue destinationViewController];
// Pass any objects to the view controller here, like...
targetVC.dateSeague = dateSeague;
targetVC.Ex = self.Ex;
}
容器中的NSFETCHCONTROLLER
- (NSFetchedResultsController *)fetchedResultsController {
NSLog(@"Test Date for second seague = %@", dateSeague);
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
//Query Entity
NSEntityDescription *entity = [NSEntityDescription entityForName:@"WorkoutDetails"inManagedObjectContext:managedObjectContext];
//Fetch Entity
[fetchRequest setEntity:entity];
//Sort Returned Values
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"weight" ascending:NO];
//Fetch Sorted Array
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
//Set Return Size
[fetchRequest setFetchBatchSize:20];
//Predicate Results Based on ID
NSPredicate *sortName = [NSPredicate predicateWithFormat:@"exid == %@", Ex.name];
//Get NSDate format from String
NSString *stringFromDate = dateSeague;
NSDateFormatter *df = [[NSDateFormatter alloc] init];
//Convert Back to NSDate
[df setDateFormat:@"ddMMyyyy"];
NSDate *inputedDate = [df dateFromString: stringFromDate];
//Set Predicate
NSPredicate *sortDate = [NSPredicate predicateWithFormat:@"date == %@", inputedDate];
//Compound Predicate Array
NSPredicate *placesPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:sortName, sortDate, nil]];
//Fetch Predicate Compound Array
[fetchRequest setPredicate:placesPredicate];
//Continue Fetching!
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext sectionNameKeyPath:nil
cacheName: nil];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
答案 0 :(得分:0)
首先,self.childViewControllers[0]
将为您提供视图控制器层次结构上的第一个视图控制器,并且只有当您有一个包含另一个视图控制器的视图控制器时才需要这个。
其次使用这种方法不是一个好习惯,因为当你调用它时你并不真正知道视图控制器的状态,并且该索引处的视图控制器可能与你预期的不同。
所以我建议你为你的视图控制器或视图实现一个委托来保存日期选择器,当日期发生变化并且你做了你想要对日期做的所有事情时,请调用委托。如果您不想对日期执行任何操作,只需将其传递给视图控制器,则可以将日期选择器delegate
设置为DisplayTableViewController
作为旁注,我不知道您现在拥有的层次结构,如果您没有找到我的答案,请在您的问题中添加更多详细信息(层次结构和您想要获得的流程)< / p>
答案 1 :(得分:0)
NSFetchRequest
的{{1}}的查询参数是否在变化?如果是这样,请确保在使用deleteCacheWithName时使用缓存,然后重新配置NSFetchController。