[英]IPHONE: UITableView cells not releasing content
我下面有这段代码来快速填充我的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并到达常规单元格以及带有按钮和标签的单元格之间的划分时,我看到它正在正确渲染,但是如果我在向下深入之后又向上移动,则会看到按钮和标签。被添加到原本不应该拥有的单元中。 从现在开始,所有单元格都包含按钮和标签...
就像细胞在绘制之前没有释放其内容一样。 就像标签和按钮被添加到单元格上已经存在的其他按钮和标签之上。 单元在再次绘制之前不会释放其内容。
怎么解决呢?
谢谢你的帮助。
注意:在做出两个前两个答案所建议的更改之后,我几乎看不到任何区别。 现在,并非所有单元都错了,只有一些。 每当我向下滚动表并返回到表的开头时,它们都会更改。
您应该为所使用的每个单元格“类型”使用单独的reuseIdentifier
。 在这种情况下,您将要使用两个。
您还会想要在遇到出队未命中而不是每次运行时都创建/添加UILabel
和UIButton
。.在伪代码中:
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.
否则,每次将单元格从表中出队时,您都将创建一个标签和按钮。 上下滚动将导致创建许多标签和按钮,这些标签和按钮永远不会释放。
您应该使用两个不同的复用标识符; 一个用于仅包含图像的单元格,另一个用于具有图像和按钮的单元格。 问题在于您的一种单元格类型正在被重用,但是其内容在出队时并未(也不应该清除)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.