UITableView不填充细胞,直到移出屏幕
问题描述:
我使用分析来填充表格中的单元格。每个单元格都包含一个用户名,用户的图片和实际的帖子内容。当我运行应用程序时,用户名和帖子内容被加载到每个单元格中。但是,直到单元格移出屏幕然后再移回,图片才会加载。这里是我的代码涉及查询: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;
}
答
。每个单元格都包含一个用户名,用户的图片和实际的帖子内容。当我运行应用程序时,用户名和帖子内容被加载到每个单元格中。但是,直到单元格移出屏幕然后再移回,图片才会加载。这里是我的代码涉及查询:
-(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];
});
}];
}
下面是实现代码如下代码:
-(void)imageFromImageFile:(PFFile *)imageFile forCell:(PostTableViewCell *)cell
{
PostTableViewCell *workingCell = cell;
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc]initWithFrame:postCell.posterImage.frame];
[workingCell.posterImage addSubview:spinner];
spinner.color = [UIColor whiteColor];
[spinner startAnimating];
[imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
if(workingCell == cell){
dispatch_async(dispatch_get_main_queue(), ^{
[spinner removeFromSuperview];
workingCell.posterImage.image = [UIImage imageWithData:data];
[workingCell setNeedsLayout];
});
}else{
dispatch_async(dispatch_get_main_queue(), ^{
[spinner removeFromSuperview];
});
}
}
}];
}
-(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"];
imageFile = [postObject objectForKey:@"posterPicture"];
cell.posterImage.image = nil;
[self imageFromImageFile:imageFile forCell:cell];
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