尝试从viewDidLoad中的可见单元格中重新加载数据

时间:2014-04-26 20:31:17

标签: ios tableview reloaddata

[self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationNone]; 

给出错误: NSInternalInconsistencyException',原因:'无效更新:第0部分中的无效行数

我在后台进行查询..一旦数据可用..需要重新加载单元格..使用新的图像和数据..我只在应用程序打开时才需要这样,因此在viewDidLoad中。 。[self.tableView reloadData]的问题是它会更新整个UI,从而产生闪烁效果。

代码:

- (void)viewDidLoad



{

[super viewDidLoad];

self.myTableView.dataSource = self;

self.myTableView.delegate = self;



PFQuery * getCollectionInfo = [PFQuery queryWithClassName:@"Collection"];   // make query    

[getCollectionInfo setCachePolicy:kPFCachePolicyCacheThenNetwork];

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_async(queue, ^{

    [getCollectionInfo findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

        if (!error) {

            CollectionQueryResult = (NSMutableArray *)objects

//                [self.tableView reloadData]; // creates flicker

            [self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationNone];

        }

        else{

           //no errors

        }

   }];

});

}



-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

return 1;

}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return [CollectionQueryResult count] ;

}



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

// configure cell

    static NSString * cellIdentifier = @"Cell";

    CollectionsTableCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

if (cell == nil) {

    cell = [[CollectionsTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

}

else {

   cell.cellImageView.image = nil;

}

[cell.collectionLoadingActivity startAnimating];

    collectionInfo = [CollectionQueryResult objectAtIndex:indexPath.row];

NSString * collName = [collectionInfo objectForKey:@"name"];

cell.cellTitle.text = [NSString stringWithFormat:@" %@",[collName uppercaseString]];

PFFile * collectionImageFile = [collectionInfo objectForKey:@"photo_landscape"];

dispatch_queue_t CollectionImageQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_async(CollectionImageQueue, ^{

    [collectionImageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {

        dispatch_async(dispatch_get_main_queue(), ^{

            cell.cellImageView.image = [UIImage imageWithData:data];

            [cell setNeedsLayout];

        });

    } progressBlock:^(int percentDone) {

        if ( !(percentDone == 100)) {

            [cell.collectionLoadingActivity isAnimating];

        }

        else if (percentDone == 100){    

            [cell.collectionLoadingActivity stopAnimating];

        }

    }];

});

return cell;

}



#pragma mark - Table View Delegate

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

self.selectedCollection = [CollectionQueryResult objectAtIndex:indexPath.row];   //to pass on

[self performSegueWithIdentifier:@"showCollectionItems" sender:self];  

}



-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

if ([segue.identifier isEqualToString:@"showCollectionItems"]) {

    CollectionItemsViewController * collectionItems = (CollectionItemsViewController *)segue.destinationViewController;

    collectionItems.collection = self.selectedCollection;

    [segue.destinationViewController setHidesBottomBarWhenPushed:YES];

}

}

@end

2 个答案:

答案 0 :(得分:1)

viewDidLoad是重新加载表格视图单元格的不好的地方。将代码移动到viewDidAppear方法。

答案 1 :(得分:1)

首先,它崩溃了,因为你没有可见的行,首先要检查这个

if([self.tableView indexPathsForVisibleRows].count)
{
// Do something
}

或者,如果您想重新加载而不闪烁,请尝试以下代码..

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];