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并仅更改其图像属性。文本字段等也是如此。

您这样做的方式使内置缓存无用,因为您一次又一次地重新生成所有子视图..

为了进一步提高性能,您可以自己绘制单元格。

苹果有一个非常好的示例项目: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 回答