3

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

上面的图片被覆盖,下面的图片平均,非常感谢您的帮助

4

5 回答 5

5

试试这个,你的单元重用逻辑以及你如何使用 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;
}
于 2013-01-14T21:45:20.177 回答
3

对于小而甜的答案:

添加子视图之前:

只需编写以下代码:

for(UIView *v in [cell.contentView subviews])
{
   [v removefromsuperview];
}
于 2013-02-12T10:52:11.853 回答
1

你可以试试下面的代码

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

static NSString *CellIdentifier;

// Was not sure why you had a reuse identifier which was different for each cell. You created a reuse identifier based on the index. Looks like your cells are all the same looking. So just use a constant string to identify the cell to be used.

CellIdentifier  = [NSString stringWithFormat:@"myTableViewCell"];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;

// Find a subview with a tag of 100 and remove it. See below as to why

[cell viewWithTag:100] removeFromSuperview];

if (cell == nil)
{
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

// I removed code from here and put it down, assuming that you have a data model which is feeding the data into the label

}


    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.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 ];

// Use  tag or some thing to identify this subview, since you cannot keep on adding subviews. You need to remove it next time you come because you are reusing the cells and you will get back a cell which you created before and that will have the label you added last time
    [lblNombre setTag:100];

    return cell;
}
于 2013-01-14T21:48:11.727 回答
0

更改此行:

    lblNombre= [[UILabel alloc] initWithFrame:CGRectMake(110, 10, 170,40)];

对此:

    UILabel *lblNombre = [[UILabel alloc] initWithFrame:CGRectMake(110, 10, 170,40)];

然后lblNombre从您的实例变量列表中删除(可能在您的头文件中)。

于 2013-01-14T21:39:16.600 回答
0

这里有一段代码

NSString *CellIdentifier = [NSString stringWithFormat:@"%ld,%ld",(long)indexPath.section,(long)indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier ];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
于 2017-02-09T10:35:19.893 回答