gpt4 book ai didi

iOS7 : UILabel constantly redraw itself on UITableViewCell

转载 作者:行者123 更新时间:2023-11-28 22:08:05 34 4
gpt4 key购买 nike

CoreData 返回 BOOL 值,根据该值我在 UITableViewCell accessoryView 上绘制了一个 UILabel。问题是 UILabel 也在它根本不应该出现的单元格上重复自己。

CGRect lblRect = CGRectMake(230, 7, 20, 20);
UILabel *lblEnabled = [[UILabel alloc] initWithFrame:lblRect];
lblEnabled.textColor=[UIColor whiteColor];
lblEnabled.textAlignment=NSTextAlignmentCenter;
[lblEnabled setFont:[UIFont fontWithName:@"Verdana" size:10.0]];
[lblEnabled setText:@"40"];
lblEnabled.backgroundColor= [UIColor colorWithPatternImage:[UIImage imageNamed:@"greenBg"]];
lblEnabled.layer.cornerRadius = 9.0;
lblEnabled.layer.masksToBounds = YES;
lblEnabled.tag = indexPath.row;
cell.accessoryView = lblEnabled;
[cell.contentView addSubview:lblEnabled];

因此它有时会出现在 BOOL value = NO 的单元格上;非常感谢您的帮助。

编辑:我在 cellForRowForIndexPath 中绘制这些标签。

编辑:我使用 Storyboard,所以我不检查单元格是否为零。

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

/*
if(cell==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
tableView.rowHeight=57.0;
}*/

Coin *coin=[[self frcFromTV:tableView ] objectAtIndexPath:indexPath];
cell.textLabel.text=coin.coinNominal;

if(coin.comSubject.length>0)
{
cell.detailTextLabel.text=[NSString stringWithFormat:@"%@%@ (%@) | %@",[self returnCatalogDefinition:coin.catalogIndex],coin.kmRef, coin.dates, coin.comSubject];
}
else
{
cell.detailTextLabel.text=[NSString stringWithFormat:@"%@%@ | %@",[self returnCatalogDefinition:coin.catalogIndex],coin.kmRef,coin.dates];
}

if(coin.isCommemorative.boolValue)
{
// implement label
}

if(coin.listed.boolValue)
{
CGRect lblRect = CGRectMake(230, 7, 20, 20);
UILabel *lblEnabled = [[UILabel alloc] initWithFrame:lblRect];
lblEnabled.textColor=[UIColor whiteColor];
lblEnabled.textAlignment=NSTextAlignmentCenter;
[lblEnabled setFont:[UIFont fontWithName:@"Verdana" size:10.0]];
[lblEnabled setText:@"40"];
lblEnabled.backgroundColor= [UIColor colorWithPatternImage:[UIImage imageNamed:@"greenBg"]];
lblEnabled.layer.cornerRadius = 9.0;
lblEnabled.layer.masksToBounds = YES;
lblEnabled.tag = indexPath.row;
cell.accessoryView = lblEnabled;
[cell.contentView addSubview:lblEnabled];
}

return cell;
}

最佳答案

在下面的代码中,您可能会添加标签,但因为这些单元格是可重用的,所以您应该处理 else 语句(隐藏标签或任何合适的内容)

if(coin.isCommemorative.boolValue)
{
// implement label
//remove from that part of the statement this line:
//[cell.contentView addSubview:lblEnabled];
} else {
cell.accessoryView = nil;
//hiding or modifying label for other cases
}

如果您不处理该 else 语句,则由于重用机制,您在 if 中所做的更改将适用于多个单元格

作为“附带建议”,我建议您将 UITableViewCell 子类化并添加您想要的属性(标签)来封装它,并只创建用于显示或隐藏该访问器的公共(public)方法。

编辑:如果您的更改标志未指定它必须指示哪个单元格(例如使用 indexPath),则结果与您的一样。

这是一个相当全局的状态 if(coin.isCommemorative.boolValue) 没有指出它计数到哪个单元格尝试例如(仅用于学习目的)添加 if(coin.isCommemorative.boolValue && indexPath.row%2==0) 并查看结果。

关于iOS7 : UILabel constantly redraw itself on UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23501388/

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