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

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

本文介绍了UITableView 自定义单元格图像在滚动后消失.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am in the process of creating my open grid view.

我创建了一个如下所示的自定义单元格:

I created a custom cell that looks like so:

我像这样处理填充它:

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

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

It is a bit messy but it works.....until I scroll. After the image is loaded then scrolled off the screen the image disappears.

我相信这可能是由于单元格的 imageViews 丢失了?

I believe that it maybe due to the imageViews of the cells being lost?

我尝试了完全相同的实现,但每个单元格中只有 1 个 imageView,并且一切正常.

I tried the exact same implementation but with only 1 imageView in each cell and all works great.

任何指向正确方向的点都将不胜感激.

Any point in the right direction would be very much appreciated.

感谢您的帮助,感谢您抽出宝贵时间.

I appreciate any help and thank you for your time.

推荐答案

我认为问题是因为您的单元格正在出列,并且您正在 tableView:cellForRowAtIndexPath: 中使用除indexPath.row.

I believe the problem is because your cells are being dequeued and you are populating cells in tableView:cellForRowAtIndexPath: using row information other than the indexPath.row.

也许可以尝试以下方法:

Maybe try something along the following lines:

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

这篇关于UITableView 自定义单元格图像在滚动后消失.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 04:37