UITableViewCell中的自定义标签问题 [英] Problem with custom label in UITableViewCell

查看:50
本文介绍了UITableViewCell中的自定义标签问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

在创建单元格时,应该只创建一次UILabel.

you should create the UILabel exactly one time, when you create the cell.

您的代码应如下所示:

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;

这篇关于UITableViewCell中的自定义标签问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