2011-12-14 8 views
0

UITableViewの最初の行にいくつかのUILabelsを挿入しようとしていますが、予期しない結果が出ています。UITableViewの行0をカスタマイズする

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

if (cell == nil) 
    cell = [[UITableViewCell alloc[ ..... 

cell.textLabel.text = @""; 

if (indexPath.row == 0) { 
    cell.backgroundColor = .... 
    UILabel* x = ..... 
    [x setTag:1]; 
    [cell insertSubview:x]; 
} 
else { 
    cell.textLabel.text = .... 
    for (UIView* view in cell.subviews) { 
     if (view.tag == 1) 
      [view removeFromSuperview]; 
    } 
} 

return cell 

}

とにかく要点です:簡潔にするため、ここでの要約です。問題は、背景色が正しく設定されている間に、いつでも私がテーブルビューをスクロールすると、挿入したカスタムUILabelが消えることです。 dequeueReusableCellWithIdentifierの誤解が原因かもしれませんが、関係なく、私はラベルが再挿入されると思います。

+0

のサブクラスを使用することになりすぎ異なる場合:cellForRowAtIndexPath:'ほとんどの動作します時間は、それを行う*公式の方法は['-tableView:willDisplayCell:forRowAtIndexPath:'](http://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/Reference/Reference .html#// apple_ref/occ/intfm/UITableViewDelegate/tableView:willDisplayCell:forRowAtIndexPath :)。 –

+0

'insertSubview'?それはあなたのコードの一例にすぎませんが、正確であるのに役立ちます。実際のコードをコピー&ペーストするだけで、何が起こっているのかを正確に確認できます。セルの 'contentView'ビューをセルにまっすぐに追加する必要もあります –

答えて

4

セルを要求するたびにたくさんのビューを作成、追加、削除するのではなく、異なるセル識別子を使用してセルが作成されたときに完全に初期化することをお勧めします。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString* normalCellIdentifier = @"Normal Cell"; 
    static NSString* specialCellIdentifier = @"Special Cell"; 

    if (indexPath.row == 0) { 
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:specialCellIdentifier]; 

    if (cell == nil){ 
     cell = [[UITableViewCell alloc[ ..... 
     cell.backgroundColor = .... 
     UILabel* x = ..... 
     [cell insertSubview:x]; 
    } 

    return cell 
    } else { 
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:normalCellIdentifier]; 

    if (cell == nil) 
     cell = [[UITableViewCell alloc[ ..... 
    cell.textLabel.text = .... 
    return cell; 
    } 
} 
1

ラベルなどの単純なものを追加したい場合は、別のセルクラスは必要ありません。

これを試してみてください:

/* ARC code */ 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    if (0 == indexPath.row) { 
     cell.textLabel.text = @"Zero"; 
     UILabel *lbl = [[UILabel alloc] initWithFrame:(CGRect){.size={80, 20}}]; 
     lbl.backgroundColor = [UIColor redColor]; 
     lbl.textColor = [UIColor blackColor]; 
     lbl.text = @"@@@@"; 
     [cell addSubview:lbl]; 
    } else { 
     cell.textLabel.text = @"Not zero"; 
    }  
    return cell; 
} 

その後、別の場所についてdequeueReusableCellWithIdentifier:読み。

また、同様のケースではUIView- (UIView *)viewWithTag:(NSInteger)tag があり、そのサブクラスは実装する必要はありません。

1

システムの作業を減らすために、ビューのインスタンス化を最小限に抑えて最小限に抑えることをお勧めします。

@EmilioPelaezには1つの解決策がありますが、唯一の欠点は、コードが繰り返しと複数の出口でかなり醜いことです。あなたが重い物を持ち上げるを行うと、一般的な形でテーブルビューのセルを設定するところif (!cell) { ... }間のビットのような、それの

私はずっと幸せ

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

    const NSInteger tag = 1; 

    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 
             reuseIdentifier:reuseIdentifier] autorelease]; 
     UILabel *label = [[UILabel alloc] initWithFrame:someFrame]; 
     label.hidden = YES; 
     label.tag = tag; 
     [cell.contentView addSubview:label]; 
     [label release]; label = nil; 
    } 

    cell.textLabel.text = nil; 
    UILabel *label = [cell.contentView viewWithTag:tag]; 

    if (0 == indexPath.row) { 
     cell.backgroundColor = [UIColor greenColor]; 
     label.hidden = NO; 
    } else { 
     cell.backgroundColor = [UIColor redColor]; 
     cell.textLabel.text = @"Some text"; 
     label.hidden = YES; 
    } 

    return cell; 
} 

このようにそれをやっていると思うがあります。

その後、ビューコンポーネントを設定するだけです。

細胞は、その後、私はおそらく別の再利用識別子と一緒に行く考えると、おそらく `-tableViewでセルの背景色を設定しながらUITableViewCellまたはxib

関連する問題