0

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'

OptimusCrime
  • 14,662
  • 13
  • 58
  • 96
  • 1
    As you can read in http://stackoverflow.com/questions/10043521/adding-unknown-number-of-rows-to-static-cells-uitableview?rq=1 you have to override every tableview delegate method. May be in that issue? – stosha Jun 14 '13 at 02:28
  • is ther number of section is Static..? – Nitin Gohel Jun 14 '13 at 05:07
  • Might be easier to tell with the whole class...where are you using the objectAtIndex method, that crash means you're trying to reference an object in an array thats empty – Chris Tetreault Jun 14 '13 at 18:01

0 Answers0