具有重叠单元格的自定义单元格
问题描述:
我正在使用方法cellForRowAtIndexPath来绘制带有2个标签的单元格。当我重复使用一个单元格时,我在该单元格中的标签没有被删除,所以我得到两个标签重叠,因为我正在重新使用一个单元格。这很奇怪,因为只有标签标题重叠,另一个不是。具有重叠单元格的自定义单元格
所以我做了什么来解决这个问题,我总是在创建一个新的单元格。哪个不合适,但解决问题...任何人都有类似的错误,并设法解决它?因为我刚刚作出了一个解决办法...
//Desenhado por codigo
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
UILabel *label = nil;
UILabel *label2 = nil;
UIColor *green = [UIColor colorWithRed:0 green: 0.77 blue:0 alpha:1];
cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
// if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell"] autorelease];
//Descrição
label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setMinimumFontSize:FONT_SIZE];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[label setFont:[UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:14.0]];
[label setTag:1];
[label setTextColor:green];
[label setBackgroundColor:[UIColor clearColor]];
label.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"barra_cinza_lista.png"]];
[[cell contentView] addSubview:label];
label2 = [[UILabel alloc] initWithFrame:CGRectZero];
[label2 setLineBreakMode:UILineBreakModeWordWrap];
[label2 setMinimumFontSize:FONT_SIZE];
[label2 setNumberOfLines:0];
[label2 setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[label2 setFont:[UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:14.0]];
[label2 setTag:2];
[label2 setTextColor:[UIColor whiteColor]];
[label2 setBackgroundColor:[UIColor clearColor]];
[[cell contentView] addSubview:label2];
}
NSString *text = [self getTableViewRow:tableView index:indexPath];
NSString *title = @" %@";
if(tableView == myTableView1)
title = [NSString stringWithFormat:title, [_titlesResults objectAtIndex:indexPath.row]];
else
title = [NSString stringWithFormat:title, [_titlesVision objectAtIndex:indexPath.row]];
CGFloat widthMax = CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2);
CGSize constraint = CGSizeMake(widthMax, 20000.0f); //Tamanho máximo permitido de largura e altura
CGSize size1 = [title sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; //Tamanho ocupado pelas letras
CGSize size2 = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; //Tamanho ocupado pelas letras
if (!label)
label = (UILabel*)[cell viewWithTag:1];
if (!label2)
label2 = (UILabel*)[cell viewWithTag:2];
[label setText:title];
[label setFrame:CGRectMake(0, 0, 320, MAX(size1.height, 44.0f))];
[label2 setText:text];
[label2 setFrame:CGRectMake(CELL_CONTENT_MARGIN, 3 * CELL_CONTENT_MARGIN + size1.height, widthMax, MAX(size2.height, 44.0f))];
return cell;
}
答
在方法的开始,通过所有的UIView
对象枚举[cell.contentView subviews]
和摆脱他们,就像这样:
for (UIView *subview in [cell.contentView subviews]) {
[subview removeFromSuperview];
}
这是可行的,因为所有UI元素都继承自UIView
,因此可以像移除它们一样被删除。
答
第一,取消这条线:
// if (cell == nil)
,如果你不这样做,你只是创建和每次滚动的时间分配一个新的单元格,从未释放旧的......
如果我这样做,我会得到我遇到的问题,因为我正在重新使用一个单元格。 – bruno 2012-02-12 16:37:41