objective-c - UITableView 自定义单元格图像在滚动后消失。

标签 objective-c ios gridview uitableview

我正在创建我的开放式 GridView 。

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

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,并且一切正常。

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

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

最佳答案

分享我的经验:

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

对我来说,问题是我正在使单元格出列,并为所有单元格提供相同的“唯一”ID。所以几个表共享相同的 ID,我相信它们相互冲突并弄乱了我正在查看的单元格。

为每个表赋予它自己的唯一标识符为我解决了这个问题。 (哒!)

关于objective-c - UITableView 自定义单元格图像在滚动后消失。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8981366/

相关文章:

iphone - 在运行时获取 iPhone/iPad/iPod Touch 的 ppi

ios - 单例中的@Published 属性在第一个事件之后不发出事件

ios - Swift 2.0 - 二元运算符 "|"不能应用于两个 UIUserNotificationType 操作数

gridview - 以编程方式将模板列添加到 gridview

Android gridview,如何去除网格项之间的间隙

iOS状态栏和UIView

iPhone:仅将 UITableView 的一部分置于编辑模式

ios - 自定义 UIViews 奇怪的行为

ios - 通过在 iOS 中使用 AudioQueue 记录的 WebSocket 流式传输音频

javascript - 如何使用 Javascript 从 GridView 单元格获取值