简体   繁体   中英

each cell of table view displays the same value multiple times

Following is my cellForRowAtIndexPath method.

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

    UITableViewCell *cell = nil;
    cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@""];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:nil];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }



    self.productAmountLabel = [[UILabel alloc]init];
    self.productAmountTextLabel = [[UILabel alloc]init];

    if (IS_IPHONE_4_OR_LESS || IS_IPHONE_5)
    {
        self.productAmountLabel.frame = CGRectMake(75, 145, 100, 30);
        self.productAmountTextLabel.frame = CGRectMake(117, 145, 100, 30);

    }
    else if (IS_IPHONE_6)
    {

        self.productAmountLabel.frame = CGRectMake(75, 145, 100, 30);
        self.productAmountTextLabel.frame = CGRectMake(117, 145, 100, 30);
    }
    else if (IS_IPHONE_6P)
    {

        self.productAmountLabel.frame = CGRectMake(75, 145, 100, 30);
        self.productAmountTextLabel.frame = CGRectMake(117, 145, 100, 30);

    }




    self.productAmountLabel.text = @"Amount:";
    self.productAmountLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:10];

    //    self.productAmountTextLabel.text =  [NSString stringWithFormat:@"%@",amountArray[indexPath.row]];
    //    self.productAmountTextLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:10];

    self.productAmountTextLabel.text = amountArray[indexPath.row];
    self.productAmountTextLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:10];


    tableView.tableFooterView = [UIView new];

    cell.layer.borderWidth = 1.0;
    cell.layer.cornerRadius = 10;
    cell.layer.borderColor = [UIColor blackColor].CGColor;



    [cell.contentView addSubview:self.productAmountLabel];
    [cell.contentView addSubview:self.productAmountTextLabel];

    //    cell.layer.shadowColor = [[UIColor blackColor] CGColor];
    //    cell.layer.shadowOffset = CGSizeMake(0.0f,0.0f);
    //    cell.layer.shadowOpacity = 0.7f;
    //    cell.layer.shadowRadius = 4.0f;
    //[cell.layer setMasksToBounds:YES];

    return cell;
}

The problem is that, when I say amountArray[indexPath.row], it should display two unique values but it displays the same value multiple times. I have even looped around table view to check if it has two values and it does but while displaying it prints same value is displayed multiple times.

Do like below code :

 -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *indentifier = @"indentifier";

   // Here I am creating cell in XIB -----------

   Team_TableViewCell *Cell = (Team_TableViewCell *)[tableView dequeueReusableCellWithIdentifier:indentifier];
   if (Cell == nil)
   {
       NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Team_TableViewCell" owner:self options:nil];
       Cell = [nib objectAtIndex:0];
   }

   // My required lable--------
   Cell.titleLbl.text = reportees.name;
   Cell.pointEarnedLbl.text = reportees.points;
   Cell.titleLbl.textColor=APP_HOME_TABLE_HEADER_COLOR;
   [Cell setSelectionStyle:UITableViewCellSelectionStyleNone];
   [Cell setAccessoryType:UITableViewCellAccessoryNone];

   return Cell;
}

Note : Set the Indentifier in Tableview also if ur creating in XIB..

You need to add dequeueReusableCellWithIdentifier like this

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

{

static NSString *cellId=@"reuseid";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];

if (cell == nil)

{

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                  reuseIdentifier:cellId];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

}

self.productAmountLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, 20)];
self.productAmountTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(10,25, 100, 20)];


self.productAmountLabel.text = @"Amount:";
self.productAmountLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:10];


self.productAmountTextLabel.text = [amountArray objectAtIndex:indexPath.row];
self.productAmountTextLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:10];

tableView.tableFooterView = [UIView new];
cell.layer.borderWidth = 1.0;
cell.layer.cornerRadius = 10;
cell.layer.borderColor = [UIColor blackColor].CGColor;

[cell.contentView addSubview:self.productAmountLabel];
[cell.contentView addSubview:self.productAmountTextLabel];
return cell;

}

The problem when you perform addSubview on a UITableViewCell is that it will get added multiple times when cellForRow:atIndexPath is invoked.

You can solve the problem in two ways:

  1. Have a custom cell with the custom labels already added on init. In this case, you won't have to do addSubview in cellForRow .
  2. Remove all subviews in cellForRow before adding any subviews, like so:

      for (UIView *sView in cell.contentView.subviews) { [sView removeFromSuperview]; } 

As a quickfix, approach 2 will work, but approach 1 is better, since you avoid unnecessary adding/removing of subviews.

EDIT

On reconsideration, there is a third way. Initialize your labels earlier and add the labels as subviews within the (cell == nil) block:

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

    UITableViewCell *cell = nil;
    cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@""];
    self.productAmountLabel = [[UILabel alloc]init];
    self.productAmountTextLabel = [[UILabel alloc]init];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:nil];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        [cell.contentView addSubview: self.productAmountLabel];
        [cell.contentView addSubview: self.productAmountTextLabel];
    }

// remaining code, but do not call `addSubview` after this point

}

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