Разница между двумя способами доступа к UIImageView под UITableViewCell UITableView - PullRequest
0 голосов
/ 20 января 2012

У меня есть собственное табличное представление для загрузки новостей через веб-сервис.Я меняю изображение в каждой ячейке в методе cellForRowAtIndexPath .После того, как я получил нужную ячейку, я могу получить доступ к изображению двумя способами:

    UIImageView *imageView = (UIImageView *)[cell viewWithTag:12];
    imageView.image = news.image;

или напрямую:

    cell.imageView.image = news.image;

, когда я использую способ second ,некоторые изображения попадают не в ту клетку, а первый способ дает мне идеальный результат.Это также произошло в методе обратного вызова iconDownLoadFinsh .

Я чувствую растерянность по этому поводу.Может кто-нибудь попытаться объяснить это?Пожалуйста, не стесняйтесь задавать вопросы.Заранее спасибо.

- (void)iconDownLoadFinsh:(NSData *)imageData row:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    UIImageView *imageView = (UIImageView *)[cell viewWithTag:12];
    imageView.image = [UIImage imageWithData:imageData];
    NewsModel *newsModel = [newsArray objectAtIndex:indexPath.row];
    newsModel.image = [UIImage imageWithData:imageData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"NewsCellIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"NewsTableViewCell" owner:self options:nil];
        cell = newsTableViewCell;
        newsTableViewCell = nil;
    }

    // read from newsModel
    NewsModel *news = [newsArray objectAtIndex:indexPath.row];

    UILabel *label;
    label = (UILabel *)[cell viewWithTag:10];
    label.text = [NSString stringWithFormat:news.title];
    label = (UILabel *)[cell viewWithTag:11];
    label.text = [NSString stringWithFormat:news.description];
    UIImageView *imageView;
    imageView = (UIImageView *)[cell viewWithTag:12];
    imageView.image = news.image;

    if (news.image == nil)
    {
        imageView.image = [UIImage imageNamed:IconPlaceHolder];

//      if (self.tableView.dragging == NO && self.tableView.decelerating == NO) {

            IconDownLoader *iconDownLoader = [[IconDownLoader alloc] init];
            iconDownLoader.url = news.imageUrl;
            iconDownLoader.delegate = self;
            iconDownLoader.indexPath = indexPath;
            [downloadArray addObject:iconDownLoader];
            [iconDownLoader start];
//      }
    }

    return cell;
}

1 Ответ

1 голос
/ 20 января 2012

Я обычно всегда сбрасываю свойства ячейки в методе

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

Поэтому я сделаю следующее после получения ячейки

if (cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"NewsTableViewCell" owner:self options:nil];
    cell = newsTableViewCell;
    newsTableViewCell = nil;
}
cell.imageView.image = nil; // Here resetting the imageview's image to nil.

Поскольку вы повторно используете ячейки и если вы делаетенепонятные старые данные, данные ячейки могут быть повреждены.

...