iOS UITableView cellForRowAtIndexPath未调用

时间:2015-02-06 13:30:53

标签: ios xcode ios7 ios5

它的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,如果我给返回数组计数;或者返回11;它工作正常Belove我附上了我的代码,错误

*****由于未捕获的异常终止应用程序' 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;
             }
         }
    }
}

3 个答案:

答案 0 :(得分:2)

所以你说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. AS当你到达最后一个单元格时,应用程序将在上面一行崩溃。

编辑2:

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

中给出的+1

让我们说allOfficedata的计数是10(索引0-9),我们已经说过表中的行数是allOfficedata + 1,所以它的11(索引0-10)。

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

答案 1 :(得分:0)

NSArray超出界限..

  • 数组0到9中的10个值[ 1到10]
  • 在表索引中也是如此 相同的0到9 [ 1到10]

所以indexpath.row将为您提供正确值,以便在正确的位置确定数组中的值

答案 2 :(得分:0)

除了行计数错误之外,永远不会调用cellForRowAtIndexPath的最可能原因是表视图认为没有要显示的行。

只有在需要单元格时才会调用cellForRowAtIndexPath。你似乎有很高的单元格看着你的代码。无法查看节标题视图或高度的任何代码。

所以考虑到大的高度,特别是对于第0行(根据你的代码为300或400),除非你向上滚动,否则不会有很多调用这个函数。当您滚动时,您应该看到更多的细胞调用。

同时检查你是否为高度返回0,因为这也可能会阻止它试图显示我认为不会被看到的单元格。

它将要求每行的高度来估计所需的滚动条大小。