2014-12-02 51 views
1

我使用分析來填充表格中的單元格。每個單元格都包含一個用戶名,用戶的圖片和實際的帖子內容。當我運行應用程序時,用戶名和帖子內容被加載到每個單元格中。但是,直到單元格移出屏幕然後再移回,圖片纔會加載。這裏是我的代碼涉及查詢: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; 

} 
+0

如果你稍等一會兒,而不是移動的tableview沒有畫面顯示?下載可能需要一段時間。 – Yan 2014-12-02 02:01:53

+0

線程問題如下所示 – Andy 2014-12-02 02:26:46

+0

只是好奇這條線是做什麼的? [self.refreshControl endRefreshing]; – Xiangdong 2014-12-02 03:07:29

回答

1

。每個單元格都包含一個用戶名,用戶的圖片和實際的帖子內容。當我運行應用程序時,用戶名和帖子內容被加載到每個單元格中。但是,直到單元格移出屏幕然後再移回,圖片纔會加載。這裏是我的代碼涉及查詢:

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

} 
+0

讓我知道如果解決方案的工作。 – Yan 2014-12-02 14:33:58

+0

是的,工作。非常感謝。 – bpomp87 2014-12-02 15:38:11

+0

太棒了!快樂編碼:) – Yan 2014-12-02 15:49:15