-1

When i scroll the tableview gives this type of overlapping enter image description here

I write the below code in CellForRow AtIndexPath method,I create tableview programatically...

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
}

NSDictionary *info = [ASSETHELPER getGroupInfo:_groupArray[indexPath.row]];
UIImageView *view;

if (indexPath.row % 2) {
    view=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"dark_bg"]];
}else{
    view=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"light_bg"]];
}
cell.backgroundView=view;

UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
imgView.backgroundColor=[UIColor clearColor];
[imgView.layer setCornerRadius:25];
[imgView.layer setMasksToBounds:YES];
[imgView.layer setBorderColor:[UIColor whiteColor].CGColor];
[imgView setImage:[info objectForKey:@"thumbnail"]];
[cell.contentView addSubview:imgView];

UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(80, 20, 200, 30)];

[label setText:[info objectForKey:@"name"]];
[cell.contentView addSubview:label];

return cell;

}

  • We are not wizards or psychics. Add some code, and more details. – n00bProgrammer Sep 04 '14 at 10:42
  • 4
    You have not handled cell re-useability properly. post your cellPathForRowAtIndexPath code here. – Yogendra Sep 04 '14 at 10:43
  • Much better. Sorry if I sounded rude before. The problem here is `[cell.contentView addSubview:label];`. Every time `cellForRowAtIndexPath:` is called, you are adding a new label each time. Since cells are being reused, the label added before is never removed. – n00bProgrammer Sep 04 '14 at 11:46

2 Answers2

8

Try this code .You have used dequeueReusableCellWithIdentifier:

NSString *CellIdentifier = [NSString stringWithFormat:@"Cell %d",indexPath.row];

UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];

if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Ganesh Kumar
  • 660
  • 6
  • 24
-1
You can try with it. Just remove the previous contentView after create a table cell.

- (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:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    //****add this code*******//
    for (UIImageView *v in [cell.contentView subviews])
    {
        [v removeFromSuperview];
        NSLog(@"%@ hello \n\n\n\n\n%@",v,[cell.contentView  subviews]);
    }
    for (UILabel *v in [cell.contentView subviews])
    {
        [v removeFromSuperview];
        NSLog(@"%@ hello \n\n\n\n\n%@",v,[cell.contentView  subviews]);
    }/////////
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *info = [ASSETHELPER getGroupInfo:_groupArray[indexPath.row]];
    UIImageView *view;
    if (indexPath.row % 2)
    {
        view=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"dark_bg"]];
    }
    else
    {
        view=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"light_bg"]];
    }
    cell.backgroundView=view;
    UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
    imgView.backgroundColor=[UIColor clearColor];
    [imgView.layer setCornerRadius:25];
    [imgView.layer setMasksToBounds:YES];
    [imgView.layer setBorderColor:[UIColor whiteColor].CGColor];
    [imgView setImage:[info objectForKey:@"thumbnail"]];
    [cell.contentView addSubview:imgView];

    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(80, 20, 200, 30)];

    [label setText:[info objectForKey:@"name"]];
    [cell.contentView addSubview:label];

    return cell;
}
Monikanta
  • 297
  • 2
  • 8
  • Just add this code after creating cell. I think it will be help you. for (UIImageView *v in [cell.contentView subviews]) { [v removeFromSuperview]; NSLog(@"%@ hello \n\n\n\n\n%@",v,[cell.contentView subviews]); } for (UILabel *v in [cell.contentView subviews]) { [v removeFromSuperview]; NSLog(@"%@ hello \n\n\n\n\n%@",v,[cell.contentView subviews]); } – Monikanta Sep 05 '14 at 05:57
  • You know it's not rocket science to format your code correctly. I've done it for near enough every other answer you have given but now I am just going to downvote. I will remove once you have formatted your answer correctly so it is readable -1 – Popeye Sep 08 '14 at 12:51