【发布时间】:2017-02-03 15:19:12
【问题描述】:
这是下一个问题的延续
https://stackoverflow.com/questions/39673003/crash-on-multiple-calls-to-loadnibnamed
基本上我在打电话
[[NSBundle mainBundle] loadNibNamed:@"YFCalendarDayCell" owner:self options:nil] objectAtIndex:0];
或
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:nibName
owner:nil
options:nil];
在 for 循环中。这最终会崩溃,BadExcess error 或 NSArray 返回的元素为零。
有正确的方法吗?
添加@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]];
}
}
}
【问题讨论】:
-
您在第一行或第二行代码中遇到错误吗?
-
尝试将循环体包裹在
@autoreleasepool { }中。也许您的内存不足。 -
@user3182143,第一个为 BadExcess,第二个为 0 个对象。这是我尝试加载 xib 的两种不同方式。
-
您是否尝试过在 for 循环中使用自动释放池?
-
正在尝试。给我一分钟
标签: ios objective-c loadnibnamed