LeXeR LeXeR - 2 months ago 14
iOS Question

Crash when loadingNibNamed in for loop

This is in continuation to the following question

Crash on Multiple Calls to LoadNibNamed

Basically i am calling

[[NSBundle mainBundle] loadNibNamed:@"YFCalendarDayCell" owner:self options:nil] objectAtIndex:0];


OR

NSArray *objects = [[NSBundle mainBundle] loadNibNamed:nibName
owner:nil
options:nil];


in a for loop. This eventually crashes with
BadExcess error
OR
NSArray
that is returned has zero elements.

Is there a correct way to do this?

After Adding
@Autoreleasepool


int i, j, k=0;
for( i=0; i<5; i++ )
{
for( j=0; j<7; j++ )
{
@autoreleasepool {
YFCalendarDayCell *tableView = [Utility loadNibNamed:@"YFCalendarDayCell" ofClass:[YFCalendarDayCell class]];

tableView.cellTableView.dataSource = tableView;
tableView.cellTableView.delegate = tableView;
tableView.controller = mainViewController;

//[calendarView addSubview:tableView];

NSLog(@"tables added to calendar view i: %d j: %d",i,j);

[calendarView.CellDict setObject:tableView forKey:[[NSNumber numberWithInt:k++] stringValue]];
}
}
}

Answer

Try below answer

for (i=0; i<5; i++) {
      @autoreleasepool
      {
          for (j=0; j<7; j++)
          {

            YFCalendarDayCell *tableView = [Utility loadNibNamed:@"YFCalendarDayCell" ofClass:[YFCalendarDayCell class]];

            tableView.cellTableView.dataSource = tableView;
            tableView.cellTableView.delegate = tableView;
            tableView.controller = mainViewController;
            NSLog(@"tables added to calendar view i: %d j: %d",i,j);
            [calendarView.CellDict setObject:tableView forKey:[[NSNumber numberWithInt:k++] stringValue]];
          }
      }
}
Comments