- (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在后台
非常感谢。
我更新了上面的方法,以反映当前标准时,我收到以下错误的。 – kschins 2012-02-24 00:28:18
编辑我的回复,忘记beginUpdates/endUpdates。我认为现在应该全部解决。 – fbernardo 2012-02-24 15:49:06
是的,我不知道。这也是我尝试过的,因为我知道UI更新应该由主线程处理,但同样的错误。我将不得不单独调查一下。我不知道为什么表视图更新不一致。这种方法应该没问题。 – kschins 2012-02-24 19:53:20