[英]How to pass selected uitableviewcell core data object to a new view controller?
[英]Core Data: How to pass a managed object to a UITableViewCell and do a fetchRequest from there?
我有一個帶有自定義單元格的UITableView,該單元格包含另一個UItableView。 我需要將一個對象傳遞給它,然后基於該對象執行fetchrequest。
從我的cellForRowAtIndexPath方法,我將托管對象傳遞給自定義單元,如下所示:
TWHistoryViewStandardExpendedCell *cell = (TWHistoryViewStandardExpendedCell *)[tableView dequeueReusableCellWithIdentifier:ExpandedCell];
if (cell == nil) {
cell = (TWHistoryViewStandardExpendedCell *)[[[NSBundle mainBundle] loadNibNamed:@"HistoryViewStandardCellExpanded" owner:self options:nil] objectAtIndex:0];
}
Day *aDay = (Day *)[_fetchedResultsController objectAtIndexPath:indexPath];
[cell setViewingDay: aDay]; // NSLog here returns the expected object! :)
return cell;
這樣,我應該能夠從我的自定義單元格基於該對象執行fetchRequest。
在我的自定義UItableViewCell上,執行以下操作:
- (void) awakeFromNib
{
[self fetchRequest];
}
- (void)fetchRequest {
NSError *error;
if (![[self fetchedResultsController] performFetch:&error]) {
// Update to handle the error appropriately.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
}
有關我的fetchedResultsController的一些詳細信息:在注釋中,我的托管對象在此處返回null!
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController) {
return _fetchedResultsController;
}
NSManagedObjectContext * managedObjectContext =
[myAppDelegate managedObjectContext];
NSEntityDescription *entity =
[NSEntityDescription entityForName:@"Clock"
inManagedObjectContext:managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entity];
[fetchRequest setPredicate: [NSPredicate predicateWithFormat:@"day == %@", _viewingDay]];
NSLog(@"_viewingDay: %@", _viewingDay); // returns null! :(
NSLog(@"_viewingDay.clocks: %@", _viewingDay.clocks); // also returns null!
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"clockIn" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
//
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
_fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
基於此,我重寫了setViewingDay方法:
- (void)setViewingDay:(Day *)viewingDay
{
if (_viewingDay != viewingDay) {
_viewingDay = viewingDay;
NSLog(@"setViewingDay: %@", _viewingDay); // returns expected object! =)
[self fetchRequest];
}
}
盡管如此,在此之后,我的UITableView仍然為空。 我的numberOfRowsInSection方法不斷返回0!
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[_fetchedResultsController sections] objectAtIndex:section];
NSLog(@"numberOfRowsInSection %d", [sectionInfo numberOfObjects]);
return [sectionInfo numberOfObjects];
}
我還添加了執行提取請求的UIButton 。 希望在我對托管對象的引用的實際設置之前找到執行fetchRequest的情況。 所以我點擊它,執行提取。 沒事了! 沒有任何行。
編輯:但是,通過使用支持的UIBUtton和IBAction,NSLogs波紋管從我的fetchResultsController內部確實返回了一個對象。 但是仍然沒有行!
NSLog(@"_viewingDay: %@", _viewingDay);
NSLog(@"_viewingDay.clocks: %@", _viewingDay.clocks);
支持UIButton執行提取:
- (IBAction)fetchem:(UIButton *)sender {
[_clocksTableView reloadData];
_fetchedResultsController = nil;
[NSFetchedResultsController deleteCacheWithName:nil];
[self fetchRequest];
NSLog(@"\n\nfetchem: %@ \n\nvd: %@ \n\nclocks: %@ ", _fetchedResultsController.description, _viewingDay.description, _viewingDay.clocks.description );
}
我在這里可能會想念什么嗎?
謝謝!
編輯2:
我只是意識到將我的謂詞設置為(1 == 1),會返回所有時鍾。 確認我的resultsController設置正確。 我的謂詞可能有問題...我看不到什么。 我在以前的控制器中有一個類似的謂詞,並且效果很好。
謂詞很簡單,沒什么花哨的:
[fetchRequest setPredicate: [NSPredicate predicateWithFormat:@"day == %@", _viewingDay]];
我有一個Clock and Day實體。
一天有很多鍾。 一對多的關系。
時鍾只有一天,只有一天。
上面的fetchRequest應該已經返回了當天的所有時鍾。 但是不是嗎
1)awakeFromNib在實例化單元格時即在設置查看日期之前執行。 如果單元被重用,它也不會執行,因此您應該單獨觸發獲取請求。
2)如何設置單元格內部的fetchedResultsController屬性?
編輯1:
從數據模型和謂詞來看,似乎時鍾和日期之間的關系未正確設置。
編輯2:
從下面引述質詢者的評論:“在插入的[setter fetchRequest]插入我的覆蓋設置器后,問題已解決。”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.