iOS-在表中显示数组值时出错

iOS-在表中显示数组值时出错,ios,uitableview,uiview,nsmutablearray,uilabel,Ios,Uitableview,Uiview,Nsmutablearray,Uilabel,在我的应用程序中,我创建了一个名为“array”的可变数组 “我的tableView”对于多个部分仅包含一行,每个部分都有一个仅包含一个标签的customView。 我的自定义视图名称是ContentOfCell ContentOfCell * contentOfCell=[[ContentOfCell alloc]initWithFrame:CGRectMake(0, 0, 100, 50)]; [cell.contentView addSubview:contentOfCell];

在我的应用程序中,我创建了一个名为“array”的可变数组

“我的tableView”对于多个部分仅包含一行,每个部分都有一个仅包含一个标签的customView。
我的自定义视图名称是ContentOfCell

ContentOfCell * contentOfCell=[[ContentOfCell alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
    [cell.contentView addSubview:contentOfCell];  
我已将数组添加到标签中

contentOfCell.label.text=[array objectAtIndex:indexPath.section];  
我的问题是,它只识别数组中的前四个值,前四个值在部分中重复

我觉得这里出了点问题

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
return [indexPath row]+50;
} 
如果我将50更改为100,如果没有正确插入值,则会发生错误

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * cellIdentifier=[NSString stringWithFormat:@"MyTableView"];
cell=[myTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell== nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
ContentOfCell * contentOfCell=[[ContentOfCell alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
    [cell.contentView addSubview:contentOfCell];
   contentOfCell.nameField.text=[arr objectAtIndex:indexPath.section];
    cell.contentView.backgroundColor=[UIColor whiteColor];
 }}
在这里,视图仅包含3行,当我向下滚动时,再次启动阵列,然后再次打印值

使用以下方法:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellIdentifier=@"CellIdentifier";//or your custom name
    UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell== nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        ContentOfCell * contentOfCell=[[ContentOfCell alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];

        contentOfCell.tag = 101;
        [cell.contentView addSubview:contentOfCell];
        cell.contentView.backgroundColor=[UIColor whiteColor];
     }

     ContentOfCell * contentOfCell = (ContentOfCell*)[cell.contentView viewWithTag:101];

     contentOfCell.nameField.text=[arr objectAtIndex:indexPath.section];

     return cell;
}

我猜你没有正确地重复使用电池

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   NSString * cellIdentifier=[NSString stringWithFormat:@"MyTableView"];
   cell=[myTableView dequeueReusableCellWithIdentifier:cellIdentifier];
   ContentOfCell *contentOfCell;
   //Initialization Part. declare objects here
   if(cell== nil)
   {
      //Creation part. Create your new cell here. Do all UI actions here
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
      contentOfCell=[[ContentOfCell alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
      contentOfCell.tag =100;
      [cell.contentView addSubview:contentOfCell];
      cell.contentView.backgroundColor=[UIColor whiteColor];
   }
   else{
      // Reusable part. Reuse the UI controls here from existing cell
      contentOfCell = (ContentOfCell *)[cell.contentView viewWithTag:100];
   }
   //Assign all the data here
   contentOfCell.nameField.text=[arr objectAtIndex:indexPath.section];
}
这是iOS新手经常犯的一个简单错误。 请查看此处的Apple developer文档,了解tableview单元格是如何重用的

我使用分组表实现了与下面相同的代码。一切正常。请将您的代码与下面的代码进行比较

- (void)viewDidLoad
{
[super viewDidLoad];
arrData=[[NSMutableArray alloc] initWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k", nil];

// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
   return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{ 
   return [arrData count];
}
 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
   return 50.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *aCell=[tableView dequeueReusableCellWithIdentifier:@"da"];
    if(aCell==Nil)
    {
      aCell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"da"];

      ContentOfCell *ContentOfCellObj=[[ContentOfCell alloc] initWithFrame:CGRectMake(0,0,320,50)];
      ContentOfCellObj.tag=101;
      [aCell.contentView addSubview:ContentOfCellObj];
     }
     ContentOfCell *ContentOfCellObj=(ContentOfCell*)[aCell.contentView viewWithTag:101];
     ContentOfCellObj.nameField.text=[NSString stringWithFormat:@"%@",[arrData objectAtIndex:indexPath.section]];

     return aCell;
}
一定要做好准备

myTableView.dataSource = self;
这两个委托方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
   return [arrData count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{ 
   return 1;
}

试试这段代码,它肯定能解决你的问题

步骤1。首先将数组声明为全局变量

@interface YOUR-CLASS-NAME ()
{
     NSMutableArray *array
}
步骤2。现在设置所需的节数。这意味着您希望连续重复表中的数组值多少次

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
步骤3。现在设置数组中需要显示的行数。这意味着不需要显示数组中的任何值

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [array count];
}

能否显示tableview方法的代码?请确保NumberOfSectionsTableView将等于数组计数a,在numberOfSection委托中,我已给出返回值[数组计数];显示您的代码
cellforrowatinexpath
?我想,
dequeueReusables
显示cellForRowAtIndexpathNo的代码时出错了,同样的事情发生了,只需在图像上抓拍一下,如果我向下滚动,接下来的行包含值a、b、c。直到部分的数量扫描你请检查这个链接,我已经给出了注释我想用坐标在地图上从字典中画出它们在那一页给我一个答案,k,你能告诉我我在这个表中犯了什么错误吗?很简单,你没有正确地重复使用单元格。可重用的概念在这里有点大,这就是为什么我给你苹果文档的链接。有关UItableviewcell的工作原理,请参考该网站或谷歌。如果我得到答案,我会验证它
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [array count];
}