2012-02-23 23 views
1
- (void)fetchImages { 
    if (self.profileImages == nil) { 
     self.profileImages = [[NSMutableDictionary alloc] initWithCapacity:200]; 
    } 
    dispatch_async(dispatch_get_global_queue(0, 0), ^{ 
     for (id tweet in self.timeline) { 
      TWRequest *fetchUserImageRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://api.twitter.com/1/users/profile_image/%@", [tweet valueForKeyPath:@"user.screen_name"]]] parameters:nil requestMethod:TWRequestMethodGET]; 
      [fetchUserImageRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 
       if ([urlResponse statusCode] == 200) { 
        [self.profileImages setObject:[UIImage imageWithData:responseData] forKey:[tweet valueForKeyPath:@"user.screen_name"]]; 
        NSArray *indexPath = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:[self.timeline indexOfObject:tweet] inSection:0]]; 
        [self.tableView reloadRowsAtIndexPaths:indexPath withRowAnimation:UITableViewRowAnimationNone]; 
       } 
      }]; 
     } 
    }); 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FavoriteCell"]; 

    // configure cell 
    id tweet = [self.timeline objectAtIndex:[indexPath row]]; 
    UILabel *tweetLabel = (UILabel *)[cell viewWithTag:102]; 
    tweetLabel.text = [tweet objectForKey:@"text"]; 
    UILabel *usernameLabel = (UILabel *)[cell viewWithTag:101]; 
    usernameLabel.text = [tweet valueForKeyPath:@"user.name"]; 
    UIImageView *profileImage = (UIImageView *)[cell viewWithTag:100]; 
    profileImage.image = [self.profileImages objectForKey:[tweet valueForKeyPath:@"user.screen_name"]]; 
    UILabel *dateLabel = (UILabel *)[cell viewWithTag:103]; 
    NSString *labelString = [[tweet objectForKey:@"created_at"] substringToIndex:10]; 
    dateLabel.text = labelString; 

    return cell; 

} 

我得到的时间表,然后想要获得时间轴上的所有用户的个人资料图像。我需要循环播放推文并获取图像。我很好奇我如何确定何时获取了所有图像,然后重新加载tableview。截至目前还没有发生。 TWRequest在表重新加载后运行。我在这里做错了什么?也许有更好的方法来做到这一点?TWRequest在后台

非常感谢。

+0

我更新了上面的方法,以反映当前标准时,我收到以下错误的。 – kschins 2012-02-24 00:28:18

+0

编辑我的回复,忘记beginUpdates/endUpdates。我认为现在应该全部解决。 – fbernardo 2012-02-24 15:49:06

+0

是的,我不知道。这也是我尝试过的,因为我知道UI更新应该由主线程处理,但同样的错误。我将不得不单独调查一下。我不知道为什么表视图更新不一致。这种方法应该没问题。 – kschins 2012-02-24 19:53:20

回答

1

这是因为[TWRequest performRequestWithHandler:]是一种异步方法。为什么你需要重新加载整个表格?为什么不在获取图像时重新加载单元格(在处理程序块的末尾)?

如果您确实想重新加载整个表,只需保留所有已完成请求的计数,并在全部完成后重新加载表。

如果要重新加载,你可以这样做一个单细胞:

dispatch_sync(dispatch_queue_get_main(), ^{ 

    [self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:0]; 

} 
+0

我只得到更新特定的行,但调用 [self.tableView reloadRowsAtIndexPaths:withRowAnimation:]; 不起作用,因为没有办法保留要更新的行的索引,因为此变量未在可靠的基础上更新。 – kschins 2012-02-23 19:34:25

+0

这行不会像[NSIndexPath indexPathForRow:[self.timeline indexForObject:tweet] inSection:0]? – fbernardo 2012-02-23 21:19:04

+0

是的,我设法让那里更新正确的行,但它仍然在尝试更新表时抛出此错误 - “无效的表视图更新。应用程序已请求更新到与该状态不一致的表视图由数据源提供。“ – kschins 2012-02-24 00:23:14