gpt4 book ai didi

iphone - UITableViewCell 中自定义标签的问题

转载 作者:搜寻专家 更新时间:2023-10-30 19:51:18 25 4
gpt4 key购买 nike

我有 UITableViewController。在 cellForRowAtIndexPath 方法中,我为标签添加了自定义设置:

UILabel *lblMainLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 9, 150, 25)];
lblMainLabel.text = c.Name;
lblMainLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
lblMainLabel.backgroundColor = [UIColor clearColor];
lblMainLabel.textColor = [UIColor whiteColor];
[cell.contentView addSubview:lblMainLabel];
[lblMainLabel release];

但是当我在表格中向上或向下滚动时,它总是在我错过的之前添加这个标签?

最佳答案

您应该在创建单元格时只创建一次 UILabel。

您的代码应如下所示:

if (cell == nil) {
cell = ...
UILabel *lblMainLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 9, 150, 25)];
lblMainLabel.tag = 42;
lblMainLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
lblMainLabel.backgroundColor = [UIColor clearColor];
lblMainLabel.textColor = [UIColor whiteColor];
[cell.contentView addSubview:lblMainLabel];
[lblMainLabel release];
}
UILabel *lblMainLabel = [cell viewWithTag:42];
lblMainLabel.text = c.Name;

关于iphone - UITableViewCell 中自定义标签的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4967554/

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