2009-08-25 13 views
6

私の問題と似ているが、全く同じではない投稿がいくつか見つかりました。uitableviewcellのtableviewの重複行

私のアプリでは、ユーザーはいくつかのuitableviews間を移動して、希望の結果にドリルダウンすることができます。ユーザーが前進、後進、後進などをすると、行が再描画され、書き直され、テキストが大胆で大胆になります。

投稿の中には、cellforrowatindexpathメソッドの中で、私が行を作成する方法に関連するものがあることがわかりました。

ユーザーがテーブルビュー間を前後に移動するたびに行が再作成/再描画されないようにするために必要なことはありますか?下のコードに何かを追加するか、viewwillappearメソッドに何かを追加する必要がありますか(現在、テーブルのビューに「reloaddata」がありますが、役立たないようです)?ここで

は私のコードです:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
} 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

    UILabel *label = [[[UILabel alloc] init] autorelease]; 
    label.font = [UIFont fontWithName:@"Arial-BoldMT" size:20]; 
    label.frame = CGRectMake(10.0f, 10.0f, 220.0f, 22.0f); 
    label.textColor = [UIColor blackColor]; 
    label.backgroundColor = [UIColor clearColor]; 
    label.opaque = NO; 
    label.text = [mapareaArray objectAtIndex:indexPath.row]; 
    [cell.contentView addSubview:label]; 

    CustomCellBackgroundView *bgView = [[CustomCellBackgroundView alloc] initWithFrame:CGRectZero]; 
    bgView.borderColor = [UIColor clearColor]; 
    bgView.fillColor = [UIColor whiteColor]; 
    bgView.position = CustomCellBackgroundViewPositionSingle; 
    cell.backgroundView = bgView; 
    return cell; 
} 

答えて

11

あなたが持っている問題は、このラインによるものです:

[cell.contentView addSubview:label]; 

あなたはそれが新しいセルだかどうか、テーブルセルにサブビューを追加します。古いセル(再利用可能なプールからデキューされている)の場合は、セルにさらに別のサブビューを追加します。

代わりに、UILabelにタグを付けて、そのUILabelの内容を変更するタグを指定する必要があります。追加(およびそのすべての属性を設定する)とIF(セル== nilの)ブロック内UILabelにタグを付ける:

if(cell == nil) { 
    // alloc and init the cell view... 

    UILabel *label = [[[UILabel alloc] init] autorelease]; 
    label.tag = kMyTag; // define kMyTag in your header file using #define 
    // and other label customizations 
    [cell.contentView addSubview:label]; // addSubview here and only here 
    ... 
} 

その後でそれを探します。

UILabel *label = (UILabel *)[cell.contentView viewWithTag: kMyTag]; 
label.text = [mapareaArray objectAtIndex:indexPath.row]; 

そして再度追加する必要はありませんそれはif(cell == nil)ブロックの外側のサブビューです。サブビューは既に存在しています(セルビューを再利用するのは、それを正しく行うと効率的です)。

+0

ありがとうございます。大きな助け。 これについての最後の質問です。プログラミングのバックグラウンドはありませんので、ヘッダーファイルで#defineを使用することで何を意味するのかはわかりません。これを行うための構文は何ですか?私は前にそれを見て、ヘッダーに#define kMyTagを入れようとしましたが、これはうまくいきません...私はあなたが値にkMyTagを定義する必要があると仮定していますが、これを行うための構文が何であるかはわかりません。手伝ってくれますか? – SKayser

1

.hファイル 「の定義は、」ヘッダファイルの先頭にあるの#importステートメントの後に置かれ、そして、私はそれを定義する方法を他に知らないので0と置いた。

#define kMyTag 0 

。 mファイル あなたのコメントに従ってこのセクションを更新しましたが、a)テーブルにデータが入力されていない、b)ユーザが次のビューにナビゲートしてこのビューに戻ると、 "インスタンスに送られた認識できないセレクタ" 、そしてc)私は2つの「リターンセル」に入れなければならなかった。エントリまたはそれは転倒します。私は間違った順序で物事を持っていて、物事を正しく初期化していないと思いますか?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 

     UILabel *label = [[[UILabel alloc] init] autorelease]; 
     label.tag = kMyTag; // define kMyTag in your header file using #define 
     label.font = [UIFont fontWithName:@"Arial-BoldMT" size:20]; 
     label.frame = CGRectMake(10.0f, 10.0f, 220.0f, 22.0f); 
     label.textColor = [UIColor blackColor]; 
     label.backgroundColor = [UIColor clearColor]; 
     label.opaque = NO; 

     CustomCellBackgroundView *bgView = [[CustomCellBackgroundView alloc] initWithFrame:CGRectZero]; 
     bgView.borderColor = [UIColor clearColor]; 
     bgView.fillColor = [UIColor whiteColor]; 
     bgView.position = CustomCellBackgroundViewPositionSingle; 
     cell.backgroundView = bgView; 

     [cell.contentView addSubview:label]; // addSubview here and only here 
     return cell; 
    } 
UILabel *label = (UILabel *)[cell.contentView viewWithTag: kMyTag]; 
label.text = [mapareaArray objectAtIndex:indexPath.row]; 
return cell; 
} 
+0

kMyTagの値に0を使用するかどうかはわかりません。 1から始めると、0がすべてのデフォルトになる可能性があります。その変更の後、ifブロック内の戻りセルを削除します。 – tchen

+0

もう一度ありがとう!それは今働きます!あなたの時間を感謝します! – SKayser

0

あなたが自分自身にするたびにサブビューを追加する必要があるかどうかを確認するためのロジックを適用し、その後、その特定のセルのためのUITableViewCellのためのサブクラスを作成する必要があります。また、UITableviewcellのprepareForReuseを使用し、その時間中にサブビューを削除してから、UILabelをセルに追加するロジックを適用してください。

関連する問題