0

I'm calling the numberOfRowsInSection method of the UITableView delegate inside of the heightForRowAtIndexPath but it gives me a Bad Access Error:

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ...
    NSLog(@"number of rows in section: %i", [tableView numberOfRowsInSection:[indexPath section]]);
    ...
}

Can anybody tell me whats wrong here?

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
user944351
  • 1,213
  • 2
  • 19
  • 27
  • You should show us what you do at `...` since this might be relevant. For example do you actually return a value. Not doing this might lead to BAD ACCESS too. – Besi Dec 17 '12 at 10:47
  • ca u check this question http://stackoverflow.com/questions/31667986/ios-uitableview-numberofrowsinsection-in-tableview-heightforrowatindexpath-give – Nischal Hada Jul 28 '15 at 05:28

4 Answers4

2

You are calling an delegate method.

When you call this method, it'll invoke the related delegates method like cellForRowAtIndexPath, heightForRowAtIndexPath etc.

It'll cause an infinite loop that's why it's crashing.

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • ca u check this question http://stackoverflow.com/questions/31667986/ios-uitableview-numberofrowsinsection-in-tableview-heightforrowatindexpath-give – Nischal Hada Jul 28 '15 at 05:28
1

That's normal.. In that moment your UITableView is being created. You shouldn't call methods related to your UITableView, before it has been build. You should rely in other mechanism to get the height of your Cells.

Rui Peres
  • 25,741
  • 9
  • 87
  • 137
  • ca u check this question http://stackoverflow.com/questions/31667986/ios-uitableview-numberofrowsinsection-in-tableview-heightforrowatindexpath-give – Nischal Hada Jul 28 '15 at 05:36
1

You need to return an actual value here. So instead of calling [tableView numberOfRowsInSection:[indexPath section]] simply do the same thing again.

In order not to duplicate your code you could create a helper method which you can call in both places, namely, your heightForRowAtIndexPath method and the numberOfRowsInSection method:

- (int) myNumberOfRowsMethod{
    // Here you would usually have some underlying model 
    return [self.myContactsOrWhateverArray count];
}
Besi
  • 22,579
  • 24
  • 131
  • 223
  • ca u check this question http://stackoverflow.com/questions/31667986/ios-uitableview-numberofrowsinsection-in-tableview-heightforrowatindexpath-give – Nischal Hada Jul 28 '15 at 05:28
0

You are calling

[tableView numberOfRowsInSection:section];

You should be calling

[self numberOfRowsInSection:section];
Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • Again with the downvote and no comment!? This works and is a hell of a lot easier than the selected answer. Oh well. – Fogmeister Dec 17 '12 at 11:43
  • Yeah. This works and I don't know what's with the downvote. I was calling `[self.tableView numberOfRowsInSection: section];` and using `[self tableView: self.tableView numberOfRowsInSection: section];` solved the problem. Thanks :) – sleighty Jun 11 '15 at 04:34