NSArrayM objectatindex 索引 1 超出范围 [0..0]

Posted

技术标签:

【中文标题】NSArrayM objectatindex 索引 1 超出范围 [0..0]【英文标题】:NSArrayM objectatindex index 1 beyond bounds [0..0] 【发布时间】:2014-04-12 13:19:47 【问题描述】:

我的项目在表格视图中显示结果时遇到问题。我在 NSmutableArray "ActosPueblo" 中有事件 alloc 并且 uitableview 正确打印了这个结果,但是当点击行时出现错误 "NSArrayM objectatindex index 1 beyond bounds [0..0]" 或者只显示来自不同行的详细信息而不是点击行。

我在 .m 中的代码是...

- (void)viewDidLoad

[super viewDidLoad];
NSString *titulo = [NSString stringWithFormat:@"%@ %@, %@", tipoNombreDia, tipoNumeroDia, tempNombreMes];
self.title = titulo;

//loading antes de pedir los datos
[SVProgressHUD showWithStatus:@"Cargando..." maskType:SVProgressHUDMaskTypeBlack];

[self cargarActosPueblos];





- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

return [actosPueblo count];



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

NSDictionary *dictionary =[actosPueblo objectAtIndex:section];
NSArray *array = [dictionary objectForKey:@"ActosPueblo"];

return [array count];


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

static NSString *CellIdentifier = @"Cell";

CalendarioActosViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];


NSDictionary *dic = [actosPueblo objectAtIndex:indexPath.section];

NSArray *array = [dic objectForKey:@"ActosPueblo"];

NSString *tipoEvento = [[array objectAtIndex:indexPath.row] objectForKey:@"TipoEventos"];
cell.labelEvento.text = tipoEvento;

NSString *tipoMunicipio = [[array objectAtIndex:indexPath.row] objectForKey:@"Municipio"];
cell.labelMunicipio.text = tipoMunicipio;

NSString *tipoGanaderia = [[array objectAtIndex:indexPath.row] objectForKey:@"Ganaderias"];
cell.labelGanaderia.text = tipoGanaderia;

NSString *tipoFecha = [[array objectAtIndex:indexPath.row] objectForKey:@"Fecha"];
cell.labelFecha.text = tipoFecha;

NSString *tipoHora = [[array objectAtIndex:indexPath.row] objectForKey:@"Hora"];
cell.labelHora.text = tipoHora;

NSString *indexP = [NSString stringWithFormat:@"fila: %ld", (long)indexPath.row];
cell.index.text = indexP;

NSString *urlImagen = [[array objectAtIndex:indexPath.row] objectForKey:@"ImagenEvento"];
//precarga imagen con SDWebImage
//precarga imagen con SDWebImage
if([urlImagen length] == 0)
    cell.celdaThumb.image = [UIImage imageNamed:@"sin-icono.png"];
else
    [cell.celdaThumb setImageWithURL:[NSURL URLWithString:urlImagen] placeholderImage:[UIImage imageNamed:@"sin-icono.png"]];



actos = [[NSMutableArray alloc] init];

for (NSDictionary* dic in array) 

    CalendarioActos *actPob = [[CalendarioActos alloc] init];

    actPob.apGanaderia= [dic objectForKey:@"Ganaderias"];
    actPob.apEvento = [dic objectForKey:@"TipoEventos"];
    actPob.apFecha = [dic objectForKey:@"Fecha"];
    actPob.apHora = [dic objectForKey:@"Hora"];
    actPob.apMunicipio = [dic objectForKey:@"Municipio"];
    actPob.apProvincia = [dic objectForKey:@"Provincia"];
    actPob.apToroImagen = [dic objectForKey:@"Imagen"];

    [actos addObject:actPob];



return cell;



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

if([self.checkedIndexPath isEqual:indexPath])
    self.checkedIndexPath = nil;
else
    self.checkedIndexPath = indexPath;


CalendarioActosViewCell *cell =(CalendarioActosViewCell*) [tableView cellForRowAtIndexPath:indexPath];
[self performSegueWithIdentifier:@"actoTodiaActo" sender:cell];


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

if([segue.identifier isEqualToString:@"actoTodiaActo"])
    CalendarioActosDetallesViewController *detallesController= (CalendarioActosDetallesViewController *)segue.destinationViewController;
    NSIndexPath *indexPath = [self.tableViewAP indexPathForSelectedRow];


        cal = [actos objectAtIndex:indexPath.row];

   /* NSString *index = [NSString stringWithFormat:@"%lu", [actos count]];
    //alerta campo vacío
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Count Actos" message:index delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    // Mostrar alerta
    [alert show];*/


    detallesController.tempEvento = cal.apEvento;
    detallesController.tempFecha = cal.apFecha;
    detallesController.tempGanaderia = cal.apGanaderia;
    detallesController.tempHora = cal.apHora;
    detallesController.tempImagen = cal.apToroImagen;
    detallesController.tempMunicipio = cal.apMunicipio;
    detallesController.tempProvincia = cal.apProvincia;


    NSString *datos = [NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ %@", cal.apEvento, cal.apFecha, cal.apGanaderia, cal.apHora, cal.apMunicipio, cal.apProvincia, cal.apToroImagen];
    NSLog(@"pasamos: %@", datos);





“ActosPueblo”包含来自 url 的数据以及来自 viewload 中函数“cargarActosPueblos”的 JSON。

我从 ***.com 看到其他问题,但没有结果。

请检查我的代码并接受任何建议。

感谢和问候

【问题讨论】:

【参考方案1】:

我不确定您要在 didSelectRowAtIndexPath 中实现什么,您真正需要做的就是执行 segue,因为您将在 prepareForSegue 中获得所选项目:

- (void)tableView:(UITableView *)tableView 
                               didSelectRowAtIndexPath:(NSIndexPath *)indexPath
   [self performSegueWithIdentifier:@"actoTodiaActo" sender:self];

"NSArrayM objectatindex index 1 越界[0..0]"

表示您正在访问大于数组大小的元素。


cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView 
                        cellForRowAtIndexPath:(NSIndexPath *)indexPath
//Get the tipo at this index
NSDictionary *dic = [actosPueblo objectAtIndex:indexPath.section];
NSArray *array = [dic objectForKey:@"ActosPueblo"];
NSDictionary *tipo  = [array objectAtIndex:indexPath.row];

//Configure the cell
static NSString *CellIdentifier = @"Cell";
CalendarioActosViewCell*cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier 
                                                                forIndexPath:indexPath];
