我的项目中有一个问题,在tableview中显示结果。我在NSmutableArray" ActosPueblo"并且uitableview正确地打印了这个结果,但是当行中点击时出现错误" NSArrayM objectatin索引1超出界限[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的数据,来自函数" cargarActosPueblos"在viewload。
我在stackoverflow.com上看到了同样问题的其他问题,但对我没有任何结果。
请检查我的代码并接受任何建议。
谢谢和问候
答案 0 :(得分:0)
每次致电cellForRowAtIndexPath
时,您都会创建一个新数组actos
。因此,它只会在其中有一个对象(位置0)。当你试图为0以外的任何一行访问它时,那就是你的崩溃。
我建议您在使用索引路径引用某些内容时使用相同的数据结构,而不是尝试创建不是ActosPueblo
的第二个数组。
答案 1 :(得分:0)
我不确定你在didSelectRowAtIndexPath
想要实现的目标是你真正需要做的就是执行segue,因为你将获得prepareForSegue
中的选定项目:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self performSegueWithIdentifier:@"actoTodiaActo" sender:self];
}
“NSArrayM objectatin索引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);
}
}