繁体   English   中英

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

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

我创建了UITableView并添加了标签和按钮。 上下滚动表时,将自动复制标签和按钮,并且同一标签和按钮出现超过2到3个。 如何避免这种情况?

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

然后使用标签在此处设置文本或其他内容。 简而言之,仅当您创建单元格对象时才分配标签。 下次再用。

我希望这对您有用:)

标签和按钮在重用单元格中多次添加,这就是原因,它会导致重复。 此行中有错误[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];
}

这样做可以保留剩余的视图。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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