asl87
asl87

Reputation: 105

UILabel in UITableView overwritten

hi I am using the following code to insert a ulabel in a UITableView

- (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;
}

but when I scroll or table is recharged, the UILabel is overwritten

uilabel overwritten

Image above is overwritten and the picture below average, thank you very much for your help

Upvotes: 3

Views: 3918

Answers (5)

ios.yd
ios.yd

Reputation: 1

Here you have a piece of code

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;
}

Upvotes: 0

AtWork
AtWork

Reputation: 1291

for small & sweet answer:

Before adding the subview:

Just write this code:

for(UIView *v in [cell.contentView subviews])
{
   [v removefromsuperview];
}

Upvotes: 3

Srikanth
Srikanth

Reputation: 1735

Can you try the following code

- (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;
}

Upvotes: 1

CSmith
CSmith

Reputation: 13458

Try this, you have issues with your cell re-use logic as well as how you're using 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;
}

Upvotes: 5

erikprice
erikprice

Reputation: 6308

Change this line:

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

to this:

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

and then remove lblNombre from your list of instance variables (which is probably in your header file).

Upvotes: 0

Related Questions