indexPathForObject返回nil

时间:2015-04-06 19:56:30

标签: ios uitableview core-data nsfetchedresultscontroller

我将一个对象从我的UITableViewController传递给一个单例,然后通过一个通知返回到我的UITableViewController。 UITableViewController使用NSFetchedResultsController。然后它需要fetchedResultsController中的Object的indexPath。

Object *obj = (Object *)notification.object;
NSIndexPath *index = [self.fetchedResultsController indexPathForObject:obj];

我在AFNetworking / AFDownloadRequestOperation进度块中设置了通知:

- (void)downloadObject:(Object *)object
{
........

    [operation1 setProgressiveDownloadProgressBlock:^(AFDownloadRequestOperation *operation, NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile) {
                         ////// Sending Notification
                         ////// Try to send notification only on significant download
                         totalBytesRead = ((totalBytesRead *100)/totalBytesExpectedToReadForFile);
                         NSLog(@"TOTAL BYTES IN PROGRESS BLOCK: %llu",totalBytesRead);
                         NSMutableDictionary *userDictionary = [[NSMutableDictionary alloc]init];
                         [userDictionary setObject:[NSNumber numberWithLongLong:totalBytesRead] forKey:@"progress"];
                         [userDictionary setObject:[NSNumber numberWithLongLong:totalBytesExpectedToReadForFile] forKey:@"totalBytes"];
                         [[NSNotificationCenter defaultCenter] postNotificationName:[NSString stringWithFormat:@"DownloadProgress%@",object.uniqueID] object:object userInfo:userDictionary];
}

[self.fetchedResultsController fetchedObjects];中只有一个对象,它与通知中传回的对象相匹配。 index始终为nil,但fetchedResultsController不为nil,[self.fetchedResultsController fetchedObjects]返回正确的对象。为什么indexPathForObject没有?

2 个答案:

答案 0 :(得分:2)

我遇到了同样的问题。在.container{width:420px;height:420px;border:1px solid black;} .section1{width:140px;height:140px;background-color:green;float:left;} .section2{width:140px;height:170px;background-color:yellow;float:left;} .section3{width:140px;height:140px;background-color:red;float:left;} .section4{width:140px;height:140px;background-color:orange;float:left;} 的配置中,NSFetchedResultsController设置为引用(可选)相关对象。 事实证明,必须满足这种关系(即相关对象不得为零)。 在确保满足sectionNameKeyPath的每个关系后,sectionNameKeyPath方法再次按预期工作。

答案 1 :(得分:0)

在我看来,您正试图在通知对象的表格视图中找到一个对象......

您应该在表视图对象上调用索引路径方法,而不是通知对象。

尝试匹配您的本地变量:

Object *obj = (Object *)notification.object;

使用:

[self.fetchedResultsController fetchedObjects];

(如果它是唯一应该相对容易的那个)...

NSArray *arrayFetchedObjects = [self.fetchedResultsController fetchedObjects];
Object *tableObject = [arrayObjects lastObject];

(如果有多个,那么您可能需要过滤arrayFetchedObjects,但这是另一个问题)

然后在表视图中找到该获取对象的位置:

NSIndexPath *indexPath = [self.fetchedResultsController indexPathForObject:tableObject];