我一直在寻找这个错误,并确实找到了一些具有类似行为但没有解决问题的解决方案的帖子。
我有一个 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:]: index 1 beyond bounds [0 .. 0]'
我知道我在某个地方错过了一些小东西,有人可以帮助我吗?
非常感谢