【问题标题】:IPHONE: UITableView cells not releasing contentIPHONE:UITableView 单元格不释放内容
【发布时间】:2009-10-20 17:29:35
【问题描述】:

我在下面有这段代码来动态填充我的 UITableView。

我必须显示两种单元格:带有背景图像的常规单元格和带有常规背景图像的单元格,外加标签和按钮。

如果 Indexpath.row 小于控制变量,则绘制常规单元格。如果没有,则绘制带有按钮和标签的单元格。

这是代码

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

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


    UIImage *imageU;

    if (indexPath.row < controlVariable)    {   

        imageU = [[[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle] 
              pathForResource:[NSString stringWithFormat: @"table%d", indexPath.row] ofType:@"jpg"]] autorelease];

        cell.imageView.image = imageU;

    } else { 

        imageU = [[[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle] 
                      pathForResource:[NSString stringWithFormat: @"table-pg%d",numberX] 
                                                      ofType:@"jpg"]] autorelease];
        cell.imageView.image = imageU;



        NSString * myString = [NSString stringWithFormat: @"pago%d", numberX];
        UILabel * myLabel = [[UILabel alloc] initWithFrame:CGRectMake(5.0, 49.0, 200.0, 22.0)];
        [myLabel setTextAlignment:UITextAlignmentLeft];
        [myLabel setBackgroundColor:[UIColor blueColor]];
        [myLabel setClipsToBounds:YES];
        [myLabel setFont:[UIFont systemFontOfSize:14.0]];
        [myLabel setTextColor:[UIColor blackColor]];
        [myLabel setText: myString];
        [myLabel setAlpha:0.6];
        [cell addSubview: myLabel];
        [myLabel release];


        UIButton *buyButton = [[UIButton alloc] initWithFrame:CGRectMake( 220, 4, 100, 35)];

        buyButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        buyButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

        [buyButton setTitle:NSLocalizedString(@"buyKey", @"") forState:UIControlStateNormal]; 
        [buyButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        buyButton.titleLabel.font = [UIFont boldSystemFontOfSize:14];

        UIImage *newImage = [[[[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle] 
                             pathForResource: @"whiteButton" ofType:@"png"]] autorelease]
                                 stretchableImageWithLeftCapWidth:12.0f topCapHeight:0.0f];
        [buyButton setBackgroundImage:newImage forState:UIControlStateNormal];

        [buyButton addTarget:self action:@selector(comprar:) forControlEvents:UIControlEventTouchDown];

        buyButton.backgroundColor = [UIColor clearColor];

        [buyButton setTag:indexPath.row];
        [cell addSubview:buyButton];
        [buyButton release];

    }

    return cell;
}

这段代码的问题是:当我向下滚动 UITableView 并到达常规单元格和带有按钮和标签的单元格之间的划分时,我看到它正在正确呈现,但是如果我在深入下去后向上,我会看到按钮和标签被添加到不应该有它们的单元格中。从现在开始,所有单元格都包含按钮和标签...

就像单元格在绘制之前没有释放其内容一样。就像标签和按钮被添加到单元格上已经存在的其他按钮和标签之上。单元格在再次绘制之前不会释放其内容。

如何解决?

感谢您的帮助。

注意:在进行两个第一个答案建议的更改后,我几乎看不到任何区别。现在,并不是所有的细胞都是错的,只是一些。每次我向下滚动表格并返回表格的开头时,它们都会改变。

【问题讨论】:

    标签: iphone iphone-sdk-3.0


    【解决方案1】:

    您应该为您正在使用的每个单元格“类型”使用单独的reuseIdentifier。在这种情况下,您需要使用两个。

    您还需要在出队时创建/添加UILabelUIButtonmiss,而不是每次运行都需要。在伪代码中:

    UILabel * lbl;
    UIButton * btn;
    cell = [table dequeueReusableCellWithIdentifier:correctIdentifier];
    if (cell == nil)
    {
        cell = ...; // alloc cell
        lbl = ...;
        lbl.tag = kTagLabel;
        [cell addSubView:lbl];
        btn = ...;
        btn.tag = kTagButton;
        [cell addSubView:btn];
    }
    else
    {
        lbl = (UILabel*)[cell viewWithTag:kTagLabel];
        btn = (UIButton*)[cell viewWithTag:kTagButton];
    }
    
    //... now set the text/image appropriately.
    

    否则,每次将单元格从表格中出列时,您都会创建一个标签和按钮。上下滚动会导致创建许多标签和按钮,而这些标签和按钮永远不会被释放。

    【讨论】:

    • 这非常有用,谢谢。另外,不要使用像 0,1,2,3 这样的标签
    【解决方案2】:

    你应该使用两个不同的reuseIdentifiers;一种用于只有图像的单元格,另一种用于带有图像和按钮的单元格。问题是您的一种单元格类型正在被重用,但它的内容在出队时没有(也不应该)被清除。

    【讨论】:

      猜你喜欢
      • 2011-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-22
      • 1970-01-01
      • 2010-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多