cell.labelEvento.text = tipo[@"TipoEventos"];
cell.labelMunicipio.text = tipo[@"Municipio"];
cell.labelGanaderia.text = tipo[@"Ganaderias"];
cell.labelFecha.text = tipo[@"Fecha"];
cell.labelHora.text = tipo[@"Hora"];
cell.index.text = [NSString stringWithFormat:@"fila: %ld", (long)indexPath.row];
NSString *urlImagen = tipo[@"ImagenEvento"];

if([urlImagen length] == 0)
    cell.celdaThumb.image = [UIImage imageNamed:@"sin-icono.png"];
else
    [cell.celdaThumb setImageWithURL:[NSURL URLWithString:urlImagen] 
                         placeholderImage:[UIImage imageNamed:@"sin-icono.png"]];



return cell;

didSelectRowAtIndexPath

- (void)tableView:(UITableView *)tableView 
                didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    [self performSegueWithIdentifier:@"actoTodiaActo" sender:self];

prepareForSegue

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

if([segue.identifier isEqualToString:@"actoTodiaActo"])

    //Get the Selected tipo at this index
    NSIndexPath *indexPath = [self.tableViewAP indexPathForSelectedRow];
    NSDictionary *dic = [actosPueblo objectAtIndex:indexPath.section];
    NSArray *array = [dic objectForKey:@"ActosPueblo"];
    NSDictionary *tipo  = [array objectAtIndex:indexPath.row];

    CalendarioActosDetallesViewController *detallesController = 
       (CalendarioActosDetallesViewController *)segue.destinationViewController;      

    detallesController.tempEvento = tipo[@"TipoEventos"];
    detallesController.tempFecha = tipo[@"Fecha"];
    detallesController.tempGanaderia = tipo[@"Ganaderias"];
    detallesController.tempHora = tipo[@"Hora"];
    detallesController.tempImagen = tipo[@"ImagenEvento"];
    detallesController.tempMunicipio = tipo[@"Municipio"];
    detallesController.tempProvincia = tipo[@"Provincia"];

    //log
    NSString *datos = [NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ %@",
                    cal.apEvento, cal.apFecha, cal.apGanaderia, cal.apHora, 
                    cal.apMunicipio, cal.apProvincia, cal.apToroImagen];
    NSLog(@"pasamos: %@", datos);



【讨论】:

在 didSelecrRowAtIndexpath 中,每次按下行时,我都会将数组 actos 的 indexpath.row 保存在 NSObject cal 中,并执行一个 segue 操作。并在 PrepareForSegue 中将 NSObject cal 中的数据传递给 DetailsViewController。你听得懂么?感谢您的帮助! 嘿meda,你的代码工作正常!!哇!!但是现在新的问题...当按下返回按钮时不显示带有数据的表格视图,显示黑屏和应用程序崩溃并显示消息...“嵌套推送动画可能导致导航栏损坏”“在意外状态下完成导航转换。导航栏子视图树可能会损坏”“由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'无法将自身添加为子视图'”我们离解决方案越来越近了!感谢您的代码。请解决后退按钮时黑屏的问题?问候;) 我注意到你在进行 segue 时使用作为发件人,这就是为什么我改为自我。您应该将 segue 从 viewcontroller 替换为 viewcontroller 噢,太棒了!工作正常。谢谢大家。我的应用程序现在可以正常工作了;)【参考方案2】:

每次调用cellForRowAtIndexPath 时,您都会创建一个新数组作为actos。因此,它只会有一个对象(在位置 0)。当您尝试访问除 0 以外的任何行时,这就是您的崩溃。

我建议您在使用索引路径引用某些内容时使用相同的数据结构,而不是尝试创建不是ActosPueblo 的第二个数组。

【讨论】:

拜托,你能试着用我的代码做你对我说的吗?我真的不知道该怎么做。提前致谢!

以上是关于NSArrayM objectatindex 索引 1 超出范围 [0..0]的主要内容,如果未能解决你的问题,请参考以下文章

[__NSArrayM objectAtIndex:]: 索引 2 超出范围 [0 .. 1]'

NSRangeException',原因:'*** -[__NSArrayM objectAtIndex:]:空数组的索引 5 超出范围'

异常'NSRangeException',原因:'*** -[__NSArrayM objectAtIndex:]:索引 19 超出范围 [0 .. 18]'

-[__NSArrayM objectAtIndex:]: 索引 11 超出界限 [0 .. 10]' 目标 c

由于未捕获的异常“NSRangeException”而终止应用程序。 [__NSArrayM objectAtIndex:]:索引 33 超出范围 [0 .. 32]'

-[__NSArrayM objectAtIndex:]:索引 0 超出了带有 userInfo 的空数组的界限(null)