0

I have receive a EXC Bad Access error when I enter the if statement below [tableView numberOfRowsInSection:0].I'm calculating the tableView numberOfRowsInSection of the UITableView inside the heightForRowAtIndexPath .

I have got following code to set height for last row of first section

-(CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

        if (indexPath.section==0) {
            NSInteger lastRowIndex = [tableView numberOfRowsInSection:0] - 1;

            if (indexPath.row==lastRowIndex) {
                return 44.0f;

            }else{
                return 100;

            }

        }else{

            return 100;
        }


}

I got the error as below image

enter image description here

I didnt find this method appropriate

iOS UITableView numberOfRowsInSection BAD ACCESS

- (int) myNumberOfRowsMethod{
    // Here you would usually have some underlying model 
    return [self.myContactsOrWhateverArray count];
}
Community
  • 1
  • 1
Nischal Hada
  • 3,230
  • 3
  • 27
  • 57
  • cant we use some thing like tableView numberOfRowsInSection that method works better then array count – Nischal Hada Jul 28 '15 at 05:32
  • what you have in `numberOfRowsInSection` method? i think instead of `[tableView numberOfRowsInSection:0] - 1;` you can use `[self myNumberOfRowsMethod] - 1;` – Akhilrajtr Jul 28 '15 at 05:32
  • I didnt want to use myNumberOfRowsMethod i want to use some better methods so bro – Nischal Hada Jul 28 '15 at 05:41
  • Can you Post your - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section method's Code? – VD Patel Jul 28 '15 at 05:55
  • - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (section==0) { return [ storeArr count]+1; }else{ return [ [[itemArr objectAtIndex:section-1] objectForKey:@"itemList"]count]; } } – Nischal Hada Jul 28 '15 at 05:59
  • possible duplicate of [Calling numberOfRowsInSection: from heightForRowAtIndexPath](http://stackoverflow.com/questions/4381805/calling-numberofrowsinsection-from-heightforrowatindexpath) – oarfish Jul 28 '15 at 06:03
  • How many no.of rows are there for 1st section ? – poojathorat Jul 28 '15 at 06:12
  • can u check this question http://stackoverflow.com/questions/31692692/what-could-we-do-in-case-of-custom-cell-in-viewforheaderinsection-to-reload-part – Nischal Hada Jul 29 '15 at 06:42

5 Answers5

2

Try this method [self tableView:tableView numberOfRowsInSection:0] instead of [tableView numberOfRowsInSection:0]. It's works fine.

Dharmesh Dhorajiya
  • 3,976
  • 9
  • 30
  • 39
Vishnuvardhan
  • 5,081
  • 1
  • 17
  • 33
1

Update your code to following

-(CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

        if (indexPath.section==0) {
            NSInteger lastRowIndex = [self numberOfRowsInSection:indexPath.section] - 1;

            if (indexPath.row==lastRowIndex) {
                return 44.0f;

            }else{
                return 100;

            }

        }else{

            return 100;
        }
}
Ashish P.
  • 848
  • 5
  • 12
  • what the difference in your answer compare to the question , already condition is validated isEqual to 0\ – Anbu.Karthik Jul 28 '15 at 05:38
  • can u check this question http://stackoverflow.com/questions/31692692/what-could-we-do-in-case-of-custom-cell-in-viewforheaderinsection-to-reload-part – Nischal Hada Jul 29 '15 at 06:42
1

Please Try With This. Make one Global NSinteger firstSectionCount; and then try with this.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (section==0){
    firstSectionCount = [ storeArr count];
    return [ storeArr count]+1;
}else{
    return [ [[itemArr objectAtIndex:section-1]   objectForKey:@"itemList"]count];
}

and then,,,,

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section==0) {
    if (indexPath.row==firstSectionCount) {
        return 44.0f;

    }else{
        return 100;

    }
}
else{
      return 100;  
}

Hope this will help you...

VD Patel
  • 286
  • 1
  • 6
  • can u check this question http://stackoverflow.com/questions/31692692/what-could-we-do-in-case-of-custom-cell-in-viewforheaderinsection-to-reload-part – Nischal Hada Jul 29 '15 at 06:42
0

Try this code:

-(CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

        if (indexPath.section==0) {
            long lastRowIndex = [self.yourArray count]-1;

            // other wise use this line
           // long lastRowIndex = [self.yourArray lastObject]-1;

            if (indexPath.row==lastRowIndex) {
                return 44.0f;

            }else{
                return 100;

            }

        }else{

            return 100;
        }
}

Because this numberOfRowsInSection not work heightForRowAtIndexPath delegate method.

Dharmesh Dhorajiya
  • 3,976
  • 9
  • 30
  • 39
0

Try this..

 -(CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section==0) {
        NSInteger lastRowIndex = yourArray.count - 1; 

        if (indexPath.row==lastRowIndex) {
            return 44.0f;

        }else{
            return 100;

        }

    }else{

        return 100;
    }
}
ajay goel
  • 104
  • 4
  • can u check this question http://stackoverflow.com/questions/31692692/what-could-we-do-in-case-of-custom-cell-in-viewforheaderinsection-to-reload-part – Nischal Hada Jul 29 '15 at 06:42