OGeek|极客世界-中国程序员成长平台

标题: ios - cellForRowAtIndexPath 数据源方法的问题 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 14:41
标题: ios - cellForRowAtIndexPath 数据源方法的问题

我的问题如下:我为 cellForRowAtIndexPath 分配了一个可变数组,以便它在一个单元格中显示每个数组对象。到目前为止一切顺利,单元格按预期显示。现在我想在第一个单元格中显示(根据条件) UILabel ,以便其他可变数组对象将移动到第二个单元格和第三个单元格,等等。 问题是,当我测试该条件时,它是真的,UILabel 显示在第一个单元格中第一个对象。所以实际上,两个元素在同一个单元格中,这不是我所期望的。我想(当条件为真时)移动所有元素,以便它们从第二个单元格显示,以便将第一个单元格留给 UIlabel

我的相关代码没有给我我的期望,是这样的:

- (UITableViewCell*)tableViewUITableView *)_tableView cellForRowAtIndexPathNSIndexPath *)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;
}

我的逻辑是否遗漏了什么?提前谢谢。



Best Answer-推荐答案


您将 indexPath.row 直接与模型索引相关联,此时它们应该为“标题”单元格偏移。

if (indexPath.row && (indexPath.row - 1) < cellList.count) {
            [cell addSubview:[cellList objectAtIndex:indexPath.row - 1]];
} else {

关于ios - cellForRowAtIndexPath 数据源方法的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11465923/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4