pengwang pengwang - 3 months ago 16
iOS Question

coredate data not show at tableView

I have used coredate on the tableView,when I enter the viewController:

dispatch_queue_t downloadQueue = dispatch_queue_create("socket login", NULL);
dispatch_async(downloadQueue, ^{
NSManagedObjectContext *managedObjectContext =[self getManagedObjectContext];
noticListArr=[[CoreDataManager sharedInstance] readEvent:@"NotificationEntity" SortDescriptor:nil managedObjectContext:managedObjectContext];
NSLog(@"refreshData.count:%d ",noticListArr.count);
for(int i=0;i<noticListArr.count;i++){
NotificationEntity *notificationEntity=[noticListArr objectAtIndex:i];
NSLog(@"notificationEntity.name:%@ ",notificationEntity.name);
NSLog(@"notificationEntity.describeString:%@ ",notificationEntity.describeString);
NSLog(@"notificationEntity.source:%@", notificationEntity.source);
}
});

dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView1 reloadData];
});
dispatch_release(downloadQueue);


all logs contains value but when I used

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"NotificationViewCell";
NotificationViewCell *cell = (NotificationViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

NotificationEntity *notificationEntity=(NotificationEntity *)[noticListArr objectAtIndex:indexPath.row];

NSLog(@"tableViewnoticListArr.count:%d ",noticListArr.count);
NSLog(@"indexPath.row:%d ",indexPath.row);

NSLog(@"notificationTable.avatarsmall:%@", notificationEntity.avatarsmall);
NSLog(@"notificationTable.source:%@", notificationEntity.source);
NSLog(@"notificationTable.name:%@", notificationEntity.name);
NSLog(@"notificationTable.describeString:%@ ",notificationEntity.describeString);
}


only

NSLog(@"tableViewnoticListArr.count:%d ",noticListArr.count);
NSLog(@"indexPath.row:%d ",indexPath.row);


gives me correct result but all notificationEntity.name notificationEntity.describeString is null.I don't know why at the tabelview cell all the values of notificationEntity is null.

Answer

Check with this change,

dispatch_async(downloadQueue, ^{
    NSManagedObjectContext *managedObjectContext = [self getManagedObjectContext];
    self.noticListArr = [[CoreDataManager sharedInstance] readEvent:@"NotificationEntity" SortDescriptor:nil managedObjectContext:managedObjectContext];
    NSLog(@"refreshData.count:%d ",noticListArr.count);
    for(int i=0; i<noticListArr.count; i++){
        NotificationEntity *notificationEntity = [noticListArr objectAtIndex:i];
        NSLog(@"notificationEntity.name:%@ ", notificationEntity.name);
        NSLog(@"notificationEntity.describeString:%@ ", notificationEntity.describeString);
        NSLog(@"notificationEntity.source:%@", notificationEntity.source);
    }     
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView1 reloadData];
    });
});

dispatch_release(downloadQueue);