Пользовательские изображения ячеек UITableView исчезают после прокрутки. - PullRequest
4 голосов
/ 24 января 2012

Я нахожусь в процессе создания моего открытого вида сетки.

Я создал пользовательскую ячейку, которая выглядит следующим образом:

enter image description here

Я выполняю заполнение следующим образом:

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

    TableGalleryCustomCell *cell = (TableGalleryCustomCell*)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if( cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TableGalleryCustomCell" owner:nil options:nil];
        for (id currentObject in topLevelObjects) {
            if ([currentObject isKindOfClass:[UITableViewCell class]]) {
                cell = (TableGalleryCustomCell*)currentObject;
                break;
            }
        }
    }

    if (countingIndex < [[[self.appDelegate rssParser] rssItems] count]) {
        cell.firstAddress.text = [[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] Address];
        cell.firstPrice.text = [[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] Deposit];
        if ([[[[[[self appDelegate] rssParser]rssItems]objectAtIndex:countingIndex] imageURLs] count] != 0 ) {
            [cell.firstImage setImageWithURL:[NSURL URLWithString:[[[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] imageURLs] objectAtIndex:0.0]]];
            cell.firstImage.tag = countingIndex;
        }
    }

    countingIndex++;
    if (countingIndex < [[[self.appDelegate rssParser] rssItems] count]) {
        cell.secondAddress.text = [[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] Address];
        cell.secondPrice.text = [[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] Deposit];
        if ([[[[[[self appDelegate] rssParser]rssItems]objectAtIndex:countingIndex] imageURLs] count] != 0 ) {
            [cell.secondImage setImageWithURL:[NSURL URLWithString:[[[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] imageURLs] objectAtIndex:0.0]]];
            cell.secondImage.tag = countingIndex;
        }
    }

    countingIndex++;
    if (countingIndex < [[[self.appDelegate rssParser] rssItems] count]) {
        cell.thirdAddress.text = [[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] Address];
        cell.thirdPrice.text = [[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] Deposit];
        if ([[[[[[self appDelegate] rssParser]rssItems]objectAtIndex:countingIndex] imageURLs] count] != 0 ) {
            [cell.thirdImage setImageWithURL:[NSURL URLWithString:[[[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] imageURLs] objectAtIndex:0.0]]];
            cell.thirdImage.tag = countingIndex;
        }
    }

    countingIndex++;
    if (countingIndex < [[[self.appDelegate rssParser] rssItems] count]) {
        cell.fourthAddress.text = [[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] Address];
        cell.fourthPrice.text = [[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] Deposit];
        if ([[[[[[self appDelegate] rssParser]rssItems]objectAtIndex:countingIndex] imageURLs] count] != 0 ) {
            [cell.fourthImage setImageWithURL:[NSURL URLWithString:[[[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] imageURLs] objectAtIndex:0.0]]];
            cell.fourthImage.tag = countingIndex;
        }
    }

    countingIndex++;


    return cell;
}

Этонемного грязно, но это работает ..... пока я не прокручиваю.После того, как изображение загружено, затем прокручивается с экрана, изображение исчезает.

Я полагаю, что это может быть из-за потери изображения ячеек?

Я попробовал точно такую ​​же реализацию, но только с 1 imageView в каждой ячейке, и все прекрасно работает.

Любая точка в правильном направлении будет очень цениться.

Я ценю любую помощь и спасибо за ваше время.

Ответы [ 2 ]

3 голосов
/ 29 января 2012

делюсь своим опытом:

Были похожие проблемы. У меня в приложении много таблиц, и в одной из них данные случайно исчезают, и в итоге вся таблица исчезает.

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

Предоставление каждой таблице собственного уникального идентификатора решило проблему для меня. (Д!)

2 голосов
/ 24 января 2012

Я полагаю, что проблема в том, что ваши ячейки удаляются из очереди, и вы заполняете ячейки в tableView:cellForRowAtIndexPath:, используя информацию о строках, отличную от indexPath.row.

Может быть, попробовать что-то вроде следующего:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray         * rssItems;
    id                rssItem;
    static NSString * MyIdentifier = @"Cell";

    TableGalleryCustomCell *cell = (TableGalleryCustomCell*)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if( cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TableGalleryCustomCell" owner:nil options:nil];
        for (id currentObject in topLevelObjects) {
            if ([currentObject isKindOfClass:[UITableViewCell class]]) {
                cell = (TableGalleryCustomCell*)currentObject;
                break;
            }
        }
    }

    rssItems =  [[self.appDelegate rssParser] rssItems];
    if (indexPath.row < [rssItems count]) {
        rssItem = [rssItems objectAtIndex:indexPath.row];
        cell.firstAddress.text = [rssItem Address];
        cell.firstPrice.text   = [rssItem Deposit];
        if ([[rssItem imageURLs] count] != 0 ) {
            [cell.firstImage setImageWithURL:[NSURL URLWithString:[[rssItem imageURLs] objectAtIndex:0.0]]];
            cell.firstImage.tag = indexPath.row;
        }
    }

    return cell;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...