Ios UITableViewCell覆盖:何时在cellForRowAtIndexPath中alloc UILabel?

Ios UITableViewCell覆盖:何时在cellForRowAtIndexPath中alloc UILabel?,ios,uitableview,Ios,Uitableview,我想从nib文件中显示UITableviewCell。并且还希望在其上添加动态标签。但这个标签本身就是一个标签。 请提供我可以从nib调用UITableviewCell并添加动态标签的任何方法 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UINib *nib = [UINib nibWithNibName

我想从
nib
文件中显示
UITableviewCell
。并且还希望在其上添加动态标签。但这个标签本身就是一个标签。 请提供我可以从
nib
调用
UITableviewCell
并添加动态标签的任何方法

- (void)viewDidLoad
{
  [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

UINib *nib = [UINib nibWithNibName:@"ItemCell" bundle:nil];
[[self tblView] registerNib:nib forCellReuseIdentifier:@"ItemCell"];

}

- (void)didReceiveMemoryWarning
 {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
 }


#pragma mark

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return 1;
   }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section                {
return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath  *)indexPath {

 return 150.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

ItemCell *cell =  [tableView dequeueReusableCellWithIdentifier:@"ItemCell"];

    for (int i=0; i<4; i++) {
    UILabel *lbl=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 20.0*i, 150.0, 20.0)];
    lbl.text=@"This is custom cell.";
    [cell.contentView addSubview:lbl];
    }

return cell;
}
-(void)viewDidLoad
{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
UINib*nib=[UINib nibWithNibName:@“ItemCell”bundle:nil];
[[self-tblView]registerNib:nib-forCellReuseIdentifier:@“ItemCell”];
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
#布拉格标记
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回4;
}
-(CGFloat)tableView:(UITableView*)表视图行高度索引路径:(NSIndexPath*)索引路径{
返回150.0;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
ItemCell*单元格=[tableView dequeueReusableCellWithIdentifier:@“ItemCell”];
对于(int i=0;i使用此

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath   *)indexPath 
{
UILabel *lbl; 
ItemCell *cell = (ItemCell *)[tableView dequeueReusableCellWithIdentifier:@"ItemCell"];
if (cell == nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ItemCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
    lbl=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 150.0, 60.0)];
    lbl.tag = indexpath.row; 
    [cell.contentView addSubview:lbl];
} 
lbl = (UIlabel*) [cell.contentView viewWithTag:indexpath.row]; 
lbl.text=@"This is custom cell.";
return cell;
}
试试这个

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

 ItemCell *cell =  [tableView dequeueReusableCellWithIdentifier:@"ItemCell"];

 UILabel* label = (UILabel*)[cell.containtView viewWithTag:indexPath.row];
 if(!label) {
   UILabel *lbl=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 150.0, 60.0)];
   lbl.text=@"This is custom cell.";
   lbl.tag = indexPath.row
   [cell.contentView addSubview:lbl];
 }
 label.text = @"your text";
 return cell;
}

问题是什么?所有单元格都有相同的文本?UILabel文本在重新加载tableview时变为粗体。问题是因为您正在重用tableviewcells。但没有重用创建的标签。请尝试重用UILabel,如下所示。每次都有重叠的标签。我想通过nib文件动态添加UILabel,这是不可能的。UILabelt可能不止一个。请参阅代码。UILabel的数量取决于数据库。因此,通过nib是不可能的。