gpt4 book ai didi

ios - UITableView 在移出屏幕之前不会完全填充单元格

转载 作者:行者123 更新时间:2023-11-29 12:32:33 24 4
gpt4 key购买 nike

我正在使用 Parse 填充表格 View 中的单元格。每个单元格都包含用户名、用户图片和实际发布内容。当我运行该应用程序时,用户名和帖子内容会加载到每个单元格中。但是,直到将单元格移出屏幕然后再移回时,才会加载图片。这是我涉及查询的代码:

-(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];
});
}];
}

这是表格 View 的代码:

-(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;

}

最佳答案

我正在使用 Parse 填充表格 View 中的单元格。每个单元格都包含用户名、用户图片和实际发布内容。当我运行该应用程序时,用户名和帖子内容会加载到每个单元格中。但是,直到将单元格移出屏幕然后再移回时,才会加载图片。这是我涉及查询的代码:

-(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];
});
}];
}

这是表格 View 的代码:

 -(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;

}

关于ios - UITableView 在移出屏幕之前不会完全填充单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27240617/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com