【问题标题】:cells repeating other section values when scroll滚动时重复其他部分值的单元格
【发布时间】:2012-06-01 07:41:05
【问题描述】:

我已经解决这个问题 2 天了。我参考了一些 SO 问题,例如 Link 1 。问题是我有 2 行和数组计数部分的组表。当我滚动表格时,意味着第 4 部分重复了第 1 部分的值,但它在第 3 部分中效果很好。这两行都取第一节行的值

我的代码是:

static NSString *CellIdentifier = @"Cell";
cell = (CopyableTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[CopyableTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 
                                         reuseIdentifier:CellIdentifier] autorelease];
}

cell.delegate = self;

/*UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];

//cell.detailTextLabel.lineBreakMode=UILineBreakModeWordWrap;
cell.detailTextLabel.numberOfLines=2;
cell.detailTextLabel.font=[UIFont systemFontOfSize:14];*/


// Configure the cell...
DLog(@"");
//cell setBackgroundColor:[UIColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:0.8]]
cell.textLabel.textColor = [UIColor blackColor];//[UIColor colorWithRed:0.22 green:0.33 blue:0.53 alpha:0.8];
cell.detailTextLabel.textColor = [UIColor blackColor];  
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    l=indexPath.section;

    switch (indexPath.row) {
        case 0: 
            cell.textLabel.text =@"Quantity";


            if (indexPath.section ==k) {
                    cell.detailTextLabel.text = self.qtyValue;
                    [cell.detailTextLabel setTag:indexPath.section];

            }

        break;
        case 1:
            cell.textLabel.text = @"Unit";



            if (indexPath.section ==j) {
                cell.detailTextLabel.text = [self.unitValue valueForKey:@"val"];
                [cell.detailTextLabel setTag:indexPath.section];                
            }


            break;


        default:
            break;
    }

任何人都可以为此提供解决方案

【问题讨论】:

  • 我在 j 和 k 右侧有 count 个节,代表节中的相应单元格。即第一个和第二个单元格
  • 但是您将 j & k 与 section 而非 row 进行比较。如果 j & k 表示单元格,那么您应该将其与 indexPath.row 进行比较,而不是与 indexPath.section 进行比较。
  • 实际上 j&k 代表单元格,但我们在单元格中有多个具有不同值的部分。

标签: iphone ios uitableview sections


【解决方案1】:

如果您的要求是,您有多个部分,每个部分中有 2 行/单元格,那么我宁愿建议您在 cellforRowAtIndexPath 中编写这样的代码:

switch(indexPath.row)
{
   case 0:   
   {
      switch(indexPath.section)
      {
          case 0:
          {
             //Statement needs to execute for cell0 in section 0.
          }
            break;
          case 1:
          {
             //Statement needs to execute for cell0 in section 1.
          }
            break;
          case 2:
          {
             //Statement needs to execute for cell0 in section 2.
          }
            break;
          case 3:
          {
             //Statement needs to execute for cell0 in section 3.
          }
            break;
      } 
   }
    break;
   case 1:   
   {
      switch(indexPath.section)
      {
          case 0:
          {
             //Statement needs to execute for cell1 in section 0.
          }
            break;
          case 1:
          {
             //Statement needs to execute for cell1 in section 1.
          }
            break;
          case 2:
          {
             //Statement needs to execute for cell1 in section 2.
          }
            break;
          case 3:
          {
             //Statement needs to execute for cell1 in section 3.
          }
            break;
      } 
   }
     break;
 default:
     break;
}

【讨论】:

  • 但我的问题是我的部分是动态的。我知道我会有多少部分。
  • 根据您的代码,我可以看到只有 cell.detailTextLabel setTag:indexPath.section] 取决于部分,而 cell.detailTextLabel.text 采用恒定值。因此,无论部分如何,单元格都会显示相同的值.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-05
  • 1970-01-01
  • 1970-01-01
  • 2013-11-11
  • 2011-06-02
  • 1970-01-01
相关资源
最近更新 更多