图像在表视图单元格中重叠

时间:2012-02-09 08:14:33

标签: objective-c ios cocoa-touch

我正在解析一个json feed并填充我的表视图。如果我的解析值是“1”,如果它对于表格的前四行,那么cell.i必须创建一个绿色的复选标记,如果它不是第一行四行复选标记是灰色check.if我的解析值是null.i得到没有checkmark.everthng工作正常。但如果我滚动我的表视图。复选标记图像被重叠。下面是屏幕截图和代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

//[tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

}
NSDictionary *boy=[self.media1 objectAtIndex:indexPath.row];    
NSString *str=[[NSString alloc]initWithFormat:@"%@",boy];

if ([str isEqualToString:@"1"]) 
{
    if(indexPath.row==0||indexPath.row==1||indexPath.row==2||indexPath.row==3)
    {
    CGRect starFrame = CGRectMake(260, 18, 50, 40);

    UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
    starImage.image = [UIImage imageNamed:@"tic.png"];
    [cell.contentView addSubview:starImage];
    }
    else  
    {
    CGRect starFrame1 = CGRectMake(260, 18, 50, 40);

    UIImageView *starImage1 = [[[UIImageView alloc] initWithFrame:starFrame1] autorelease];
    starImage1.image = [UIImage imageNamed:@"brrr.png"];
    [cell.contentView addSubview:starImage1];

    }


}    

cell.textLabel.text=[story objectAtIndex:indexPath.row];

        return cell;
   }

enter image description here

2 个答案:

答案 0 :(得分:1)

您正在重复使用该单元格。因此,一旦它们不可见,tableView将返回使用过的单元格。

为避免此问题,您应该在代码下面。

if ([str isEqualToString:@"1"]) 
{
    if(indexPath.row==0||indexPath.row==1||indexPath.row==2||indexPath.row==3)
    {
    CGRect starFrame = CGRectMake(260, 18, 50, 40);

    UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
    starImage.image = [UIImage imageNamed:@"tic.png"];
    starImage.tag = 1000;
    [cell.contentView addSubview:starImage];
    }
    else  
    {
    CGRect starFrame1 = CGRectMake(260, 18, 50, 40);

    UIImageView *starImage1 = [[[UIImageView alloc] initWithFrame:starFrame1] autorelease];
    starImage1.image = [UIImage imageNamed:@"brrr.png"];
    starImage.tag = 1000;
    [cell.contentView addSubview:starImage1];

    }


}  
else
{
UIImageView *starImage1 = [cell.contentView viewWithTag:1000];
starImage1.image = nil;
}

答案 1 :(得分:0)

使用以下代码

if ([str isEqualToString:@"1"]) 
{
    if(indexPath.row==0||indexPath.row==1||indexPath.row==2||indexPath.row==3)
    {
        CGRect starFrame = CGRectMake(260, 18, 50, 40);

        UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
        starImage.image = [UIImage imageNamed:@"tic.png"];
        starImage.tag = 1000;
        [cell.contentView addSubview:starImage];
    }
    else  
    {
        CGRect starFrame1 = CGRectMake(260, 18, 50, 40);

        UIImageView *starImage1 = [[[UIImageView alloc] initWithFrame:starFrame1] autorelease];
        starImage1.image = [UIImage imageNamed:@"brrr.png"];
        starImage1.tag = 1000;
        [cell.contentView addSubview:starImage1];

    }

}   
else
{
    UIImageView *starImage = [cell.contentView viewWithTag:1000];
    if (starImage) {
        [starImage removeFromSuperview];
    }
}