我有一个UITableViewCell加载10个图像,它加载前6个,然后它说(单元!= nil)所以它不加载剩余的图像,但如果我删除“if(cell == nil)”它加载所有图像 我错过了什么吗?
感谢
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
AsyncImageView *asyncImageView = nil;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
int section = indexPath.section;
NSMutableArray *sectionItems = [sections objectAtIndex:section];
if (cell == nil) {
NSLog(@"CELL == NIL %@", cell);
int n = ([sectionItems count]) ;
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
int i=0,f=0;
while (i < n)
{
int yy = 4 +f*74;
for(int j=0; j<3;j++){
Item *item = [sectionItems objectAtIndex:i];
int buttonX = 10;
if (i == 0) {
buttonX = 10;
}else {
buttonX = 203
}
CGRect frame;
frame.origin.x = buttonX;
frame.origin.y = yy;
frame.size.width = 107;
frame.size.height = 107;
asyncImageView = [[[AsyncImageView alloc] initWithFrame:frame] autorelease];
asyncImageView.tag = ASYNC_IMAGE_TAG;
NSString *urlString = item.image;
NSURL *url = [NSURL URLWithString:urlString];
[asyncImageView loadImageFromURL:url];
[cell.contentView addSubview:asyncImageView];
i++;
}
f = f+1;
}
}
else {
NSLog(@"cell != nill");
asyncImageView = (AsyncImageView *) [cell.contentView viewWithTag:ASYNC_IMAGE_TAG];
}
return cell;
}
答案 0 :(得分:0)
UITableViewCells通常被重新用于节省时间和内存,而且你正在使用我看到的这个范例。您需要实现cell!= nil case并设置单元格以显示基于indexPath的内容。该单元格以前用于显示其他内容,因此您需要调整非零单元格中所有与索引路径相关的视图。
答案 1 :(得分:0)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString * CellIdentifier = @“Cell”; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(cell == nil){ NSLog(@“CELL == NIL%@”,单元格); cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; cell.accessoryType = UITableViewCellAccessoryNone; cell.selectionStyle = UITableViewCellSelectionStyleNone; CGRect frame = CGRectMake(0,20,30,40); UIImageView * img = [[[UIImageView alloc] initWithFrame:frame]; img.tag = IMAGE_TAG; [cell.contentView addSubview:img]; [img release]; } int row = indexPath.row; UIImageView * myImage = =(UIImageView *)[cell.contentView viewWithTag:IMAGE_TAG]; UIImage * image = [UIImage imageNamed:[NSString stringWithFormat:@“%d”,row]]; [myImage setImage:image]; 返回单元格;}
UITableViewCells被缓存以节省内存和分配成本(因此性能)。您只需创建一个单元格,然后根据索引路径设置内容。我只是在单元格中创建了一个imageview,然后根据该单元格中的索引路径设置了一个图像(1.png,2.png等)。
阅读文档here。