嗨,我正在使用以下代码在 UITableView 中插入一个 ulabel
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier;
CellIdentifier = [NSString stringWithFormat:@"myTableViewCell %i,%i",
[indexPath indexAtPosition:0], [indexPath indexAtPosition:1]];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
lblNombre= [[UILabel alloc] initWithFrame:CGRectMake(110, 10, 170,40)];
lblNombre.textColor = [UIColor colorWithRed:90/255.0f green:132/255.0f blue:172/255.0f alpha:1];
lblNombre.backgroundColor = [UIColor clearColor];
lblNombre.text=@"Nicolas ahumada";
lblNombre.numberOfLines=2;
lblNombre.font = [UIFont fontWithName:@"Magra" size:18.0 ];
[cell.contentView addSubview:lblNombre ];
}
lblNombre.text=[[jsonpodio valueForKey:@"name"]objectAtIndex:indexPath.row ];
[cell.contentView addSubview:lblNombre ];
return cell;
}
但是当我滚动或给表格充电时,UILabel 会被覆盖
上面的图片被覆盖,下面的图片低于平均水平,
非常感谢您的帮助
最佳答案
试试这个,你的单元重用逻辑以及你如何使用 lblNombre 都有问题
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier;
// use a single Cell Identifier for re-use!
CellIdentifier = @"myCell";
// make lblNombre a local variable!
UILabel *lblNombre;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
// No re-usable cell, create one here...
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// get rid of class instance lblNombre, just use local variable!
lblNombre= [[UILabel alloc] initWithFrame:CGRectMake(110, 10, 170,40)];
lblNombre.tag = 1001; // set a tag for this View so you can get at it later
lblNombre.textColor = [UIColor colorWithRed:90/255.0f green:132/255.0f blue:172/255.0f alpha:1];
lblNombre.backgroundColor = [UIColor clearColor];
lblNombre.numberOfLines=2;
lblNombre.font = [UIFont fontWithName:@"Magra" size:18.0 ];
[cell.contentView addSubview:lblNombre ];
}
else
{
// use viewWithTag to find lblNombre in the re-usable cell.contentView
lblNombre = (UILabel *)[cell.contentView viewWithTag:1001];
}
// finally, always set the label text from your data model
lbl.text=[[jsonpodio valueForKey:@"name"]objectAtIndex:indexPath.row ];
return cell;
}
关于ios - UITableView 中的 UILabel 被覆盖,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14326992/