iOS UITableView cellForRowAtIndexPath 未调用

iOS UITableView cellForRowAtIndexPath Not Calling

UITableView numberOfRowsInSection 没有调用。但未调用 cellForRowAtIndexPath。我不明白为什么不调用 cellForRowAtIndexPath。如果我重新调整部分

中的行数
 return (allOfficedata.count >0)? allOfficedata.count + 1 : allOfficedata.count; 

return [array count]+1;  
 if (allOfficedata.count >0) return [allOfficedata count] + 1;

没有调用 cellforrowatindexpath,如果我给 return 数组计数;或 return 11;它工作正常亲爱的我附上了我的代码,错误

***** 由于未捕获的异常 'NSRangeException' 而终止应用程序,原因:'* -[__NSArrayM objectAtIndex:]:索引 10 超出范围 [0 .. 9]'

-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView==self.LeftMenuTable)
        return leftOptions.count;
    return [allOfficedata count]+1;

}
**********************************************************************
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView==self.LeftMenuTable)
        return 60;

    Feedmodel *fmodel=[allOfficedata objectAtIndex:indexPath.row];
    NSString  *checkType =  fmodel.achievementType;
    if (isPad)
    {
        if (indexPath.row == 0)
        {
            return (isPad)?300.0:434.0;
        }
        else if (indexPath.row == allOfficedata.count+1)
          {return 30.0;}
        else{
        if (![checkType isEqualToString:@""] || [checkType isEqualToString:@"0"] || [checkType isEqualToString:@"1"])
            return ([fmodel.feedCommentCount integerValue] > 0)?366:318;
        else
            return  ([fmodel.feedCommentCount integerValue] > 0)?249:200;
        }
        return 0.0;
    }
    else
    {
        if (indexPath.row == 0)
        {
            return (isPad)?232.0:434.0;
        }
        else
        {
            if (![checkType isEqualToString:@""] || [checkType isEqualToString:@"0"] || [checkType isEqualToString:@"1"])
                return ([fmodel.feedCommentCount integerValue] )?259:222;
            else
                return  ([fmodel.feedCommentCount integerValue])?184:142;
        }
        return 0.0;
    }
}
***************************************************************
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier3 = @"loadMore";
    static NSString *CellIdentifier = @"TableViewCell";

    if (tableView==self.LeftMenuTable)
    {
        TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
            cell = [topLevelObjects objectAtIndex:0];

            UIView *sepView=[[UIView alloc ]initWithFrame:CGRectMake(0,59, 250, 1)];
            [cell.contentView addSubview:sepView];
            [sepView setBackgroundColor:[UIColor grayColor]];
        }

        cell.cellTextLabel.textColor=[UIColor whiteColor];
        [cell.cellTextLabel setText:[[leftOptions objectAtIndex:indexPath.row] valueForKey:@"name"]];
        [cell.CellImageView setImage:[UIImage imageNamed:[[leftOptions objectAtIndex:indexPath.row] valueForKey:@"image"]]];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.backgroundColor=[UIColor clearColor];

        return cell;
    }
    else
    {
        NSUInteger row = [indexPath row];
        NSInteger countRoe = [allOfficedata count];
          if (row == countRoe )
          {


              UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier3];
              if (cell == nil) {
                  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier3];
              }
              cell.textLabel.text = @"LoadMore";
              return cell;

          }else
         {
             if (indexPath.row == 0)
             {
                 //CellCallOutBox_Onboarding_iPad
                 //CellCalloutbox_Office_iPad
                 //CellCalloutbox_Office_iPhone

                 NSString *nibName;
                 if ([calloutOffice.is_empty_daily_mission isEqualToString:@"false"]) {
                     nibName = (isPad)?@"CellCalloutbox_Office_iPad":@"CellCalloutbox_Office_iPhone";

                 }
                 else if ([calloutOffice.is_empty_onboarding_mission isEqualToString:@"false"])
                 {
                     nibName = (isPad)?@"CellCallOutBox_Onboarding_iPad":@"CellCallOutBox_Onboarding_iPhone";
                 }
                 else if ([calloutOffice.is_empty_community_mission isEqualToString:@"false"])
                 {
                     nibName = (isPad)?@"CellCallOutBox_Onboarding_iPad":@"CellCallOutBox_Onboarding_iPhone";
                 }

                 CellCalloutbox_Office *cellcallout = (CellCalloutbox_Office *)[tableView dequeueReusableCellWithIdentifier:nibName];

                 if (cellcallout == nil)
                 {
                     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:nibName owner:self options:nil];
                     cellcallout = [nib objectAtIndex:0];
                 }
                 cellcallout.aMissionDelegate = self;
                 [cellcallout.btnVideoOfTheDay addTarget:self action:@selector(videoOfTheDay) forControlEvents:UIControlEventTouchUpInside];
                 cellcallout.backgroundColor=[UIColor clearColor];
                 [cellcallout setArraywithCallOut:calloutOffice expanded:isCallOutOfficeExpanded];
                 [cellcallout.btnBackCallout addTarget:self action:@selector(calloutBack) forControlEvents:UIControlEventTouchUpInside];
                 [cellcallout.btnDailyMission addTarget:self action:@selector(openCalloutDilyMission) forControlEvents:UIControlEventTouchUpInside];

                 return cellcallout;
             }


             Feedmodel *fmodel=[allOfficedata objectAtIndex:indexPath.row];
             NSString  *checkType =  fmodel.achievementType;

             if (![checkType isEqualToString:@""] || [checkType isEqualToString:@"0"] || [checkType isEqualToString:@"1"])

             {
                 static NSString *simpleTableIdentifier = @"CellWithImageVideo";

                 CellWithImageVideo *cell = (CellWithImageVideo *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
                 NSString *nibNmaeForiPad = (([fmodel.feedCommentCount integerValue])?@"CellWithImage_Comment_iPad":@"Cell_WithImage_MyOffice_iPad");
                 NSString *nibNmaeForiPhone = (([fmodel.feedCommentCount integerValue])?@"CellWithImagewithComment_iPhone":@"CellWithImageVideo");
                 if (cell == nil)
                 {
                     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:(isPad)?nibNmaeForiPad:nibNmaeForiPhone owner:self options:nil];
                     cell = [nib objectAtIndex:0];
                 }
                 cell.backgroundColor=[UIColor clearColor];
                 cell.contentView.backgroundColor=[UIColor clearColor];
                 [cell setFeeddata:fmodel indexPath:indexPath];


                 [cell.hifiveBtn         addTarget:self action:@selector(hifiveClickedMyOfiice:) forControlEvents:UIControlEventTouchUpInside];
                 [cell.playMintBtn       addTarget:self action:@selector(getFeedDetails:) forControlEvents:UIControlEventTouchUpInside];
                 [cell.planToDoBtn       addTarget:self action:@selector(planToDoClickedOffice:) forControlEvents:UIControlEventTouchUpInside];
                 [cell.userImageBtn      addTarget:self action:@selector(gotouserProfile:) forControlEvents:UIControlEventTouchUpInside];
                 [cell.inspireBtn        addTarget:self action:@selector(InspireClicked:) forControlEvents:UIControlEventTouchUpInside];
                 [cell.sendCommentBtn    addTarget:self action:@selector(serverCommentPost:) forControlEvents:UIControlEventTouchUpInside];
                 [cell.btnCompliment     addTarget:self action:@selector(getFeedCommentPopup:) forControlEvents:UIControlEventTouchUpInside];

                 cell.txtFldCommentField.delegate  = self;
                 return  cell;
             }
             else
             {
                 static NSString *simpleTableIdentifier = @"CellWithoutImage";
                 NSString *nibNmaeForiPad = (([fmodel.feedCommentCount integerValue])?@"Cell_withoutImage_withcomment_iPad":@"Cell_withoutImage_Ipad_Office");
                 NSString *nibNmaeForiPhone = (([fmodel.feedCommentCount integerValue])?@"CellWithoutImagewithComment_iPhone":@"CellWithoutImage");


                 CellWithoutImage *cell = (CellWithoutImage *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
                 if (cell == nil)
                 {
                     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:(isPad)?nibNmaeForiPad:nibNmaeForiPhone owner:self options:nil];
                     cell = [nib objectAtIndex:0];
                 }
                 [cell setFeeddata:fmodel indexPath:indexPath];
                 cell.backgroundColor=[UIColor clearColor];
                 cell.contentView.backgroundColor=[UIColor clearColor];



                 [cell.hifiveBtn         addTarget:self action:@selector(hifiveClickedMyOfiice:)           forControlEvents:UIControlEventTouchUpInside];
                 [cell.planToDoBtn       addTarget:self action:@selector(planToDoClickedOffice:)           forControlEvents:UIControlEventTouchUpInside];
                 [cell.userImageBtn      addTarget:self action:@selector(gotouserProfile:)                 forControlEvents:UIControlEventTouchUpInside];
                 [cell.inspireBtn        addTarget:self action:@selector(InspireClicked:)                  forControlEvents:UIControlEventTouchUpInside];
                 [cell.btnCompliment     addTarget:self action:@selector(getFeedCommentPopup:)                   forControlEvents:UIControlEventTouchUpInside];
                 [cell.sendCommentBtn     addTarget:self action:@selector(serverCommentPost:)               forControlEvents:UIControlEventTouchUpInside];
                 [cell.btnTextMintTapped    addTarget:self action:@selector(getFeedDetails:)               forControlEvents:UIControlEventTouchUpInside];

                 cell.txtFldComment.delegate = self;
                 return cell;
             }
         }
    }
}

