Моя цель - установить текст, затем сделать асинхронный вызов для изображения и установить изображение по прибытии.UITableView asynchronous UIImage setting
Я в настоящее время сделать асинхронный вызов для изображений, чтобы установить в UITableViewCell
, и заметили мою предыдущую технику привел в петле я недавно идентифицированный:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell* cell = [_tableView dequeueReusableCellWithIdentifier:@"id"]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"id"]; } [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:userAvatar]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { if (data) { UIImage* avatarImage = [UIImage imageWithData:data]; if (avatarImage) { dispatch_async(dispatch_get_main_queue(), ^{ [cell.imageView setImage:avatarImage]; }); /* .--. [STOP] .----' '--. | '-()-----()-' | If I include the following, the program goes in a loop, and have not readily identified workaround: dispatch_async(dispatch_get_main_queue(), ^{ [_tableView reloadData]; }); [GO] .--. | .----' '--. | '-()-----()-' */ } } }]; cell.textLabel.text = @"foo"; cell.detailTextLabel.text = @"bar"; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; return cell; }
Есть ли методика для получения результата я ищу без пользовательский сеттер в пользовательском классе?
Не называть 'reloadData' из' cellForRow ...'. Плохие вещи случаются. Посмотрите пример приложения Apple LazyTableImages, чтобы посмотреть, как это сделать. – rmaddy
Вы также можете использовать AFNetworking, чтобы сделать это легко - проверьте дополнения UIImageView + AFNetworking.h. – joao
@joao AFNetworking использует очень упрощенный подход, который в основном «заставляет его работать», но на самом деле представляет более серьезную проблему. Поэтому я бы не рекомендовал его. – CouchDeveloper