我有一个使用FetchedResultsController管理TableView的自定义ViewController。
当我的视图被加载时,一切似乎都工作正常(单元格包含正确的数据),但是在滚动视图后,再次使用来自FetchedResultsController的数据绑定的单元格,但是某些对象的关系现在设置为nil
滚动期间不会触发controllerWillChangeContent 和 controllerDidChangeContent 。
这是我的视图控制器中的代码:
- (UITableViewCell *)tableView:(UITableView *)tb cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ClaimProductCell";
ClaimProductTableViewCell *cell = (ClaimProductTableViewCell*)[tb dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelItems = [[self productCellLoader] instantiateWithOwner:self options:nil];
cell = [topLevelItems objectAtIndex:0];
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)configureCell:(UITableViewCell*)cell atIndexPath:(NSIndexPath*)indexPath
{
ClaimProduct* claimProduct = (ClaimProduct*)[[self fetchedResultsController] objectAtIndexPath:indexPath];
ClaimProductTableViewCell* productCell = (ClaimProductTableViewCell*)cell;
productCell.indexPath = indexPath;
[productCell bindWithClaimProduct:claimProduct];
}
此处遵循表格视图单元格绑定代码:
- (void)bindWithClaimProduct:(ClaimProduct*)claimProduct
{
DocumentProduct* documentProduct = claimProduct.claimProductToDocumentProduct;
Product* product = documentProduct.documentProductToProduct;
self.addButton.hidden = YES;
self.productCode.text = product.code;
self.quantityLabel.text = [documentProduct.qty stringValue];
self.nameLabel.text = product.descriptionName;
self.documentCode.text = documentProduct.documentProductToDocument.code;
NSDate * date = documentProduct.documentProductToDocument.date;
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];
self.documentDate.text = [NSString stringWithFormat:@"%ld/%ld/%ld", (long)[components day], (long)[components month], (long)[components year]];
// product image
NSString* imagePath = [ImagePathUtility imagePathFromImageName:product.image imageCode:product.code imageType:kImageProduct];
[self.productImageView setImageWithPath:imagePath placeholderName:kProductPlaceholderImage];
}
如果我关闭并再次打开应用程序,视图控制器会正确显示单元格。如果我滚动它们,我又会遇到同样的问题。
我正在使用Xcode 5并处理一个不使用ARC的旧项目。
[编辑]这是NSFetchedResultsController延迟实例化代码:
- (NSFetchedResultsController*)fetchedResultsController
{
if (_fetchedResultsController)
return _fetchedResultsController;
NSManagedObjectContext* mainContext = [[AppDelegate sharedAppDelegate] managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"ClaimProduct" inManagedObjectContext:mainContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entity];
[fetchRequest setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObject:@"claimProductToDocumentProduct.documentProductToProduct"]];
[fetchRequest setReturnsObjectsAsFaults:NO];
[fetchRequest setFetchBatchSize:20];
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"claimProductToDocumentProduct.documentProductToProduct.code" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"claimProductToClaim == %@", [self claim]];
[fetchRequest setPredicate:predicate];
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:mainContext sectionNameKeyPath:nil cacheName:nil];
_fetchedResultsController.delegate = self;
[fetchRequest release];
[sortDescriptor1 release];
[sortDescriptors release];
return _fetchedResultsController;
}
[编辑2]我注意到在[self.fetchedresultscontroller fetchedObjects]上循环时出现了另一个错误。关系中的对象似乎是非零的,但它的属性都是零。这是代码的一部分:
- (void)updateTotals
{
NSArray * claimProducts = [self.fetchedResultsController fetchedObjects];
NSDecimalNumber * imponibile = [NSDecimalNumber decimalNumberWithString:@"0"];
NSDecimalNumber * lordo = [NSDecimalNumber decimalNumberWithString:@"0"];
for (ClaimProduct * claimProduct in claimProducts) {
if (![claimProduct.qty isEqualToNumber:@0]) {
//compute totals
NSDecimalNumber * unitPrice = [claimProduct.claimProductToDocumentProduct.price decimalNumberByDividingBy:claimProduct.claimProductToDocumentProduct.qty];
NSDecimalNumber * claimProductPrice = [unitPrice decimalNumberByMultiplyingBy:claimProduct.qty];
imponibile = [imponibile decimalNumberByAdding:claimProductPrice];
NSDecimalNumber * unitPriceLordo = [claimProduct.claimProductToDocumentProduct.totalprice decimalNumberByDividingBy:claimProduct.claimProductToDocumentProduct.qty];
NSDecimalNumber * claimProductPriceLordo = [unitPriceLordo decimalNumberByMultiplyingBy:claimProduct.qty];
lordo = [lordo decimalNumberByAdding:claimProductPriceLordo];
}
}
...
}
这里的claimProductToDocumentProduct关系不是nil,但有时会发生它的属性为nil而我在 decimalNumberByAdding 中得到一个EXC_BAD_ACCESS。