如何在iPhone中滚动UITableView时避免重复Label,Button

时间:2014-03-14 05:20:12

标签: ios iphone uitableview

我创建了UITableView并添加了标签和按钮。在上下滚动桌子时,自动标记&按钮是重复的和相同的标签&按钮出现超过两到三个。怎么避免这个?

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

3 个答案:

答案 0 :(得分:1)

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

然后使用标签设置文本或其他内容。 简而言之,只有在您创建单元格对象时才分配标签。下次重复使用它。

我希望这对你有用:)

答案 1 :(得分:0)

在重复使用单元格期间多次添加标签和按钮,这就是为什么会导致重复。错误在此行[cell addSubview:myAmount];中。在添加视图之前,请检查它是否已在可重复使用的单元格中添加。只需执行以下步骤即可。

  

首先想一想,我注意到,你使用了两种类型的细胞,所以使用两种类型   用于创建和检索单元格的可重用标识符。

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

为了保持添加的视图,请执行此操作。

答案 2 :(得分:0)

如果您的单元格有多种类型,则应创建许多标识符。 像这样:

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