Search code examples
iosxcodeios7ios5

iOS UITableView cellForRowAtIndexPath Not Calling


It's UITableView numberOfRowsInSection not calling. but cellForRowAtIndexPath is not being called. I don't understand why cellForRowAtIndexPath is not called. if i retun number of row in section

 return (allOfficedata.count >0)? allOfficedata.count + 1 : allOfficedata.count; 

or

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

is not calling cellforrowatindexpath, If i give return Array count; or return 11; its working fine Belove i attached my code, Error

***** Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 10 beyond bounds [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;
             }
         }
    }
}

Solution

  • So your saying allOfficedata + 1 for numberOfRowsInSection but in heightForRowAtIndexPath: you have the following line:

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

    This causes your crash on the last cell, so you want to check is it the last cell and if it is do something else.

    Edit:

    As well as that you seem to be doing a similar thing in cellForIndexPath,

    you do something if (indexPath == 0) but after that for the next cell you get:

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

    you should - 1 from the indexPath.row, as currently your get for the cell at, lets say indexPath.row == 1, your asking for the object at index 1 where, I think you should ask for 0. AS it stands when you get to the last cell the app will crash on the above line.

    Edit 2:

    So the problem is your asking allOfficedata for an index that is not there, when you say allOfficedata+1 for the rows and when it gets to the heightForRowAtIndexPath for the last cell in the table it asks for the data out of allOfficedata for index that is not there, because its looking for the +1 you gave in numberOfRowsInSection

    So lets say the count of allOfficedata is 10 (index 0-9) and we've said the number of rows in the table is allOfficedata+1, so its 11 (index 0-10).

    Now in heightForRowAtIndexPath we get to last cell(index 10), allOfficedata doesn't have it because its index is 0-9. you ask allOfficedata for index 10 it will crash.