cellForRowAtIndexPath: UILabel overlaps after scroll

5.6k views Asked by At

cellForRowAtIndexPath:

cell.textLabel.text works fine.

UILabel is overlapping after scrolling. Here is the code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

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

        // I have tried it by removing views and without.  No difference.   
        NSArray *viewsToRemove = [self.tableView subviews];
        for (UITableView *table in viewsToRemove)
        {
            [table removeFromSuperview];
        }
    }


    NSManagedObject *managedObject = [newClass objectAtIndex:indexPath.row];
    NSString  *entityName= [[managedObject entity]name];
    cell.textLabel.text = [NSString stringWithFormat:@"%@   %i", entityName, [indexPath row]];
    cell.textLabel.font=[UIFont systemFontOfSize:14.0];


    NSDate *date = [managedObject valueForKey:@"lastmoddate"];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"EEE, MMM d, YYYY  h:mm a"];
    NSString *dateString = [formatter stringFromDate:date];

    UILabel *lblDate = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 215, 10)];
    lblDate.text = dateString;
    lblDate.textColor = [UIColor grayColor];
    lblDate.font = [UIFont systemFontOfSize:10.0];

    [lblDate setBackgroundColor:[UIColor clearColor]];
    [cell.contentView addSubview:lblDate];
    return cell;
}

Here is the image:

enter image description here

10

There are 10 answers

1
user1107173 On BEST ANSWER

This is what I came up with and it works well:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

    NSManagedObject *managedObject = [newClass objectAtIndex:indexPath.row];
    NSString  *entityName= [[managedObject entity]name];

    NSDate *date = [managedObject valueForKey:@"lastmoddate"];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"EEE h:mm a MMM d, yy'''"];
    NSString *dateString = [formatter stringFromDate:date];

    UILabel *lblUser = [[UILabel alloc] initWithFrame:CGRectMake(30, 8, 215, 14)];
    lblUser.text = [NSString stringWithFormat:@"%@   %i", entityName, [indexPath row]];
    lblUser.textColor = [UIColor blackColor];
    lblUser.font = [UIFont systemFontOfSize:16.0];
    lblUser.tag = 1;
    [lblUser setBackgroundColor:[UIColor clearColor]];

    UILabel *lblDate = [[UILabel alloc] initWithFrame:CGRectMake(30, 21, 215, 20)];
    lblDate.text = dateString;
    lblDate.textColor = [UIColor grayColor];
    lblDate.font = [UIFont systemFontOfSize:12.0];
    lblDate.tag = 2;
    [lblDate setBackgroundColor:[UIColor clearColor]];

    if ((([cell.contentView viewWithTag:1]) && ([cell.contentView viewWithTag:2])))
    {
        [[cell.contentView viewWithTag:1]removeFromSuperview];
        [[cell.contentView viewWithTag:2]removeFromSuperview];
    }

    [cell.contentView addSubview:lblDate];
    [cell.contentView addSubview:lblUser];


    return cell;
}
11
iAppDeveloper On

Try this

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

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

    UILabel *lblDate = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 215, 10)];

    [cell.contentView addSubview:lblDate];
    }


    NSManagedObject *managedObject = [newClass objectAtIndex:indexPath.row];
    NSString  *entityName= [[managedObject entity]name];
    cell.textLabel.text = [NSString stringWithFormat:@"%@   %i", entityName, [indexPath row]];
    cell.textLabel.font=[UIFont systemFontOfSize:14.0];
lblDate.text = dateString;
    lblDate.textColor = [UIColor grayColor];
    lblDate.font = [UIFont systemFontOfSize:10.0];

    [lblDate setBackgroundColor:[UIColor clearColor]];

    NSDate *date = [managedObject valueForKey:@"lastmoddate"];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"EEE, MMM d, YYYY  h:mm a"];
    NSString *dateString = [formatter stringFromDate:date];
    return cell;
}
2
Puneet Sharma On

You are right in writing the code for removing all contents from cell's subview. But you wrote it at wrong place. UITableView's dequeueReusableCellWithIdentifier will return you the cell after it has been allocated and initialized once. So the code you wrote for removing the cell.contentView.subViews will never run and you get the Overlapped views.

