gpt4 book ai didi

iphone - 具有重叠单元格的自定义单元格

转载 作者:行者123 更新时间:2023-11-28 17:37:51 26 4
gpt4 key购买 nike

我正在使用方法 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;
}

最佳答案

在该方法的开头,枚举[cell.contentView subviews] 中的所有UIView 对象并删除它们,如下所示:

for (UIView *subview in [cell.contentView subviews]) {
[subview removeFromSuperview];
}

这是有效的,因为所有 UI 元素都继承自 UIView,因此可以像它们一样被删除。

关于iphone - 具有重叠单元格的自定义单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9250197/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com