UIimageview在uitableviewcell中显示多次

时间:2016-05-18 09:15:24

标签: ios objective-c xcode uitableview uiimageview

我的应用程序中有一个表正在加载xml菜单。并且我在其中为每个单元格放置了uiimageview作为子弹图像。但我的问题是第一次表显示正确(一个单元格中的一个图像)但是当我在菜单中进一步向下滚动时。它开始在一个单元格中显示图像3次。我会发布我的代码。

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"menuCell";
    UITableViewCell *cell = (UITableViewCell *)[theTableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-bold" size:18];
    if ([[[rssOutputData objectAtIndex:indexPath.row]xmlid] isEqualToString:@"1"]) {
        cell.textLabel.textColor = [UIColor redColor];
        [cell setIndentationLevel:3];
        [cell setIndentationWidth:10];
        cellImage = [[UIImageView alloc]initWithFrame:CGRectMake(5,cell.frame.size.height/2+5, 15, 15)];

    }else if ([[[rssOutputData objectAtIndex:indexPath.row]xmlid] isEqualToString:@"2"]) {
        cell.textLabel.textColor = [UIColor blueColor];
        [cell setIndentationLevel:6];
        [cell setIndentationWidth:10];
        cellImage = [[UIImageView alloc]initWithFrame:CGRectMake(35,cell.frame.size.height/2+5, 13, 13)];

    }else if ([[[rssOutputData objectAtIndex:indexPath.row]xmlid] isEqualToString:@"3"]) {
        cell.textLabel.textColor = [UIColor lightGrayColor];
        [cell setIndentationLevel:9];
        [cell setIndentationWidth:10];
        cellImage = [[UIImageView alloc]initWithFrame:CGRectMake(65,cell.frame.size.height/2+5, 11, 11)];
    }
    cell.textLabel.text = [[rssOutputData objectAtIndex:indexPath.row]xmltitle];

    cellImage.image=[UIImage imageNamed:@"arrow.png"];
    [cell addSubview:cellImage];


    return cell;
}

这是实际发生的事情。 enter image description here

6 个答案:

答案 0 :(得分:0)

如果您在故事板中创建storyboard,请仔细检查tableView中的 [cell.contentView addSubview:MyImageView];

你的细胞被重复使用,这就是你看到这种行为的原因。

不要在Cell中添加Image而不是在ContentView中添加Image。

{{1}}

答案 1 :(得分:0)

因为这行

[cell addSubview:cellImage];

滚动时,会调用cellForRowAtIndexPath,并将图像视图添加到表格视图单元格中。

要解决此问题,可以采取一些解决方案:

  • 在layoutSubViews中编写自定义单元格类,初始图像视图一次, 并且只改变cellForRowAtIndexPath中的图像内容。

  • 在自定义单元格xib中创建图像视图,并为自定义添加插座 细胞类

答案 2 :(得分:0)

用此代码替换您的代码,

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"menuCell";
    UITableViewCell *cell = (UITableViewCell *)[theTableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

        UIImageView *cellImage = [[UIImageView alloc]initWithFrame:CGRectZero];
        [cellImage setTag:100];
        [cell.contentView addSubview:cellImage];
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-bold" size:18];

    if ([[[rssOutputData objectAtIndex:indexPath.row]xmlid] isEqualToString:@"1"]) {
    cell.textLabel.textColor = [UIColor redColor];
    [cell setIndentationLevel:3];
    [cell setIndentationWidth:10];
    [cellImage setFrame:CGRectMake(5,cell.frame.size.height/2+5, 15, 15)];

}else if ([[[rssOutputData objectAtIndex:indexPath.row]xmlid] isEqualToString:@"2"]) {
    cell.textLabel.textColor = [UIColor blueColor];
    [cell setIndentationLevel:6];
    [cell setIndentationWidth:10];
    [cellImage setFrame:CGRectMake(35,cell.frame.size.height/2+5, 13, 13)];

}else if ([[[rssOutputData objectAtIndex:indexPath.row]xmlid] isEqualToString:@"3"]) {
    cell.textLabel.textColor = [UIColor lightGrayColor];
    [cell setIndentationLevel:9];
    [cell setIndentationWidth:10];
    [cellImage setFrame:CGRectMake(65,cell.frame.size.height/2+5, 11, 11)];
}

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

    cellImage.image=[UIImage imageNamed:@"arrow.png"];


    return cell;
}

答案 3 :(得分:0)

分配imageView一次并将tag提供给imageView并使用该标记来显示图片和 使用方法:

[cell.contentView addSubview:MyImageView];

你的问题将得到解决

答案 4 :(得分:0)

在您的方法中添加cellImage = nil;作为第一行,您的问题就会得到解决。

更新:

尝试在contentview的单元格中添加cellImage而不是像<,p>这样的单元格

[cell.contentView addSubview: cellImage];

希望这会有所帮助:)

答案 5 :(得分:0)

请在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

中添加此行
cell.clipsToBounds=YES;
相关问题