-1

我必须填充行数。当我使用

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  

这一行,在滚动到某些行后,它会在同一位置重绘行。见下面的屏幕
在此处输入图像描述

如果我让细胞为零,即

UITableViewCell *cell = nil;  

然后工作正常,但它需要更多的内存。现在我的问题是它为什么会发生。

使单元格为零与在同一位置重绘有什么关系?

这是我的代码:

- (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] autorelease];
    }

    UIButton *questionButton=[UIButton buttonWithType:UIButtonTypeCustom];
    questionButton.backgroundColor=[UIColor clearColor];
    questionButton.tag=indexPath.row+1;

    [questionButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];

    UIImageView *statusImageView=[[UIImageView alloc]initWithFrame:CGRectMake(190,25,23,23)];

    NSString *statusImageName=[[NSString  alloc]init];
    statusImageName=@"Right";
    [statusImageView setImage:[UIImage imageNamed:statusImageName]];
    [questionButton addSubview:statusImageView];

    if(indexPath.row%2==0)
    {
        [questionButton setBackgroundImage:[UIImage imageNamed:@"TopicsbarLeft"] forState:UIControlStateNormal];
        [questionButton setBackgroundImage:[UIImage imageNamed:@"TopicsbarLetSelected"] forState:UIControlStateHighlighted];
        CGRect topicButtonFrame=CGRectMake(0, 10, 250, 70);
        questionButton.frame=topicButtonFrame;



        NSLog(@"Even Rows =%d",indexPath.row);

        NSString *questionLabel=[NSString stringWithFormat:@" Question %d :",indexPath.row+1];
        [questionButton setTitle:questionLabel forState:UIControlStateNormal];
        //questionButton.titleLabel.textAlignment=NSTextAlignmentLeft;

        [questionButton.titleLabel setTextAlignment:UITextAlignmentLeft];
    }
    else
    {
        [questionButton setBackgroundImage:[UIImage imageNamed:@"TopicsbarRight"] forState:UIControlStateNormal];
        [questionButton setBackgroundImage:[UIImage imageNamed:@"TopicsbarRightSelected"] forState:UIControlStateHighlighted];
        CGRect topicButtonFrame=CGRectMake(100, 10, 250, 70);
        questionButton.frame=topicButtonFrame;


        NSString *questionLabel=[NSString stringWithFormat:@"     Question %d :",indexPath.row+1];
        [questionButton setTitle:questionLabel forState:UIControlStateNormal];
        questionButton.titleLabel.textAlignment=UITextAlignmentRight;
        [cell addSubview:questionButton];

    }

    [cell addSubview:questionButton];

    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return cell;
}
4

2 回答 2

2

的目的dequeueReusableCellWithIdentifier是使用更少的内存。如果屏幕可以容纳 4 或 5 个表格单元格,那么通过重用,即使表格有 1000 个条目,您也只需在内存中分配 4 或 5 个表格单元格。

因此,当您执行此操作时UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];,它会返回一个旧单元格以供重用。它不是一个新的单元实例。它包含初始化为旧数据的单元格的 UI 元素。要再次显示它,您需要重置所有 UI 元素并输入新值。

第二种方式放UITableViewCell *cell = nil;的时候没有重用。与仅使用表格单元格数组相比,第二种方式没有任何优势。如果您的表有 1000 个条目,那么您将在内存中分配 1000 个单元格。如果你打算这样做,你可以把它们放在一个数组中,然后用行号索引数组并返回单元格。这应该可以解释您的内存使用量激增,因为它没有重用任何单元实例。

对于具有固定单元格的小表,第二种解决方案可能是一个合理的解决方案,对于动态或大表重用是最好的方法......

更新:因为您要求提供一些代码。另请注意,您正在根据奇数或偶数进行 2 种类型的渲染。reuse因此,当您想显示偶数问题时,出来的单元格可能是奇数。在这种情况下,您需要在获得单元后(通过重用或创建新单元)重置单元,并让您的正常逻辑通过......

    [questionButton setBackgroundImage:[UIImage imageNamed:@"default.png"]];
    [questionButton setTitle:@"" forState:UIControlStateNormal];
    CGRect topicButtonFrame=CGRectMake(0, 0, 250, 70);
    questionButton.frame=topicButtonFrame;
于 2013-01-07T05:28:25.660 回答
0

问题在这里:

        [cell addSubview:questionButton];
    }
    [cell addSubview:questionButton];

您正在添加 questionButton 两次。请在其他部分删除一个并尝试。

更新 :

你可以试试这个只是为了确认是什么问题:

if(indexPath.row%2==0)
{
    UIButton *questionButton=[UIButton buttonWithType:UIButtonTypeCustom];
    questionButton.backgroundColor=[UIColor clearColor];
    questionButton.tag=indexPath.row+1;

    [questionButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];

    UIImageView *statusImageView=[[UIImageView alloc]initWithFrame:CGRectMake(190,25,23,23)];

    NSString *statusImageName=[[NSString  alloc]init];
    statusImageName=@"Right";
    [statusImageView setImage:[UIImage imageNamed:statusImageName]];
    [questionButton addSubview:statusImageView];
    [questionButton setBackgroundImage:[UIImage imageNamed:@"TopicsbarLeft"] forState:UIControlStateNormal];
    [questionButton setBackgroundImage:[UIImage imageNamed:@"TopicsbarLetSelected"] forState:UIControlStateHighlighted];
    CGRect topicButtonFrame=CGRectMake(0, 10, 250, 70);
    questionButton.frame=topicButtonFrame;

    NSString *questionLabel=[NSString stringWithFormat:@" Question %d :",indexPath.row+1];
    [questionButton setTitle:questionLabel forState:UIControlStateNormal];
    //questionButton.titleLabel.textAlignment=NSTextAlignmentLeft;

    [questionButton.titleLabel setTextAlignment:UITextAlignmentLeft];
    [cell addSubview:questionButton];
}
else
{
    UIButton *questionButton=[UIButton buttonWithType:UIButtonTypeCustom];
    questionButton.backgroundColor=[UIColor clearColor];
    questionButton.tag=indexPath.row+1;

    [questionButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];

    UIImageView *statusImageView=[[UIImageView alloc]initWithFrame:CGRectMake(190,25,23,23)];

    NSString *statusImageName=[[NSString  alloc]init];
    statusImageName=@"Right";
    [statusImageView setImage:[UIImage imageNamed:statusImageName]];
    [questionButton addSubview:statusImageView];
    [questionButton setBackgroundImage:[UIImage imageNamed:@"TopicsbarRight"] forState:UIControlStateNormal];
    [questionButton setBackgroundImage:[UIImage imageNamed:@"TopicsbarRightSelected"] forState:UIControlStateHighlighted];
    CGRect topicButtonFrame=CGRectMake(100, 10, 250, 70);
    questionButton.frame=topicButtonFrame;


    NSString *questionLabel=[NSString stringWithFormat:@"     Question %d :",indexPath.row+1];
    [questionButton setTitle:questionLabel forState:UIControlStateNormal];
    questionButton.titleLabel.textAlignment=UITextAlignmentRight;
    [cell addSubview:questionButton];

}
于 2013-01-07T05:26:21.670 回答