UITableViewCell 中自定义标签的问题
我有 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];
但是当我在表格中向上或向下滚动时,它总是将此标签添加到我之前错过的内容之上?
I have UITableViewController. In cellForRowAtIndexPath
method I added custom setup for label:
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];
But when I scroll UP or DOWN in table it always add this label on top of previous what I miss?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该在创建单元格时创建 UILabel 一次。
您的代码应如下所示:
you should create the UILabel exactly one time, when you create the cell.
Your code should look like this:
是的,fluchtpunkt,你是对的。
每次 tableview 滚动时,cellForRowAtIndexPath 都会被触发,它将重新加载数据。
一旦单元格分配就会被解雇。否则内存也会增加。
Yes fluchtpunkt, you are right.
cellForRowAtIndexPath gets fired every times the tableview scrolls, it will reloads the data.
will get fired once the cell is allocating. else memory also gets increased.