You can either right that code in the else statement but I do not prefer that way. Why allocate and initialize all the contentView's every-time the UITableView needs cell. Rather I would create UILabel once and give it a tag to access it later. Like this:

 UILabel *lblDate = nil;
  if (cell == nil)
  {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    lblDate = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 215, 10)];
    lblDate.textColor = [UIColor grayColor];
    lblDate.font = [UIFont systemFontOfSize:10.0];
    lblDate.tag = 1;
    [lblDate setBackgroundColor:[UIColor clearColor]];
    [cell.contentView addSubview:lblDate];
  }
  else
  {
    //get a reference to the label in the recycled view
    lblDate = (UILabel *)[cell.contentView viewWithTag:1];
  }
0
rdelmar On

dequeueReusableCellWithIdentifier:forIndexPath: is guaranteed to return a cell (either a new one, or one from the reuse queue), so your if (cell == nil) clause never gets executed -- that's why it doesn't make a difference whether you remove the views or not. The labels overlap because that's the way you're setting it up. The default label is on the left side of the cell, and lblDate is also on the left (10 points from the left). Even if you move lblDate to the right, it might not show, because I think the default label goes full width of the cell. It would be better to make a custom cell with two labels that you place where you want them.

You also need to test whether the label already exists before you add another one. You can give the labels a unique tag, and check for a view with that tag, or, the easier way, I think, is to just make a custom cell in the storyboard or xib, and add the labels there. Then you only need to add the content to them in code.

2
Mahesh On

Try this code. Your problem will be resolved.

-(NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section     
{

    return [arrTableData count];

}


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

static NSString *CellIdentifier = @"Cell";

UILabel *lblName;

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

    lblName = [[[UILabel alloc] initWithFrame:CGRectMake(10, 20, 300, 20.0)] autorelease];
    lblName.tag = LBLNAME;
    lblName.numberOfLines=1;
    lblName.textAlignment=UITextAlignmentLeft;
    lblName.font = [UIFont systemFontOfSize:16.0];
    lblName.textColor = [UIColor blackColor];
    lblName.backgroundColor = [UIColor clearColor];
    lblName.autoresizingMask = UIViewAutoresizingFlexibleRightMargin  ;
    [cell.contentView addSubview:lblName];

}else{

    lblName = (UILabel *)[cell.contentView viewWithTag:LBLNAME];
}
if (arrTableData.count>0) { lblName.text=[NSString stringWithFormat:@"%@",[arrTableData objectAtIndex:indexPath.row]];

}

return cell;}
0
Pavan kumar C On

Add this line:

[cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

Before:

[cell.contentView addSubview:lblDate];
0
Samitha K On

This is problem with recreating cell contents. Try with following code segment.

for(UIView *view in cell.contentView.subviews){  
        if ([view isKindOfClass:[UIView class]]) {  
            [view removeFromSuperview];   
        }
    }
0
Preetha On

In my case same issue is happened,allocating the tableviewcell in 2places,one is allocated in tableviewcell custom class,and customview controller has to been allocated,after i was changed the alllocation then everything will be working fine.

0
Rajkumar Yadav On

Try This

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


static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];

NSManagedObject *managedObject = [newClass objectAtIndex:indexPath.row];
NSString  *entityName= [[managedObject entity]name];

NSDate *date = [managedObject valueForKey:@"lastmoddate"];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"EEE h:mm a MMM d, yy'''"];
NSString *dateString = [formatter stringFromDate:date];

UILabel *lblUser = [[UILabel alloc] initWithFrame:CGRectMake(30, 8, 215, 14)];
lblUser.text = [NSString stringWithFormat:@"%@   %i", entityName, [indexPath row]];
lblUser.textColor = [UIColor blackColor];
lblUser.font = [UIFont systemFontOfSize:16.0];
lblUser.tag = 1;
[lblUser setBackgroundColor:[UIColor clearColor]];

UILabel *lblDate = [[UILabel alloc] initWithFrame:CGRectMake(30, 21, 215, 20)];
lblDate.text = dateString;
lblDate.textColor = [UIColor grayColor];
lblDate.font = [UIFont systemFontOfSize:12.0];
lblDate.tag = 2;
[lblDate setBackgroundColor:[UIColor clearColor]];



[cell.contentView addSubview:lblDate];
[cell.contentView addSubview:lblUser];


return cell;
}
0
abdul sathar On
if ([cell.contentView viewWithTag:tagnumber]
    {
        [[cell.contentView viewWithTag:tagnumber]removeFromSuperview];

    }
lblDate.tag = tagnumber;
    [cell.contentView addSubview:lblDate];

this line is enough just remove the previous tag subviews and added new subview with tag .. Thanks for your answer