在iPhone中将图像添加到表视图时出现问题
我有下面的代码来创建单元格并向其中添加图像在iPhone中将图像添加到表视图时出现问题,iphone,uitableview,uiimageview,Iphone,Uitableview,Uiimageview,我有下面的代码来创建单元格并向其中添加图像 UIImageView* MyImage =[[UIImageView alloc]initWithFrame:CGRectMake(100,10,40,40)]; [MyImage setImage:[UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]]]; [MyImage setBackgroundColor:[UIColor clearColor]]; [cell
UIImageView* MyImage =[[UIImageView alloc]initWithFrame:CGRectMake(100,10,40,40)];
[MyImage setImage:[UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]]];
[MyImage setBackgroundColor:[UIColor clearColor]];
[cell addSubview:MyImage];
[MyImage release];
cell.textLabel.text=[dataArray objectAtIndex:indexPath.row];
return cell;
我使用这张桌子有很多目的,因此一次又一次地刷新它。。。。
因此,下一个图像与发生问题的最后一个图像重叠,并且这两个图像都可见(我为每个图像使用透明背景)。。。。当我不需要图像时会出现其他问题。在这里,我无法删除最后一个图像
请在您使用的位置提供帮助
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:@“MyIdentifier”];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithFrame:CGRectZero重用标识符:@“MyIdentifier”]自动释放];
}
将此修改为
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:@“MyIdentifier”];
cell=[[UITableViewCell alloc]initWithFrame:CGRectZero重用标识符:@“MyIdentifier”]自动释放]
这是一个解决方案,它总是将新框架添加到旧的现有单元
或
将背景色从“清晰颜色”中删除为某种颜色,每次添加到图像时,都可以选择默认图像或特定图像。标签也是如此。当您不想向标签添加任何文本时,也可以添加文本=@“ 在哪里使用
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:@“MyIdentifier”];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithFrame:CGRectZero重用标识符:@“MyIdentifier”]自动释放];
}
将此修改为
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:@“MyIdentifier”];
cell=[[UITableViewCell alloc]initWithFrame:CGRectZero重用标识符:@“MyIdentifier”]自动释放]
这是一个解决方案,它总是将新框架添加到旧的现有单元
或
将背景色从“清晰颜色”中删除为某种颜色,每次添加到图像时,都可以选择默认图像或特定图像。标签也是如此。当您不想向标签添加任何文本时,也可以添加文本=@“ 您可能知道,当您从“dequeueReusableCellWithIdentifier”重用单元时,它会返回您现有的单元实例(如果存在),这意味着如果单元存在,则其数据也存在,即图像,因此您需要在使用新数据更新单元之前清除旧图像,就像新数据没有图像一样,然后它将显示旧图像,我想你明白了 好的,下面是解决方案:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell==nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewStyleDefault reuseIdentifier:cellIdentifier];
UIImageView* MyImage =[[UIImageView alloc]initWithFrame:CGRectMake(100,10,40,40)];
MyImage.tag = 1000;
[MyImage setBackgroundColor:[UIColor clearColor]];
[cell addSubview:MyImage];
[MyImage release];
}
UIImageView *imgView = (UIImageView*)[cell viewWithTag:1000];
imgView.image = nil;
imgView.image = [UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]];
您可能知道,当您从“dequeueReusableCellWithIdentifier”重用单元时,它会返回您现有的单元实例(如果存在),这意味着如果单元存在,则其数据也存在,即图像,因此您需要在使用新数据更新单元之前清除旧图像,就像新数据没有图像一样,然后它将显示旧图像,我想你明白了 好的,下面是解决方案:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell==nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewStyleDefault reuseIdentifier:cellIdentifier];
UIImageView* MyImage =[[UIImageView alloc]initWithFrame:CGRectMake(100,10,40,40)];
MyImage.tag = 1000;
[MyImage setBackgroundColor:[UIColor clearColor]];
[cell addSubview:MyImage];
[MyImage release];
}
UIImageView *imgView = (UIImageView*)[cell viewWithTag:1000];
imgView.image = nil;
imgView.image = [UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]];
UIImageView*imgView=[带标记的单元格视图:1000];这行给出的错误是==单元格可能不响应viewWithTag哦,它的“viewWithTag”不是“viewWithTag”,很抱歉,但我的程序仍然crshes我使用了返回单元格的所有代码;添加到它,但它再次崩溃。您尝试过吗?@Sanniv它也不能与[MyImage setBackgroundColor:[UIColor clearColor]]一起使用;因为旧图像也会与此一起显示。@Ranjeet这可以与[MyImage setBackgroundColor:[UIColor clearColor]]一起使用;将此行更改为[MyImage setBackgroundColor:[UIColor whiteColor]];它将帮助您UIImageView*imgView=[带标记的单元格视图:1000];这行给出的错误是==单元格可能不响应viewWithTag哦,它的“viewWithTag”不是“viewWithTag”,很抱歉,但我的程序仍然crshes我使用了返回单元格的所有代码;添加到它,但它再次崩溃。您尝试过吗?@Sanniv它也不能与[MyImage setBackgroundColor:[UIColor clearColor]]一起使用;因为旧图像也会与此一起显示。@Ranjeet这可以与[MyImage setBackgroundColor:[UIColor clearColor]]一起使用;将此行更改为[MyImage setBackgroundColor:[UIColor whiteColor]];这将有助于YHI SHIU,它管理单元格内容的错误方式,如果你考虑正确的内存管理,你能理解DeQueReReababeleListIdvor到底是做什么的,它避免了不需要的小区分配。同时让现有单元格实例更新内容,那么为什么要再次分配单元格,它也会出现内存泄漏。@Ranjeet我知道这些解决方案,肯定还有其他解决方案存在。@Sanniv,你是对的,这不是正确的方法。(这是一个糟糕的解决方案,但可能是一个解决方案)另外,我还提出了另一种解决方案。这也是可行的。嘿,Ishu,它管理单元格内容的错误方式,如果你考虑正确的内存管理,你能理解DeQueReReababeleListIdHub到底做了什么吗?它避免了不需要的小区分配。同时让现有单元格实例更新内容,那么为什么要再次分配单元格,它也会出现内存泄漏。@Ranjeet我知道这些解决方案,肯定还有其他解决方案存在。@Sanniv,你是对的,这不是正确的方法。(这是一个糟糕的解决方案,但可能是一个解决方案).我还提出了另一个解决方案。这也会奏效的。