ios - UITableView 中的 UILabel 被覆盖

标签 ios objective-c uitableview uilabel

您好,我正在使用以下代码在 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 被覆盖

uilabel overwritten

上面的图片被覆盖了,图片低于平均水平, 非常感谢你的帮助

最佳答案

试试这个,你的单元格重用逻辑以及你如何使用 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/

相关文章:

ios - 使用 Core Data 将图像添加到数据库

ios - 需要应用 UIRotationGestureRecognizer 后跟 UILongPressGestureRecongnizer

ios - 核心数据未在 TableView 中获取

ios - 通过 keyboardFrameWillChange 显示键盘会导致表格数据滚出 View

ios - 如何在 Swift 中设置代理?

ios - 尝试自定义 FUIAuthPickerViewController 并在 AppDelegate 中出错

objective-c - 数据上传期间在 UITableView 上的 UIProgressView+Activity Indicator View

iphone - 当应用程序进入前台时如何呈现模态视图 Controller ?

iPhone 没有崩溃,仪器没有泄漏,应用程序可以上传了吗?

ios - 在 xib 的每一行中使用 uitableview 自定义多个单元格