我有两个部分,A-M和N-Z。
我注意到,如果我在每个部分中拥有相同数量的城市,那么就不会有问题。但是,如果我没有相同数量的城市,该程序将崩溃。
错误是
-[__ NSArrayM objectAtIndex:]:索引2超出范围[0 .. 0]'
错误生成时,这是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Region";
self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
AM = [[NSMutableArray alloc] init];
NZ = [[NSMutableArray alloc] init];
[AM addObject: @"Bologna"];
[AM addObject: @"Florence"];
[AM addObject: @"Milan"];
[NZ addObject: @"Naples"];
}
- (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 [AM count];
break;
case 1:
return [NZ count];
break;
default:
return section;
break;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSInteger section = [indexPath section];
switch (section) {
case 0:
[cell.textLabel setText: [AM objectAtIndex: [indexPath row]]];
break;
case 1:
[cell.textLabel setText: [NZ objectAtIndex: [indexPath row]]];
break;
default:
break;
}
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
switch (section) {
case 0:
return @"A-M";
break;
case 1:
return @"N-Z";
break;
default:
break;
}
return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * AMPath = self.luzonRegion [indexPath.row];
NSString * NZPath = self.visayasRegion [indexPath.row];
switch (indexPath.section) {
case 0:
cityController.title = luzonRegionPath;
NSLog(@"Selected city: %@", AMPath);
break;
case 1:
cityController.title = visayasRegionPath;
NSLog(@"Selected city: %@", NZPath);
break;
default:
break;
}
[[self navigationController] pushViewController:cityController animated:YES];
}
最佳答案
你这样尝试
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Region";
tableView = [[[UITableView alloc] initWithFrame:CGRectMake(0,0,320,480) style:UITableViewStyleGrouped] autorelease];
tableView.delegate=self;
tableView.dataSource=self;
[self.view addSubview:tableView];
AM = [[NSMutableArray alloc] init];
NZ = [[NSMutableArray alloc] init];
[AM addObject: @"Bologna"];
[AM addObject: @"Florence"];
[AM addObject: @"Milan"];
[NZ addObject: @"Naples"];
}
- (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 [AM count];
break;
case 1:
return [NZ count];
break;
default:
return section;
break;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSInteger section = [indexPath section];
switch (section) {
case 0:
[cell.textLabel setText: [AM objectAtIndex: [indexPath row]]];
break;
case 1:
[cell.textLabel setText: [NZ objectAtIndex: [indexPath row]]];
break;
default:
break;
}
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
switch (section) {
case 0:
return @"A-M";
break;
case 1:
return @"N-Z";
break;
default:
break;
}
return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// NSString * AMPath = self.luzonRegion [indexPath.row];
// NSString * NZPath = self.visayasRegion [indexPath.row];
switch (indexPath.section) {
case 0:
//cityController.title = luzonRegionPath;
NSLog(@"Selected city: %@", [AM objectAtIndex: [indexPath row]]);
break;
case 1:
//cityController.title = visayasRegionPath;
NSLog(@"Selected city: %@",[NZ objectAtIndex: [indexPath row]]);
break;
default:
break;
}
// [[self navigationController] pushViewController:cityController animated:YES];
}
它工作正常.....
关于ios - [__NSArrayM objectAtIndex:]:索引2超出范围[0 .. 0],并且是填充UITableView的正确方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12817961/