0

TableViewCellの作成時にUILabelを追加しました。このようなコード:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Special *special = [speciales objectAtIndex:indexPath.row];
    ......
    UILabel *description = [[UILabel alloc] initWithFrame:CGRectMake(80, 21, 220, 50)];
    description.text = special.specialDescription;
    description.font = [UIFont fontWithName:@"Heiti SC" size:12];
    description.textColor = [UIColor darkGrayColor];
    description.lineBreakMode = UILineBreakModeWordWrap;
    description.numberOfLines = 3;

    [cell addSubview:description];

    return cell;
}

うまく機能しますが、下から上にスクロールしたとき、および行を選択したときに、古い値が同時に表示されました。誰が私がこれを修正するのを手伝うことができますか?

ありがとうございました!

更新:私のコードはすべてこれです:-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

Special *special = [speciales objectAtIndex:indexPath.row];

static NSString *CellIdentifier = @"Cell";

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

UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 60, 60)];
img.image = special.specialIconImage;

[self addShadowToImage:img];

[cell addSubview:img];

UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(80, 5, 220, 16)];
name.text = special.specialName;

name.font = [UIFont fontWithName:@"Heiti SC" size:16];

[cell addSubview:name];

UILabel *description = [[UILabel alloc] initWithFrame:CGRectMake(80, 21, 220, 50)];
description.text = special.specialDescription;
description.font = [UIFont fontWithName:@"Heiti SC" size:12];
description.textColor = [UIColor darkGrayColor];
description.lineBreakMode = UILineBreakModeWordWrap;
description.numberOfLines = 3;

[cell addSubview:description];

return cell;

}

4

5 に答える 5

1

問題は、セルが呼び出されるたびに画像とラベルのサブビューを追加していることです。代わりに、セルを作成するときにのみこれらのサブビューを追加する必要があります。セルが呼び出されるたびに、サブビューの値を設定するだけです。あなたはこのようなものが欲しいでしょう(メモリから実行されます):

Special *special = [speciales objectAtIndex:indexPath.row];

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 60, 60)];
    [imageView setTag:100];
    [cell addSubview:imageView];

    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 5, 220, 16)];
    name.font = [UIFont fontWithName:@"Heiti SC" size:16];
    [nameLabel setTag:101];
    [cell addSubview:nameLabel];

    UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 21, 220, 50)];
    [descriptionLabel setTag:102];
    descriptionLabel.font = [UIFont fontWithName:@"Heiti SC" size:12];
    descriptionLabel.textColor = [UIColor darkGrayColor];
    descriptionLabel.lineBreakMode = UILineBreakModeWordWrap;
    descriptionLabel.numberOfLines = 3;
    [cell addSubview:descriptionLabel];
}

UIImageView *img = (UIImageView *)[cell viewWithTag:100];
img.image = special.specialIconImage;
[self addShadowToImage:img];

UILabel *name = (UILabel *)[cell viewWithTag:101];
name.text = special.specialName;

UILabel *description = (UILabel *)[cell viewWithTag:102];
description.text = special.specialDescription;

return cell;
于 2012-04-17T16:27:18.217 に答える
0

[tableView reloadData]; これを試して。

于 2012-04-17T11:01:38.137 に答える
0

コードで.....はどういう意味ですか?

そこでdequeueReusableCellWithIdentifierを使用していますか?

コードのその部分が以下のようなコードを含む場合、それはうまく機能するはずです-

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if(cell == nil)
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
于 2012-04-17T11:09:35.027 に答える
0

セルを再利用するにはセルをデキューする必要があると思いますが、セルを再利用して表示されない限り、問題の原因ではないようです。問題を指摘するコードがそこにある可能性があります。

この問題は再描画に関連している可能性があると思います。tableView:cellForRowAtIndex:メソッドの最後にこれを入れてみてください

[cell setNeedsDisplay];

これにより、UIはセルを再描画します。

于 2012-04-17T11:22:33.513 に答える
0

覚えておくべきことがいくつかあります。

  • The new views must be added when we instantiate a new cell, but not when we
    reuse a cell (because a reused cell already has them).

  • We must never send addSubview: to the cell itself — only to its contentView (or
    some subview thereof).

  • We should assign the new views an appropriate autoresizingMask, because the
    cell’s content view might be resized.

  • Each new view should be assigned a tag so that it can be referred to elsewhere.   

コードは次のとおりです。

//thanks to Thomas Hajcak's code
Special *special = [speciales objectAtIndex:indexPath.row];

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 60, 60)];
[imageView setTag:100];


//autoresizingMask as follow
imageView.autoresizingMask = (UIViewAutoresizingFlexibleHeight |
                           UIViewAutoresizingFlexibleLeftMargin);
[cell.contentView addSubview:imageView];

UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 5, 220, 16)];
name.font = [UIFont fontWithName:@"Heiti SC" size:16];
[nameLabel setTag:101];

[cell.contentView addSubview:nameLabel];

UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 21, 220, 50)];
[descriptionLabel setTag:102];
descriptionLabel.font = [UIFont fontWithName:@"Heiti SC" size:12];
descriptionLabel.textColor = [UIColor darkGrayColor];
descriptionLabel.lineBreakMode = UILineBreakModeWordWrap;
descriptionLabel.numberOfLines = 3;
[cell.contentView addSubview:descriptionLabel];
}

UIImageView *img = (UIImageView *)[cell.contentView viewWithTag:100];
img.image = special.specialIconImage;
[self addShadowToImage:img];

UILabel *name = (UILabel *)[cell.contentView viewWithTag:101];
name.text = special.specialName;

UILabel *description = (UILabel *)[cell.contentView viewWithTag:102];
description.text = special.specialDescription;

return cell;
于 2012-04-18T05:47:19.737 に答える