Ios cellForRowAtIndexPath数据源方法存在问题

Ios cellForRowAtIndexPath数据源方法存在问题,ios,uitableview,Ios,Uitableview,我的问题如下:我将一个可变数组分配给cellforrowatinexpath,以便它显示单元格中的每个数组对象。到目前为止,这些单元格的显示情况与预期一致。现在,我希望在第一个单元格中显示(根据条件)一个UILabel,以便将其他可变数组对象移到第二个单元格,然后移到第三个单元格,以此类推。 问题是,当我测试该条件时,它是真的,UILabel与第一个对象一起显示在第一个单元格。所以实际上,两个元素在同一个单元格中,这不是我所期望的。我希望(当条件为真时)移动所有元素,以便将它们从第二个单元格显示

我的问题如下:我将一个可变数组分配给
cellforrowatinexpath
,以便它显示单元格中的每个数组对象。到目前为止,这些单元格的显示情况与预期一致。现在,我希望在第一个单元格中显示(根据条件)一个
UILabel
,以便将其他可变数组对象移到第二个单元格,然后移到第三个单元格,以此类推。 问题是,当我测试该条件时,它是真的,
UILabel
与第一个对象一起显示在第一个单元格。所以实际上,两个元素在同一个单元格中,这不是我所期望的。我希望(当条件为真时)移动所有元素,以便将它们从第二个单元格显示出来,以便将第一个单元格保留到
UIlabel

我的相关代码没有给出我所期望的,是:

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


     UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"any-cell"];


 // Add and display the Cell     
      cell.tag = [indexPath row];
      NSLog(@"cell.tag= %i",cell.tag);
      //test the condition, if it's ok, then add the label to the first cell
      if ([self isNoScoreLabelDisplayed] && cell.tag==0) {
        UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 220, 50)];
        [lbl setBackgroundColor:[UIColor greenColor]];
        [cell addSubview:lbl];
      }
    cell.tag = [self isNoScoreLabelDisplayed]?[indexPath row]+1:[indexPath row];//here i wanted to shift the tags in case the condition is true, so that all the elements will be displayed from the second cell. But seems not doing what i want :(


      //
      if (indexPath.row < cellList.count) {

            [cell addSubview:[cellList objectAtIndex:[indexPath row]]];//cellList is the mutable array from which i get all the elements to display in the cells

      }else{

            [cell addSubview:nextButton];
      }


      return cell;
}
-(UITableViewCell*)tableView:(UITableView*)\u tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
UITableViewCell*单元格=[[U tableView dequeueReusableCellWithIdentifier:@“任意单元格];
//添加并显示单元格
cell.tag=[indexPath行];
NSLog(@“cell.tag=%i”,cell.tag);
//测试条件,如果可以,则将标签添加到第一个单元格
if([self-isNoScoreLabelDisplayed]&&cell.tag==0){
UILabel*lbl=[[UILabel alloc]initWithFrame:CGRectMake(0,0,220,50)];
[lbl setBackgroundColor:[UIColor greenColor]];
[单元添加子视图:lbl];
}
cell.tag=[self-isNoScoreLabelDisplayed]?[indexPath行]+1:[indexPath行];//这里我想在条件为真的情况下移动标记,以便从第二个单元格显示所有元素。但似乎没有做我想做的事情:(
//
if(indexPath.row

我的逻辑中是否遗漏了某些内容?请提前检查。

您可以将标签文本作为第一个元素插入容器中并进行检查。这样您就不需要索引偏移和代码的进一步复杂化

例如


您将indexath.row直接与模型索引相关联,此时“title”单元格应该偏移这些索引

if(indexPath.row&&(indexPath.row-1)
我将为此使用两个单元格标识符,一个用于LabelCell,一个用于常规ArrayCell,这将为u清除问题,而u将不会得到带有标签和对象的单元格


另外,我也不知道你在做什么,但看起来你每次都在向一个单元格添加子视图,但你不会在任何地方删除它们。别忘了单元格会被重用…

嗨,在我看来,你检查了标签条件,然后在可变数组中添加了相同的对象,因为第一行的indexath.row==0
cell.tag = [self isNoScoreLabelDisplayed]?[indexPath row]+1:[indexPath row];//here i wanted to shift the tags in case the condition is true, so that all the elements will be displayed from the second cell. But seems not doing what i want :(
因此,如果必须显示标签,上面的代码只需将单元格标记设置为indexpath.row+1,但下面的代码(请记住,第一次indexpath.row==0,因此即使显示了标签)将相同的标记添加到数组对象:-)

if(indexPath.row
据我所知,你想要一个类似标题的东西-为什么不使用表格的headerview?嗨,thanx作为你的回复。实际上标题在做其他事情,它显示了我的游戏名称。无法使用标题:))创建的
单元格在哪里?你能扩展代码吗?@ctrahey:我扩展了我的代码,该单元格是使用相同的数据源方法创建的,请检查:)谢谢。注意:请记住在单元格的
prepareforeuse
方法中正确清理,或者对两种类型的单元格使用两个不同的重复使用池(更好).Thanx作为回答,您能进一步澄清吗?您所说的
//添加所需标签是什么意思?您的意思是将标签添加到
单元格:addSubview
?类似这样的内容:
[cell addSubview:myLabel];
?不,像添加到代码中一样添加它(创建标签,从
[cellList objectAtIndex:[indexath row]设置其文本)
&将其作为子视图添加到单元格中)。在这种情况下,
atIndex:0
将删除实际值,并将
UILabel
替换为。是否有方法移动所有值并插入第一个索引?它不会删除它,它将移动所有元素。谢谢您的回复。不要担心变量状态的
FALSE
检查,我已经检查过了,并且当它为
FALSE
时,UILabel根本不显示,一切正常。我的问题仍然处于变量的
TRUE
状态。
if (indexPath.row && (indexPath.row - 1) < cellList.count) {
            [cell addSubview:[cellList objectAtIndex:indexPath.row - 1]];
} else {
cell.tag = [self isNoScoreLabelDisplayed]?[indexPath row]+1:[indexPath row];//here i wanted to shift the tags in case the condition is true, so that all the elements will be displayed from the second cell. But seems not doing what i want :(
if (indexPath.row < cellList.count)