EXC_BAD_ACESS
Publicado por Yn4K (1 intervención) el 03/08/2015 00:41:59
Hola,
Soy nuevo en el mundillo IOS, estoy intentando hacer un menú desplegable, en vez de el típico que trae IOS por defecto de navegación por diferentes viewControllers. Pero me surgen dos problemas, el principal es que el código no me suelta ningún error a la hora de probarlos, simplemente se para en:
Diciendo "thread 1:EXC_BAD_ACCESS(code=2, address=0x7fff53135ff8)", por lo que pude leer es algo relacionado con una mala gestión de punteros ya sea al iniciarlos o durante el proceso de carga, pero por más que miro el código de la clase que controla la tabla no veo nada.
Intentando depurar el código me he dado cuenta que en el método "- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath " solo genera dos de las tres celdas que debería generar.
Adjunto el código por si pudiese servir de ayuda. De ante mano muchas gracias por el tiempo y por la ayuda, simplemente si alguien ha tenido un problema similar si puede explicarme como lo solucionó.
Un saludo.
Soy nuevo en el mundillo IOS, estoy intentando hacer un menú desplegable, en vez de el típico que trae IOS por defecto de navegación por diferentes viewControllers. Pero me surgen dos problemas, el principal es que el código no me suelta ningún error a la hora de probarlos, simplemente se para en:
1
2
3
4
5
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
Diciendo "thread 1:EXC_BAD_ACCESS(code=2, address=0x7fff53135ff8)", por lo que pude leer es algo relacionado con una mala gestión de punteros ya sea al iniciarlos o durante el proceso de carga, pero por más que miro el código de la clase que controla la tabla no veo nada.
Intentando depurar el código me he dado cuenta que en el método "- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath " solo genera dos de las tres celdas que debería generar.
Adjunto el código por si pudiese servir de ayuda. De ante mano muchas gracias por el tiempo y por la ayuda, simplemente si alguien ha tenido un problema similar si puede explicarme como lo solucionó.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
- (void)viewDidLoad {
[super viewDidLoad];
//Creamos datos de prueba
NSString *sectionTitle1 = @"Paises";
NSArray *sectionSubtitles1 = @[@"España",@"Francia",@"Alemania",@"Reino Unido",@"Rusia"];
NSString *sectionTitle2 = @"Ciudades";
NSArray *sectionSubtitles2 = @[@"Cádiz",@"Nueva York",@"Amsterdam",@"Brujas",@"Tokyo"];
NSString *sectionTitle3 = @"Continentes";
NSArray *sectionSubtitles3 = @[@"Europa",@"Asia",@"America",@"Antártida",@"Oceanía"];
NSDictionary *section1 = [NSDictionary dictionaryWithObjectsAndKeys:
sectionTitle1,@"title",
sectionSubtitles1,@"Categorias",
@"1", @"visible", nil];
NSDictionary *section2 = [NSDictionary dictionaryWithObjectsAndKeys:
sectionTitle2,@"title",
sectionSubtitles2,@"Categorias",
@"1", @"visible", nil];
NSDictionary *section3 = [NSDictionary dictionaryWithObjectsAndKeys:
sectionTitle3,@"title",
sectionSubtitles3,@"Categorias",
@"1", @"visible", nil];
//Se carga en el array principal
self.tablaDatos = @[section1,section2,section3];
NSLog(@"%@",self.tablaDatos);
//Recargamos la tabla
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Devolvemos el número de secciones
NSLog(@"Numero de secciones: %lu",(unsigned long)[self.tablaDatos count]);
return [self.tablaDatos count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//Cargamos los datos de la sección
NSDictionary *dataSection = [self.tablaDatos objectAtIndex:section];
NSArray *sectionSubtitles = [dataSection objectForKey:@"Categorias"];
BOOL visible = [[dataSection objectForKey:@"visible"] boolValue];
//Comprobamos si el flag "visible" está activo o no. Si lo está montramos si no no
//Se mostrará una sección vacía (oculta)
if(visible){
return [sectionSubtitles count];
}else{
return 0;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//Accedemos a la celda
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
//Cargamos la sección con datos
NSDictionary *dataSeccion = [self.tablaDatos objectAtIndex:indexPath.section];
NSLog(@"%ld",(long)indexPath);
NSArray *sectionSubtitles = [dataSeccion objectForKey:@"Categorias"];
NSLog(@"%@",sectionSubtitles);
//Modificamos la celda con el texto correspondiente dentro de la sección
[cell.textLabel setText:[sectionSubtitles objectAtIndex:indexPath.row]];
NSLog(@"%@",[sectionSubtitles objectAtIndex:indexPath.row]);
return cell;
}
- (UIView*) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
//Cargamos el título de la sección
NSDictionary *dataSection = [self.tablaDatos objectAtIndex:section];
NSString *sectionTitle = [dataSection objectForKey:@"title"];
//Creamos un botón que contiene el título y que al pulsarlo modifica
UIButton *headerView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 60)];
[headerView setTitle:sectionTitle forState:UIControlStateNormal];
[headerView setBackgroundColor:[UIColor orangeColor]];
[headerView addTarget:self action:@selector(updateSectionVisibility:) forControlEvents:UIControlEventTouchUpInside];
[headerView setTag:section];
//Devolvemos el botón
return headerView;
}
-(void)updateSectionVisibility:(UIButton*)sender
{
//Cargamos una copia mutable de la sección cuya visibilidad se va a modificar
NSMutableDictionary *sectionToModify = [[self.tablaDatos objectAtIndex:sender.tag] mutableCopy];
//Se carga el valor del flag "visible" pero invertido
BOOL sectionVisibility = ![[sectionToModify objectForKey:@"visible"]boolValue];
//Se actualiza el valor del flag en la copia mutable
[sectionToModify setObject:[NSNumber numberWithBool:sectionVisibility] forKey:@"visible"];
//Cargamos una copia mutable de los datos
NSMutableArray *tablaDatosCopia = [self.tablaDatos mutableCopy];
//Reemplazamos la sección correspondiente por su copia modificada
[tablaDatosCopia replaceObjectAtIndex:sender.tag withObject:sectionToModify];
//Volvemos a cargar los datos y recargasmos la tabla
self.tablaDatos = tablaDatosCopia;
[self.tableView reloadData];
}
Un saludo.
Valora esta pregunta


0