UIImageView附加在Uitableviewcell中仅适用于偶数行
我用uiimage创建uitableview自定义单元格我只想为偶数行添加一个图像,但它不起作用这里是代码UIImageView附加在Uitableviewcell中仅适用于偶数行,uitableview,uiimageview,uiimage,Uitableview,Uiimageview,Uiimage,我用uiimage创建uitableview自定义单元格我只想为偶数行添加一个图像,但它不起作用这里是代码 UITableViewCell *cell; UILabel *label = nil; cell = [tv dequeueReusableCellWithIdentifier:@"Cell"]; if (cell == nil) { //label information } NS
UITableViewCell *cell;
UILabel *label = nil;
cell = [tv dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil)
{
//label information
}
NSString *text = [items objectAtIndex:[indexPath row]];
UIView *selectionView = [[UIView alloc]initWithFrame:cell.bounds];
[selectionView setBackgroundColor:[UIColor colorWithRed:0 green:1 blue:1 alpha:1]];
cell.selectedBackgroundView = selectionView;
if (indexPath.row % 2==0) {
UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(128,5, 32, 32)];
imv.image=[UIImage imageNamed:@"England.png"];
[cell.contentView addSubview:img];
[imv release];
}
else {
// ....
}
return cell;
在第3行之后,如果我尝试滚动“所有行都有一个图像”,则所有行都将使用此图像。重复使用单元格时,您的单元格将被重复使用,并且UIImageView不会被删除。在else语句中,您需要添加代码来删除带有图像的UIImageView。现在,您似乎没有保留对UIImageView的引用,因此不能只调用
removeFromSuperview
。在else语句中,您可以尝试枚举cell.contentView的所有子视图,并删除任何UIImageView:
for (UIView *theSubview in [cell.contentView subviews])
{
if(theSubview isKindOfClass:[UIImageView class])
{
[theSubview removeFromSuperview];
}
}
请注意,我还没有测试这段代码