0

UITableViewでスクロールが途切れるのはなぜですか? 私の考えでは、次のコードが原因です。このロジックを別のものに置き換えるのに非常に苦労しています。

for (UIView *view in cell.contentView.subviews) {
[view removeFromSuperview]; }

これが私がやっていることです。

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

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

for (UIView *view in cell.contentView.subviews) {
    [view removeFromSuperview];
}

BGMArticleAbstract *articleAbstract = [self.section.articleAbstracts objectAtIndex:indexPath.row];
[cell.contentView addSubview:[self getHedlineFromArticleAbstract:articleAbstract]];
[cell.contentView addSubview:[self getThumbnailImageFromArticleAbstract:articleAbstract]];
[cell.contentView addSubview:[self getAbstractParaFromArticleAbstract:articleAbstract]];

cell.selectionStyle = UITableViewCellSelectionStyleNone;

return cell; }

動的なセルの高さを作成しているため、addSubview を contentview に追加しています。このスクロール ビューをスムーズに動作させる方法はありますか? ご協力ありがとうございました。

4

2 に答える 2

3

必要に応じてセルを設計する必要があります。セルにラベルや必要なものを追加してから、これらのすでに利用可能なサブビューのコンテンツを変更します。
画像を表示する必要がある場合はUIImageView、セルにを1回追加し、その画像プロパティのみを変更します。テキストフィールドなども同様です。

これを行う方法では、すべてのサブビューを何度も再生成するため、組み込みキャッシュが役に立たなくなります。

パフォーマンスをさらに向上させるために、セルの描画を自分で行うことができます。

Appleには非常に優れたサンプルプロジェクトがあります:http: //developer.apple.com/library/ios/#samplecode/AdvancedTableViewCells/Introduction/Intro.html

于 2013-01-04T21:13:11.167 に答える
1

セルを返す方法が問題の原因であることは正しいです。正しいパターンは次のとおりです...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


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

    // see if cell contains our image views.  A reused cell will, but a new cell won't
    UIImageView *imageViewA = (UIImageView *)[cell viewWithTag:32];
    UIImageView *imageViewB = (UIImageView *)[cell viewWithTag:33];
    UIImageView *imageViewC = (UIImageView *)[cell viewWithTag:34];

    if (!imageViewA) {
        // the cell must be new, so create it's image views
        // you should be able to borrow most of this code from your getHeadline/thumbnail/etc methods.
        // the good news is that this relatively expensive code runs only for new
        // cells and there are only a few of those - only enough to fill the visible frame
        imageViewA = [[UIImageView alloc] initWithFrame:CGRectMake(/* frame it here */)];
        [cell.contentView addSubview:imageViewA];
        imageViewA.tag = 32;

        imageViewB = [[UIImageView alloc] initWithFrame:CGRectMake(/* frame it here */)];
        [cell.contentView addSubview:imageViewB];
        imageViewB.tag = 33;

        imageViewC = [[UIImageView alloc] initWithFrame:CGRectMake(/* frame it here */)];
        [cell.contentView addSubview:imageViewC;
        imageViewC.tag = 34;

        // this too, need only be done upon creation
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    // now, whether it's a new cell or a reused cell, we have image views 

    BGMArticleAbstract *articleAbstract = [self.section.articleAbstracts objectAtIndex:indexPath.row];

    // change your methods getHeadline... getThumbnail... etc to answer UIImages
    // not UIImageViews, which are setup only for new cells

    imageViewA.image = [self getHedlineFromArticleAbstract:articleAbstract]];
    imageViewB.image = [self getThumbnailImageFromArticleAbstract:articleAbstract]];
    imageViewC.image = [self getAbstractParaFromArticleAbstract:articleAbstract]];

    // as a side note, once you get these methods returning images (more like model objects)
    // rather than image views (view objects) they might be more appropriately placed
    // in the BGMArticleAbstract class rather than the view controller

    return cell;
}
于 2013-01-05T01:28:59.693 に答える