1

I have an iOS 7/8 application. In a view I have a static UITableView with given number of cells - 17 in my case.
One of the cells contains another UITableView with dynamic cells. In the case described they are 20.
Because of the difference in the number of cells (+3) I get

NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 17 beyond bounds [0 .. 16]

exception when I set the 18th cell in the dynamic view.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == _dynamicTableView) {
        NSLog(@"%lu", (unsigned long)[[_filter types] count]);
        return [[_filter types] count];
    } else {
        return 17;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (tableView == _dynamicTableView) {

        static NSString *cellIdentifier = @"type";
        TypeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        if (cell == nil) {
            cell = [[TypeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                            reuseIdentifier:cellIdentifier];
        }

        NSArray *types = [_filter types];
        Type *type = [types objectAtIndex:[indexPath row]];

        [cell.nameLabel setText:[type name]];

        return cell;
    } else {
        return [super tableView:tableView cellForRowAtIndexPath:indexPath];
    }
}

When I go to the Storyboard and increase the number of static cells to something like 30 everything works fine. tableView:numberOfRowsInSection... method removes the unused cells - only 17 static and 20 dynamic cells are shown.
I am aware that the source of my problem is having two UITableView-s in one controller and the large amount of 'if'-s.

Mihail Velikov
  • 566
  • 1
  • 8
  • 14

1 Answers1

0

Try to check with tags, you can give different tags to each table and return number of cells according to that.

 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 if (tableView.tag == 1)  // Tag for _dynamicTableView
      {
          return [[_filter types] count];
      } 

    else {
      return 17;
    }
  }
Nithin M Keloth
  • 1,595
  • 1
  • 20
  • 37