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する必要がない場合)、古い画像が再利用されるため、この画像設定をそのブロックの外に移動する必要があります。セルごとに変更する必要があるものについて、そのステートメント内のすべてを確認し、それをブロックの外に移動する必要があります。allocinitifif

于 2013-03-18T15:53:17.377 に答える
0

スクロールすると、このように混乱する場合は、REUSEDUITableViewCellのコンテンツを完全に初期化していないことを示しています。あなたのコードスニペットはあなたがそれを何かに設定することを示していますが、あなたの結果はあなたがそうではないことを示唆しています。あなたはそれを探すことができます。

たとえば、セルを取得した直後にaccessoryViewをnilに設定して、問題が解決するかどうかを確認してください。

于 2013-03-18T15:54:25.660 に答える