简体   繁体   中英

How to avoid duplication of Label, Button on scrolling UITableView in iPhone

I have created the UITableView and added label and button. On scrolling the table up and down, automatically label & button are duplicated and same label & button occurs more than two to three. How to avoid this??

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

    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

   // [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

    if (cell == nil)
    {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];

    }


    // Set up the cell...
    if(indexPath.section == 0)

    {
        cell.textLabel.text = [propertyDetails objectAtIndex:indexPath.row];

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


    if (indexPath.row==0)
        {
           UIView *myAmount = [[UIView alloc]initWithFrame:CGRectMake(200, 10, 90, 30)];
            myAmount.backgroundColor = [UIColor blueColor];
            [cell addSubview:myAmount];

            //cell.accessoryView = myAmount;

            UILabel *dollarSymbol = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 20, 30)];
            [dollarSymbol setText:@"$"];
            dollarSymbol.backgroundColor = [UIColor greenColor];
            [myAmount addSubview:dollarSymbol];

            UITextField *amountEntered = [[UITextField alloc]initWithFrame:CGRectMake(20, 0, 70, 30)];
            amountEntered.placeholder = @"Amount";
            amountEntered.backgroundColor = [UIColor redColor];
            [myAmount addSubview:amountEntered];
        }


        if (indexPath.row==1)
        {
            UIView *myDeposit = [[UIView alloc]initWithFrame:CGRectMake(200, 10, 90, 30)];
            myDeposit.backgroundColor = [UIColor blueColor];
            [cell addSubview:myDeposit];

            UILabel *percentageSymbol = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 20, 30)];
            [percentageSymbol setText:@"%"];
            percentageSymbol.backgroundColor = [UIColor greenColor];
            [myDeposit addSubview:percentageSymbol];

            UITextField *depositPer = [[UITextField alloc]initWithFrame:CGRectMake(20, 0, 70, 30)];
            depositPer.placeholder = @"Amount";
            depositPer.backgroundColor = [UIColor redColor];
            [myDeposit addSubview:depositPer];
        }

}

    else if(indexPath.section == 1)
    {
        cell.textLabel.text = [suburbSelect objectAtIndex:indexPath.row];



        if (indexPath.row==0)
        {
            UIView *enterSuburb = [[UIView alloc]initWithFrame:CGRectMake(150, 10, 140, 30)];
            enterSuburb.backgroundColor = [UIColor blueColor];
            [cell addSubview:enterSuburb];

            UITextField *suburbField = [[UITextField alloc] initWithFrame:CGRectMake(0,0,140,30)];
            suburbField.placeholder = @"Enter Suburb";
            suburbField.backgroundColor = [UIColor redColor];
            [enterSuburb addSubview:suburbField];

            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }

        if (indexPath.row==1) {

            UIButton *homeBtn = [[UIButton alloc]initWithFrame:CGRectMake(50, 10, 50, 30)];
            homeBtn.backgroundColor = [UIColor redColor];
            [homeBtn setTitle:@"Home" forState:UIControlStateNormal];

            UIButton *unitBtn = [[UIButton alloc] initWithFrame:CGRectMake(150, 10, 50, 30)];
            unitBtn.backgroundColor = [UIColor greenColor];
            [unitBtn setTitle:@"Unit" forState:UIControlStateNormal];

            [cell addSubview:homeBtn];
            [cell addSubview:unitBtn];

        }

        if (indexPath.row==2) {

            UIView *myYear = [[UIView alloc]initWithFrame:CGRectMake(200, 10, 90, 30)];
            myYear.backgroundColor = [UIColor blueColor];
            [cell addSubview:myYear];


            UITextField *enterYear = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 90, 30)];
            enterYear.placeholder = @"Years";
            enterYear.backgroundColor = [UIColor redColor];
            [myYear addSubview:enterYear];

            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        }


    }
    else if(indexPath.section == 2)
    {
        cell.textLabel.text = [result objectAtIndex:indexPath.row];

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        cell.accessoryView = nil; 

    }
    else if(indexPath.section == 3)
    {
        cell.textLabel.text = [compareSuburb objectAtIndex:indexPath.row];

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        cell.accessoryView = nil; 
    }
    else if(indexPath.section == 4)
    {
        cell.textLabel.text = [SuburbTwoCompare objectAtIndex:indexPath.row];

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        cell.accessoryView = nil; 
    }
    else if(indexPath.section == 5)
    {
        cell.textLabel.text = [resultCompare objectAtIndex:indexPath.row];

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        cell.accessoryView = nil; 
    }
    else if(indexPath.section == 6)
    {
        cell.textLabel.text = [gainLoss objectAtIndex:indexPath.row];

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        cell.accessoryView = nil; 
    }
    else
    {
        return 0;
    }
    return cell;
}
UILabel *lblFirst=nil;
        UILabel *lblSecond=nil;
        UILabel *lblThird=nil;
