I have this piece of code that is taken from several similar questions on SO and I have no idea why it is crashing. I have followed examples from accepted and obviously correct answers.
I have a static table where the first section of the table holds the only static row. The second section is empty at first and is supposed to be populated by an async-task later. First numOfDynamicRows
is sat to zero, and when the async-task is done, this value is changed to the length of the dict I get back. Everything crashes when I use reloadData
. I have tried to debug the code and it looks like it is crashing when it comes to the section it is supposed to add new rows.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
return 1;
else
return numOfDynamicRows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
int section = indexPath.section;
int row = indexPath.row;
if (section == 0) {
return [super tableView:tableView cellForRowAtIndexPath:indexPath];
}
else {
HomeFeedCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:@"myCustomCell"];
if (!cell)
{
// create a cell
cell = [[HomeFeedCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCustomCell"];
}
return cell;
}
}
It is crashing with the follow message (which is confusing me a lot):
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array'