我使用分析來填充表格中的單元格。每個單元格都包含一個用戶名,用戶的圖片和實際的帖子內容。當我運行應用程序時,用戶名和帖子內容被加載到每個單元格中。但是,直到單元格移出屏幕然後再移回,圖片纔會加載。這裏是我的代碼涉及查詢:UITableView不填充細胞,直到移出屏幕
-(void)retrieveFromParse {
PFQuery *retrievePosts = [PFQuery queryWithClassName:@"Posts"];
[retrievePosts orderByDescending:@"createdAt"];
retrievePosts.cachePolicy = kPFCachePolicyCacheThenNetwork;
[retrievePosts findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
postsArray = [[NSArray alloc] initWithArray:objects];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.refreshControl endRefreshing];
});
}];
}
下面是實現代碼如下代碼:我使用的解析來填充在實現代碼如下細胞
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
postCell = [tableView dequeueReusableCellWithIdentifier:@"PostCellOne"];
if (postCell == nil) {
postCell = [[PostTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PostCellOne"];
}
postCell.backgroundColor = [UIColor blackColor];
postCell.posterName.textColor = [UIColor whiteColor];
postCell.postContent.textColor = [UIColor whiteColor];
PFObject *postObject = [postsArray objectAtIndex:indexPath.row];
postCell.posterName.text = [postObject objectForKey:@"posterName"];
postCell.postContent.text = [postObject objectForKey:@"postContent"];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc]initWithFrame:postCell.posterImage.frame];
[postCell.posterImage addSubview:spinner];
spinner.color = [UIColor whiteColor];
[spinner startAnimating];
imageFile = [postObject objectForKey:@"posterPicture"];
[imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
dispatch_async(dispatch_get_main_queue(), ^{
[spinner removeFromSuperview];
postCell.posterImage.image = [UIImage imageWithData:data];
[tableView reloadInputViews];
});
}
else {
dispatch_async(dispatch_get_main_queue(), ^{
[spinner removeFromSuperview];
});
}
}];
return postCell;
}
如果你稍等一會兒,而不是移動的tableview沒有畫面顯示?下載可能需要一段時間。 – Yan 2014-12-02 02:01:53
線程問題如下所示 – Andy 2014-12-02 02:26:46
只是好奇這條線是做什麼的? [self.refreshControl endRefreshing]; – Xiangdong 2014-12-02 03:07:29