1

我做了一个tableViewController. 我想为第 1 个和第 7 个索引单元格提供不同的图像。cellForRowAtIndexPath这是我的委托方法中的代码片段。它在开始时正确初始化单元格,但是当我上下滚动几次时,它也开始在其他单元格的附件视图中给出“button4.png”。

                UIImage *indicatorImage;
            if(indexPath.row==0||indexPath.row==6)
            {
                indicatorImage = [UIImage imageNamed:@"button4.png"];
            }
            else
            {
                indicatorImage = [UIImage imageNamed:@"img-arrow.png"];
            }      

        cell.accessoryView =
        [[UIImageView alloc]
          initWithImage:indicatorImage];

这可能是什么原因?

该函数的完整代码有点乱,但我在这里发布:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    selection=self.tabBarController.selectedIndex;
    static NSString *CellIdentifier = @"Cell";
    UILabel *topLabel;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        UIImage *indicatorImage;



        if(indexPath.row==0||indexPath.row==6)
        {
            indicatorImage = [UIImage imageNamed:@"button4.png"];
        }
        else
        {
            indicatorImage = [UIImage imageNamed:@"img-arrow.png"];
        }      

    cell.accessoryView =
    [[UIImageView alloc]
      initWithImage:indicatorImage];

        cell.accessoryView =
        [[UIImageView alloc]
          initWithImage:indicatorImage];
        const CGFloat LABEL_HEIGHT = 20;



        topLabel =
        [[UILabel alloc]
          initWithFrame:
          CGRectMake(
                     2.0 * cell.indentationWidth,
                     0.5 * (tableView.rowHeight - 2 * LABEL_HEIGHT),
                     tableView.bounds.size.width -
                     4.0 * cell.indentationWidth
                     - indicatorImage.size.width,
                     LABEL_HEIGHT)];

        [cell.contentView addSubview:topLabel];

        topLabel.backgroundColor = [UIColor clearColor];
        topLabel.shadowColor = [UIColor whiteColor];
        topLabel.shadowOffset = CGSizeMake(0, 1);
        topLabel.textColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];
        topLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
        topLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];

        topLabel.tag=10;
        cell.backgroundView =
        [[UIImageView alloc] init];
        cell.selectedBackgroundView =
        [[UIImageView alloc] init];

    }
}
else
{
    topLabel = (UILabel *)[cell viewWithTag:10];
}

UIImage *rowBackground;
UIImage *selectionBackground;


rowBackground = [UIImage imageNamed:@"topAndBottomRow.png"];
selectionBackground = [UIImage imageNamed:@"topAndBottomRowSelected.png"];


    if(indexPath.row==0||indexPath.row==6)
    {

        rowBackground = [UIImage imageNamed:@"topAndBottomRowSelected.png"];
        selectionBackground = [UIImage imageNamed:@"topAndBottomRowSelected.png"];

        topLabel.textColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];
        topLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
        topLabel.font = [UIFont systemFontOfSize:20];
    }


((UIImageView *)cell.backgroundView).image = rowBackground;
((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;


NSString *object = _objects[indexPath.row];
NSString *str=[object description];
topLabel.text=[NSString stringWithFormat:@"         %@",str];

return cell;

}

4

2 回答 2

2

您在if (cell == nil) ...创建和配置单元的块中设置此图像。但是您需要将此图像设置移到该if块之外,因为如果正在重用单元格(即您不必allocinit),旧图像将被重用。您应该查看该if语句中的任何内容,以了解应该逐个单元格更改的内容,并将其移到if块之外。

于 2013-03-18T15:53:17.377 回答
0

当滚动让你像这样混乱时,它表明你没有完全初始化REUSED UITableViewCell 的内容。虽然您的代码片段说您确实将其设置为某些东西 - 您的结果表明您不是。你可以找那个。

例如,在获取单元格后立即尝试将附件视图设置为 nil 并查看问题是否消失。

于 2013-03-18T15:54:25.660 回答