滚动后UITableView自定义单元格图像消失。

时间:2012-01-24 02:30:52

标签: objective-c ios gridview uitableview

我正在创建我的开放网格视图。

我创建了一个看起来像这样的自定义单元格:

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;
}

它有点乱,但它有效.....直到我滚动。加载图像后,滚动屏幕,图像消失。

我相信这可能是由于细胞的imageViews丢失了?

我尝试了完全相同的实现,但每个单元格中只有1个imageView,并且一切都很好。

非常感谢正确方向的任何一点。

感谢您的帮助,感谢您的时间。

2 个答案:

答案 0 :(得分:3)

分享我的经验:

有类似的问题。我在应用程序中有很多表,其中一个数据会随机消失,最终整个表都会消失。

对我而言,问题在于我将细胞出列,给予他们所有相同的“独特”身份。因此,几个表共享相同的ID,我相信它们是相互冲突的,并且弄乱了我正在查看的单元格。

给每张桌子它自己的唯一标识符为我解决了问题。 (DAH!)

答案 1 :(得分:2)

我认为问题是因为您的单元格正在出列,并且您使用indexPath.row以外的行信息在tableView:cellForRowAtIndexPath:中填充单元格。

也许可以尝试以下几点:

- (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;
}