所以你说 allOfficedata + 1 for numberOfRowsInSection 但在 heightForRowAtIndexPath: 你有以下行:

 Feedmodel *fmodel=[allOfficedata objectAtIndex:indexPath.row];

这会导致您在最后一个单元格上崩溃,因此您想检查它是否是最后一个单元格以及是否执行其他操作。

编辑:

此外,您似乎在 cellForIndexPath 中做了类似的事情,

如果 (indexPath == 0),你会做一些事情,但在那之后你会得到下一个单元格:

 Feedmodel *fmodel=[allOfficedata objectAtIndex:indexPath.row];

你应该 - 来自 indexPath.row 的 1,因为当前你获取的单元格位于 indexPath.row == 1,你要求索引 1 处的对象,我想你应该要求 0。当你到达最后一个单元格时,应用程序将在上面的行上崩溃。

编辑 2:

所以问题是你向 allOfficedata 询问一个不存在的索引,当你为行说 allOfficedata+1 时,当它到达 table 中最后一个单元格的 heightForRowAtIndexPath 时,它要求allOfficedata 中不存在索引的数据,因为它正在寻找您在 numberOfRowsInSection

中提供的 +1

所以假设 allOfficedata 的计数是 10(索引 0-9)并且我们说 table 中的行数是 allOfficedata+1,所以它是 11(索引 0-10) .

现在在 heightForRowAtIndexPath 中我们到达最后一个单元格(索引 10),allOfficedata 没有它,因为它的索引是 0-9。你向 allOfficedata 询问索引 10 它会崩溃。

NSArray 越界..

  • 数组 0 到 9 中有 10 个值 [不是 1 到 10]
  • 在table索引也是 相同的 0 到 9 [not 1 到 10]

所以 indexpath.row 会给你正确的值来确定数组中正确位置的值

cellForRowAtIndexPath 从未被调用的最可能原因,除了行计数错误之外,是 table 视图认为没有要显示的行。

cellForRowAtIndexPath 仅在需要单元格时调用。查看您的代码时,您似乎有非常高的单元格。无法看到部分 header 视图或高度的任何代码。

因此,考虑到较大的高度,尤其是第 0 行(根据您的代码为 300 或 400),除非您向上滚动,否则不会多次调用此函数。当您滚动时,您应该会看到更多对单元格的调用。

还要检查你是否 return 高度为 0,因为这也可能会阻止它尝试显示我认为不会被看到的单元格。

它会询问每行的高度来估计所需的滚动条大小。