if (cell == nil)
        {
            cell = [[[UITableViewCell alloc] initWithStyle:style reuseIdentifier:MyIdentifier] autorelease];
            if([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0)
            {
                lblFirst = [[UILabel alloc] initWithFrame:CGRectMake(45, 0, 400,
                                                                     40)] ;
            }
            else
            {
                lblFirst = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, 400,
                                                                     40)] ;
            }
            lblFirst.tag=1;
            [self labelCommonSettingsLeftAlign:lblFirst];
            [cell.contentView addSubview:lblFirst];

            //----------------------------------------
            //start time
            if([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0)
            {
                lblSecond =  [[UILabel  alloc] initWithFrame:CGRectMake(45, 25, 250,
                                                                        40)] ;
            }
            else
            {
                lblSecond =  [[UILabel  alloc] initWithFrame:CGRectMake(40, 25, 250,
                                                                        40)] ;
            }
            lblSecond.tag=2;
            [self labelCommonSettingsLeftAlign:lblSecond];
            [cell.contentView addSubview:lblSecond];

            //end time
            if([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0)
            {
                lblThird =  [[UILabel   alloc] initWithFrame:CGRectMake(260, 25, 250.0,
                                                                        40)] ;
            }
            else
            {
                lblThird =  [[UILabel   alloc] initWithFrame:CGRectMake(260, 25, 250.0,
                                                                        40)] ;
            }

            lblThird.tag=3;
            [self labelCommonSettingsLeftAlign:lblThird];
            [cell.contentView addSubview:lblThird];
            [lblFirst release];
            lblFirst=nil;
            [lblSecond release];
            lblSecond=nil;
            [lblThird release];
            lblThird=nil;

        }




 lblFirst = (UILabel *)[cell.contentView viewWithTag:1];
    lblSecond = (UILabel *)[cell.contentView viewWithTag:2];
    lblThird = (UILabel *)[cell.contentView viewWithTag:3];

Then use the labels to set there text or other things. In short allocate the labels only when u create the cell object. Next time reuse it.

I hope this will work for you:)

Labels and buttons added multiple times during reuse cell that's why, it cause duplication. Error is in this line [cell addSubview:myAmount]; . Before adding view, check if it's already added in case of reusable cell. Just do the following steps.

First think, I've noted, You used two types of cell, So use two type of reusable identifier for creation and retrieval of cell.

UIView *myAmount = [cell viewWithTag:AMOUNTTAG];
if (!myAmount)
{
        myAmount = [[UIView alloc]initWithFrame:CGRectMake(150, 10, 140, 30)];
        myAmount.backgroundColor = [UIColor blueColor];
       myAmount.tag = AMOUNTTAG;
        [cell addSubview:myAmount];
}

Do like this for remaining added view.

If your cell have many types, you should create many identifiers. Like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *identifierRow0 = @"CellRow0";
  static NSString *identifierRow1 = @"CellRow1";
  static NSString *identifierRow2 = @"CellRow2";
  UITableViewCell *cell = nil;
  if (indexPath.row == 0) {
    cell = [tableView dequeueReusableWithIdentifier:identifierRow0];
  } else if (indexPath.row == 1) {
    cell = [tableView dequeueReusableWithIdentifier:identifierRow1];
  } eles if (indexPath.row == 2) {
    cell = [tableView dequeueReusableWithIdentifier:identifierRow2];
  }

  if (cell == nil) {
     if (indexPath.row == 0) {
       UIView *enterSuburb = [[UIView alloc]initWithFrame:CGRectMake(150, 10, 140, 30)];
       enterSuburb.backgroundColor = [UIColor blueColor];
       [cell addSubview:enterSuburb];

       UITextField *suburbField = [[UITextField alloc] initWithFrame:CGRectMake(0,0,140,30)];
       suburbField.placeholder = @"Enter Suburb";
       suburbField.backgroundColor = [UIColor redColor];
       [enterSuburb addSubview:suburbField];

       cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
     } else if (indexPath.row == 1) {
       UIButton *homeBtn = [[UIButton alloc]initWithFrame:CGRectMake(50, 10, 50, 30)];
       homeBtn.backgroundColor = [UIColor redColor];
       [homeBtn setTitle:@"Home" forState:UIControlStateNormal];

       UIButton *unitBtn = [[UIButton alloc] initWithFrame:CGRectMake(150, 10, 50, 30)];
       unitBtn.backgroundColor = [UIColor greenColor];
       [unitBtn setTitle:@"Unit" forState:UIControlStateNormal];

       [cell addSubview:homeBtn];
       [cell addSubview:unitBtn];
     } else if (indexPath.row == 2) {
       UIView *myYear = [[UIView alloc]initWithFrame:CGRectMake(200, 10, 90, 30)];
       myYear.backgroundColor = [UIColor blueColor];
       [cell addSubview:myYear];
     }

     // code for updating cell

     return cell;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM