我正在使用Parse来填充tableview中的单元格。每个单元格都包含用户名,用户的图片和实际的帖子内容。当我运行应用程序时,用户名和帖子内容将加载到每个单元格中。但是,在将单元格移出屏幕然后向后移动之前,图片不会加载。这是我的代码涉及查询:
-(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];
});
}];
}
以下是tableview的代码:
-(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;
}
答案 0 :(得分:1)
我正在使用Parse来填充tableview中的单元格。每个单元格包含用户名,用户的图片和实际的帖子内容。当我运行应用程序时,用户名和帖子内容将加载到每个单元格中。但是,在将单元格移出屏幕然后向后移动之前,图片不会加载。这是我的代码涉及查询:
-(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];
});
}];
}
以下是tableview的代码:
-(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;
}