【问题标题】:Reload UITableview data in cell out of view重新加载单元格中的 UITableview 数据
【发布时间】:2011-12-23 01:51:36
【问题描述】:
[self.countryList reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:6 inSection:0], nil] withRowAnimation:UITableViewRowAnimationNone]; // or NO ;)

我的问题是我有一个包含 10 多个项目的表格视图,其中只有 6 个单元格,但是当加载数据并滚动表格时,单元格 7 中没有数据。如何重新加载该 1 个单元格中的数据?

上面的代码是我正在使用的,它不起作用!

---编辑---

if (cell == nil) {  

    cell =[[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:identifier]autorelease];
    //[cell setBackgroundColor: [UIColor clearColor]];




    mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(52.0, 0.0, 200.0, 20.0)] autorelease];           
    mainLabel.tag = 1;          
    mainLabel.font = [UIFont systemFontOfSize:14.0];            
    mainLabel.textAlignment = UITextAlignmentLeft;          
    mainLabel.textColor = [UIColor blackColor];
    mainLabel.opaque = YES;
    mainLabel.backgroundColor=[UIColor clearColor];
    [cell.contentView addSubview:mainLabel];

    revenue = [[[UILabel alloc] initWithFrame:CGRectMake(52.0, 20.0, 150.0, 20.0)] autorelease];            
    revenue.tag = 3;            
    revenue.font = [UIFont systemFontOfSize:14.0];          
    revenue.textAlignment = UITextAlignmentLeft;            
    revenue.textColor = [UIColor blackColor];
    revenue.opaque = YES;
    revenue.backgroundColor=[UIColor clearColor];
    [cell.contentView addSubview:revenue];

    promos = [[[UILabel alloc] initWithFrame:CGRectMake(252.0, 20.0, 150.0, 20.0)] autorelease];            
    promos.tag = 4;         
    promos.font = [UIFont systemFontOfSize:14.0];           
    promos.textAlignment = UITextAlignmentLeft;         
    promos.textColor = [UIColor blackColor];
    promos.opaque = YES;
    promos.backgroundColor=[UIColor clearColor];
    [cell.contentView addSubview:promos];

    updates = [[[UILabel alloc] initWithFrame:CGRectMake(252.0, 0.0, 200.0, 20.0)] autorelease];            
    updates.tag = 5;            
    updates.font = [UIFont systemFontOfSize:14.0];          
    updates.textAlignment = UITextAlignmentLeft;            
    updates.textColor = [UIColor blackColor];
    updates.opaque = YES;
    updates.backgroundColor=[UIColor clearColor];
    [cell.contentView addSubview:updates];

    photo = [[[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 50.0, 50.0)] autorelease];
    photo.contentMode= UIViewContentModeScaleToFill;
    photo.tag = 2;
    photo.opaque = YES;
    photo.backgroundColor=[UIColor clearColor];
    [cell.contentView addSubview:photo]; 

}    
else {
    cell.clearsContextBeforeDrawing=YES;
    mainLabel = (UILabel *)[cell.contentView viewWithTag:1];    
    photo = (UIImageView *)[cell.contentView viewWithTag:2];
    promos=(UILabel *)[cell.contentView viewWithTag:4];
    revenue=(UILabel *)[cell.contentView viewWithTag:3];
    updates=(UILabel *)[cell.contentView viewWithTag:5];


    if(photo.image){
        photo.image=nil;
    }
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *imageURL =[NSString stringWithString:[(arrayResults*)[tweets objectAtIndex:indexPath.row] myCountryIcon]];

    NSArray *myArray = [imageURL componentsSeparatedByString: @"/"];
    NSString *fileName = [NSString stringWithString:[myArray lastObject]];
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/AppIcons"];
    NSString* path = [dataPath stringByAppendingPathComponent:fileName];
    UIImage* image = [UIImage imageWithContentsOfFile:path];
    if (!image) {

        UIApplication* app = [UIApplication sharedApplication];
        app.networkActivityIndicatorVisible = YES;
        // Create an array with the URL and name.  
        NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:fileName,imageURL, nil  ];  


        [self performSelectorInBackground:@selector(loadImageInBackground:)  withObject:arr]; 
        [arr release];


    }               
    else{
        photo.image=image;


    }
    NSString*promostext=[[NSString alloc] initWithFormat:@"promos:%@",[[(arrayResults*)[tweets objectAtIndex:indexPath.row] mypromos]stringValue] ];
    NSString*revenuetext=[[NSString alloc] initWithFormat:@"revenue:%@",[(arrayResults*)[tweets objectAtIndex:indexPath.row] myrevenue] ];
    NSString*updatestext=[[NSString alloc] initWithFormat:@"updates:%@",[[(arrayResults*)[tweets objectAtIndex:indexPath.row] myupdates]stringValue] ];

    mainLabel.text=[(arrayResults*)[tweets objectAtIndex:indexPath.row] myCountryName];
    promos.text=promostext;
    revenue.text=revenuetext;
    updates.text=updatestext;
    [promostext release];
    [revenuetext release];
    [updatestext release];

}

【问题讨论】:

  • 这很可能是您的 tableView:cellForRowAtIndexPath: 有问题。你能发布你的代码吗?
  • 剩下的呢?您实际上将文本放在标签中的什么位置,而将图像放在图像视图中的位置?
  • 对不起,我匆忙做了,我必须补充一下,我在另一个应用程序中使用相同的代码,我没有这个问题

标签: ios iphone uitableview cocoa-touch reload


【解决方案1】:

这通常是cellForRowAtIndexPath: 的回收/重用机制中的误用。

滚动时会重复使用单元格,以避免分配过多的 UITableViewCells。因此,在这两种情况下,您都需要在 cellForRowAtIndexPath: 中设置单元格的内容,无论是刚刚分配单元格 (cell==nil) 还是重复使用了单元格 (cell!=nil)。

【讨论】:

  • 那么为什么单元格 1-6 和 8-10 会加载而不是 7?我必须添加我在另一个应用程序中使用相同的代码,我没有这个问题
  • 正是出于我解释的原因。因为这些第一个单元格已分配并且您在 cell==nil 的情况下填充它们的内容(因为没有现有的 UITableViewCell 已经分配但未使用和可重用,所以 dequeueCellWithIdentifier 返回 nil),但是当您滚动时,那些单元格被重用(回收)以显示其他内容,因此在这种情况下,您将进入cell!=nil 情况,您的代码可能存在问题。在代码中设置断点,以便更好地了解代码中的哪条路径在两种情况下都执行。
  • 特别是您应该填写标签、图像视图等的内容(以及在一个单元格和另一个单元格之间发生变化的所有内容)if 语句之外,以填充单元格在这两种情况下,这些单元格都已被回收或刚刚分配。
猜你喜欢
  • 2012-05-25
  • 2021-03-21
  • 1970-01-01
  • 2011-07-27
  • 1970-01-01
  • 2012-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多