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?
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];
}
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.
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.
You are calling
[tableView numberOfRowsInSection:section];
You should be calling
[self numberOfRowsInSection:section];