我一直在搜索这个错误,并找到一些类似的行为,但没有解决方案,解决了这个问题。混合静态和动态UITableViewController内容导致NSRangeException
我有一个UITableViewController(声明为静态在SB),其具有以部分:第一部分0(配方)是静态的4个细胞,第1(香料)应该是动态
这是
- (void)viewDidLoad {
[super viewDidLoad];
rows = [[NSMutableArray alloc] initWithArray:@[@"test",@"test",@"test",@"test",@"test",@"test",@"test"]];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
switch (section) {
case 0:
return 4;
break;
case 1:
return rows.count;
break;
default:
break;
}
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
switch (indexPath.section) {
case 0:
return [super tableView:tableView cellForRowAtIndexPath:indexPath];
break;
case 1:
cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell)
{
// create a cell
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
cell.textLabel.text = [rows objectAtIndex:indexPath.row];
return cell;
default:
break;
}
return cell;
}
现在运行的时候,我得到的错误是: 'NSRangeException',原因是:我使用的测试代码“*** - [__ NSArrayI objectAtIndex:]:指数1超出范围[0 .. 0]'
我知道我错过了某个地方的小东西,有人可以帮助我吗?
非常感谢
看看这个http://stackoverflow.com/a/9428324/285190 – Flexicoder 2014-12-03 14:05:25
我怎么没看到你的链接应该在帮助这种情况下,因为这里描述的问题不等于我的,除了我使用UITableViewController而不是UIViewController – CRE8IT 2014-12-03 14:16:30
它崩溃之后numberOfRowsInSection甚至没有进入CellForRowAtIndexPath – CRE8IT 2014-12-03 14:41:22