Ios 表视图的每个单元格多次显示相同的值

Ios 表视图的每个单元格多次显示相同的值,ios,uitableview,Ios,Uitableview,下面是我的cellForRowAtIndexPath方法 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = nil; cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@""];

下面是我的cellForRowAtIndexPath方法

- (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;
}
问题是,当我说amountArray[indexPath.row]时,它应该显示两个唯一的值,但它多次显示相同的值。我甚至打了个圈
“环绕表”视图,检查它是否有两个值,但在显示它时,相同的值会显示多次。

您需要像这样添加dequeueReusableCellWithIdentifier

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

{

}请执行以下代码:

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

注意:如果在XIB中创建,也可以在Tableview中设置标识符。

UITableViewCell
上执行
addSubview
时的问题是,调用
cellForRow:atIndexPath
时会多次添加标识符

您可以通过两种方式解决此问题:

  • 在init上已经添加了自定义标签的自定义单元格。在这种情况下,您不必在
    cellForRow
    中执行
    addSubview
  • 在添加任何子视图之前,请删除
    cellForRow
    中的所有子视图,如下所示:

     for (UIView *sView in cell.contentView.subviews) {
    
          [sView removeFromSuperview];
     }
    
  • 作为一种快速修复方法,方法2可以工作,但方法1更好,因为可以避免不必要地添加/删除子视图

    编辑

    再考虑一下,还有第三种方法。提前初始化标签,并将标签作为子视图添加到
    (cell==nil)
    块中:

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

    您没有添加dequeueReusableCellWithIdentifier。只需将单元格=(UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:@“identifier”];在该方法中本地创建productAmountLabel。不要使其成为全局性的。@SRNayak您能解释一下吗?
    “无法将标识符为的单元格出列-必须为标识符注册nib或类,或者在情节提要中连接原型单元格”***第一次抛出调用堆栈:
    您可以像这样在viewdidload方法中注册[self.tableView registerClass:[UITableViewCell class]forCellReuseIdentifier:@“reuseid”];还是同样的问题你能在本地创建所有标签吗?你能把这封邮件上的代码发给我吗imindpixel@gmail.comWhich你试过了吗?我试过这两种方法,但同样的问题仍然存在,兄弟
    - (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
    
    }