滚动后,UITableView自定义单元格图像消失。 [英] UITableView custom cell images disappear after scrolling.

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

问题描述

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



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


我会像这样处理它:

   - (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:@TableGalleryCustomCellowner:nil options:nil];
for(id currentObject in topLevelObjects){
if([currentObject isKindOfClass:[UITableViewCell class]]){
cell =(TableGalleryCustomCell *)currentObject;
休息;


$ b 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;



$ b $ p
$ b

这有点麻烦,但它的工作.....直到我滚动。图像加载后,滚动屏幕图像消失。



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

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

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



感谢您的宝贵时间。

解决方案

我相信问题是因为您的单元正在出列,并且您正在填充 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:@TableGalleryCustomCellowner:nil options:nil];
for(id currentObject in topLevelObjects){
if([currentObject isKindOfClass:[UITableViewCell class]]){
cell =(TableGalleryCustomCell *)currentObject;
休息;
}
}
}

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


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

I created a custom cell that looks like so:

I handle populating it 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.

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

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.

解决方案

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自定义单元格图像消失。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