2

UITableView を操作する通常の状況では、古いセルを再利用するための標準コードがあります。

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    return cell;
}

ただし、セルにサブビューを追加すると、サブビューが削除されず、毎回新しいビューが追加されることに気付きました。以下に、それを完全に示す例を示します。

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

    UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

    UILabel *label = [[UILabel alloc] init];
    label.text = @"HELLO";
    label.frame = CGRectMake(arc4random() % 50, -1, 286, 45);
    label.backgroundColor = [UIColor clearColor];

    // Add views
    [cell addSubview:label];

    return cell;
}

セルが再利用されるのと同じ方法でラベルを再利用するコードが必要です。私は何をすべきか?

ありがとう

4

4 に答える 4

4

新しいセルを作成する場合にのみ、サブビューを追加する必要があります。デキューする場合、サブビューはすでに存在するため、再作成しないでください。

あなたの方法は次のとおりです。

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

    UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:cellIdentifier]; 
    UILabel *label;
    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
        label = [[UILabel alloc] init];
        label.tag = 1;
        // Add views 
        [cell addSubview:label];
    }
    else
    {
        // Label will already exist, get a pointer to it
        label = [cell viewWithTag:1];
    }

    // Now set properties on the subview that are unique to each cell
    label.text = @"HELLO"; 
    label.frame = CGRectMake(arc4random() % 50, -1, 286, 45); 
    label.backgroundColor = [UIColor clearColor]; 

    return cell; 
} 

セルが nil の場合にのみラベルが作成されることに注意してください。それ以外の場合は、タグを使用して検出されます。

于 2012-05-01T07:27:17.610 に答える
0

elseの部分のようなものを使用できますif(cell == nil)

for (UIView *sub in [cell.contentView subviews]) 
{
            if([UILabel class] == [sub class])
                NSLog(@"%@",[sub class]);
                UILabel *label = (UILabel *)sub;
                //do label coding ie set text etc.
}
于 2012-05-01T07:27:24.343 に答える
0

セルが再利用されるのと同じ方法でラベルを再利用するコードが必要です。

いいえ、テーブル ビューの設計をよりよく理解する必要があります。ビューが複数回追加される理由は明らかです。セルを再利用するということは、UITableViewCell不要になった以前のインスタンスを取得し (したがって、コストのかかる新しいオブジェクトの割り当てを節約できます)、このインスタンスを新しいセルに再利用することを意味します。ただし、この前のインスタンスには既にラベルが添付されているため、ラベルの数が増えます。

サブクラスUITableViewCell化して、ラベルの作成をこの新しいクラスの初期化コード内に配置します。(または、Matt Gallagher によるこの素敵なテーブル チュートリアルで提案されているように、UIViewサブクラスを作成し、セルの として設定します。) ビューの詳細をカプセル化し、テーブル データ ソースからそれらを非表示にする適切な方法です。contentView

于 2012-05-01T07:35:54.870 に答える
0

カスタム テーブル セル クラス内でビューの遅延初期化を使用します。ビューと「addSubview」を一度だけロードする必要があります。

- (void) lazyInitTitleLabel {
    if (_titleLabel != nil) {
        return;
    }

    _titleLabel = [[UILabel alloc] initWithFrame: CGRectMake(10.0f, 10.0f, 200.0f, 30.0f)];
    // Cell adds the label as a subview...
    [self addSubview: _titleLabel];
}

注意する必要があるのは、ビューがラベルのテキストやイメージ ビューのイメージのように表示するコンテンツをリセットすることだけです。そうしないと、古いコンテンツがリサイクルされた表のセルと一緒に再利用される可能性があります。

幸運を!

于 2012-05-01T08:16:17.617 に答える