【问题标题】:overwritting of contents in UItable View Cell in iosios中表格视图单元格内容的覆盖
【发布时间】:2012-07-25 20:01:57
【问题描述】:

在我的表格视图单元格中,我需要在单个单元格中显示我的数组元素的内容,如果我插入任何新内容,则不会覆盖以前的内容。以前的内容和我的新内容应该按顺序显示.这是我的代码

- (void)viewDidLoad
{
[super viewDidLoad];


NSArray *arr=[NSArray arrayWithObjects:cnfqty,cnftitle,cnfrate, nil];

//cnfqty,cnftitle,cnfrate are NSString

finarray=[[NSMutableArray alloc]init];
[finarray addObjectsFromArray:arr];
NSLog(@"%@",finarray);


}

- (void)viewDidUnload
{
[super viewDidUnload];

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [finarray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 40, 20)];
    lab.text=[finarray objectAtIndex:indexPath.row];
    [cell.contentView addSubview:lab];

}
// Configure the cell...

return cell;
}

我的代码面临的问题是每个字符串都显示在每个单元格中,如果我插入任何新字符串,以前的内容就会消失,只显示新内容。

为了清楚地了解这个问题,我正在尝试为在线购物实施“添加到购物车”服务。根据概念,必须从各种产品中添加项目,它会保存产品信息并显示详细信息在表格视图中。但我没有得到它..

请指导..提前谢谢..

【问题讨论】:

    标签: iphone objective-c ios xcode uitableview


    【解决方案1】:
    • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

         UITableViewCell *cell = (UITableViewCell*)[self.YourTableName dequeueReusableCellWithIdentifier:nil];        
      if (cell == nil) {
          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
      }
      
      
          return cell; 
      }
      

      使用 ReusablecellIdentifier nil 使其正常工作.....

    【讨论】:

      【解决方案2】:

      不确定您的 cellForRowAtIndexPath 是否正确。看起来您没有检查单元格是否为零。如果您是(不知何故在括号内),那么您似乎只是在该 if(cell == nil) 语句中更新 lab.text。

      它可能应该更像以下内容:

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
          static NSString *CellIdentifier = @"Cell";
          UILabel *lab; 
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
      
          if(cell == nil){
              cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];      
              lab=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 40, 20)];
              lab.tag = 1; // Really any arbitraty number
              [cell.contentView addSubview:lab];  
          }else{
              lab = [cell.contentView viewWithTag:1]
          }
      
          // Configure the cell... 
          lab.text=[finarray objectAtIndex:indexPath.row];      
      
          return cell; 
      }
      

      【讨论】:

      • 这段代码不会被编译,因为lab 在重用单元格的情况下将不存在。使用标签来引用重复使用的单元格可以解决这个问题(请参阅我的答案)。
      • 但是我的标签是在 if 条件里面声明的......我不能在循环之外访问它......
      • @Dima - 很好,我已经习惯了我的代码(带有示例)可供我使用!试图回答没有:-)谢谢
      • @Dima - 不再。认为这也会更快(每次都不需要分配一个单元格)!这也是许多网站上呈现的更标准的方式。谢谢你的收获。
      • 原来是这样,但你又编辑了。你是对的,只是它不是分配,只是指针分配。
      【解决方案3】:

      cellForRowAtIndexPath 中的代码有点不对劲。您应该在初始化之前检查单元格是否为零。好像您从某处复制了括号,但省略了条件。

      除此之外,您没有代码可以更改单元格的标签以使单元格被重用。您只需将文本设置在新文本上。一个容易跟踪标签以重复使用的方法是给它一个标签。

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(cell == nil)
        { // set up brand new cell
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
            UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 40, 20)];
            lab.tag = 150; // some int you can keep track of, possibly make it a define.
            [cell.contentView addSubview:lab];
        }
        // configure cell
        UILabel *cellLabel = (UILabel *)[cell.contentView viewWithTag:150]; // there's that tag again
        cellLabel.text = [finarray objectAtIndex:indexPath.row];
        return cell;
      }
      

      【讨论】:

      • 我在另一个示例中尝试了它,通过给出 if(cell==nil) 重复单元格图像并且图像从我的 json 文件加载。所以我随后不使用条件..
      • 您需要查看我的答案并将其与您尝试的内容进行比较,以了解为什么您尝试的内容不正确。请求一个单元格时可能会发生两件事:1)创建一个全新的单元格,2)重用现有单元格。然后,可以填充视图。将此功能复制/粘贴到您的上面,它应该可以正常工作。
      • 发生同样的问题..所有内容都显示在每个单元格中,并且警告显示为不兼容的指针类型
      • 查看我的编辑以修复该警告,只需将视图转换为标签。 “所有内容都显示在每个单元格中”是什么意思?
      • 我的 3 个字符串值加载到每个单元格中..但我需要的是所有字符串值需要显示在一个单元格中..我需要实现添加到购物车服务。我想你知道这一点..
      【解决方案4】:

      让你的“finarray”在每个对象中存储一个数组(我的意思是你需要一种二维数组)。在cellForRowAtIndexPath 中,您将通过indexPath.rowfinarray 中提取数组,并对提取的数组进行循环以填充您的内容。 finarray 中的每个数组第一次将仅包含 1 个对象。通过更改单元格中的内容,您实际上将在位于已编辑单元格索引处的数组中添加一个对象。然后,通过重新加载数据,表格将显示新添加的数据。确保管理已编辑单元格的行高,因为它们需要增加。

      - (void)viewDidLoad
      {
      [super viewDidLoad];
      
      //bla bla bla whatever you had here
      
      NSMutableArray *arr1=[NSMutableArray arrayWithObjects:cnfqty, nil];
      NSMutableArray *arr2=[NSMutableArray arrayWithObjects:cnftitle, nil];
      NSMutableArray *arr3=[NSMutableArray arrayWithObjects:cnfrate, nil];
      
      NSMutableArray *arrMain=[NSMutableArray arrayWithObjects:arr1, arr2, arr3, nil];
      
      
      finarray=[[NSMutableArray alloc]init];
      [finarray addObjectsFromArray:arr];
      
      }
      
      
      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
      
      //bla bla bla the code to initiate the cell
      
      NSArray *tmpArray = [finarray objectAtIndex:indexPath.row];
      
      for(int i = 0; i<tmpArray.count;i++){
        //create the label
        //set the frame, making sure that origin.y is multiplied by "i"
        //set label's text as [tmpArray objectAtIndex:i]
        //add the label to cell
      }
      
      return cell;
      }
      

      【讨论】:

      • 你能解释一些简短的..我明白你在说什么,但不能完全理解
      • 感谢您的宝贵回答,但我需要在同一个单元格中显示 3 个字符串。我该怎么做..
      • 而且它还覆盖了我以前的内容..有什么办法可以避免这种情况...我需要有我的旧内容..
      • @Dany,什么,见鬼,你不明白吗?你看到在“for”循环中创建的标签了吗?您将在子数组中有 100 个对象,因此您的单元格中将显示 100 个不同的标签,一个在另一个之下,它们看起来就像您的单元格中的 100 个不同的字符串
      • 而且,正如我所说,您必须定义单元格的高度,因为您将增加单元格中的内容,因此在您的 - (CGFloat) 中返回 cellHeight*(indexPath.row+1) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath,其中“cellHeight”是您在 xib 中设置的行的高度。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-07
      • 2018-08-19
      相关资源
      最近更新 更多