Ios UITableView在移出屏幕之前不会完全填充单元格
我正在使用Parse填充tableview中的单元格。每个单元格都包含用户名、用户图片和实际帖子内容。当我运行应用程序时,用户名和帖子内容会加载到每个单元格中。但是,直到单元格移出屏幕,然后移回屏幕,图片才会加载。以下是我的查询代码:Ios UITableView在移出屏幕之前不会完全填充单元格,ios,objective-c,uitableview,parse-platform,xcode6,Ios,Objective C,Uitableview,Parse Platform,Xcode6,我正在使用Parse填充tableview中的单元格。每个单元格都包含用户名、用户图片和实际帖子内容。当我运行应用程序时,用户名和帖子内容会加载到每个单元格中。但是,直到单元格移出屏幕,然后移回屏幕,图片才会加载。以下是我的查询代码: -(void)retrieveFromParse { PFQuery *retrievePosts = [PFQuery queryWithClassName:@"Posts"]; [retrievePosts orderByDescending:@"creat
-(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)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;
}
-(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;
}
我正在使用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];
});
}];
}
-(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;
}
-(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,图片会显示吗?下载可能需要一段时间。下面指出的线程问题只是好奇这行代码做什么?[自我刷新控制结束刷新];我有一个刷新控件,所以你可以拉刷新表视图。该控件具有运行retrieveFromParse方法的操作。这样,一旦检索到所需的所有内容,行就会停止刷新控件。这太棒了!快乐编码:)