Tengo problemas para animar un encabezado de sección de UITableView personalizado.
El objetivo era crear secciones colapsables.
Cuando toco el encabezado personalizado la primera vez que se anima como se esperaba, sin embargo, cada vez que lo hago deja un duplicado en la ubicación original y anima otro.Sección UITableView Encabezado de sección personalizada, problema duplicado
imagen de ejemplo:
alt text http://img35.imageshack.us/img35/4018/screenshot2010022722565.png alt text http://img291.imageshack.us/img291/8287/screenshot2010022723035.png
Mi encabezado personalizado:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView* customView = [[[UIView alloc]initWithFrame:CGRectMake(10.0, 0.0, 300.0, 44.0)]autorelease];
customView.backgroundColor = [UIColor lightGrayColor];
UILabel * headerLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.textColor = [UIColor darkGrayColor];
headerLabel.font = [UIFont boldSystemFontOfSize:16];
headerLabel.frame = CGRectMake(10, 7, 260.0, 44.0);
headerLabel.textAlignment = UITextAlignmentCenter;
NSDictionary *dictionary = [self.data objectAtIndex:section];
headerLabel.text = [dictionary objectForKey:@"Title"];
[customView addSubview:headerLabel];
// add button to right corner of section
UIButton* headerButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 320, 44)];
headerButton.center = CGPointMake(160.0, 22.0);
headerButton.backgroundColor = [UIColor clearColor];
headerButton.tag = section;
[headerButton addTarget:self action:@selector(expandSection:) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:headerButton];
return customView;
}
Mi Animación Método:
- (void) expandSection:(id)sender {
if (expandedSection == [sender tag]) {
expandedSection = -1;
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationNone];
}else if (expandedSection == -1){
expandedSection = [sender tag];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationNone];
}else{
[self.tableView beginUpdates];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:expandedSection] withRowAnimation:UITableViewRowAnimationNone];
expandedSection = [sender tag];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
}
//[self.tableView reloadData];
}
No estoy exactamente seguro de lo que pasa, pero el las instancias sugieren que necesito tratar algo. He intentado algunas cosas pero no puedo resolver esto. ¡Cualquier persona que ayude con esto sería genial!
Editar: Creo que el problema es que reloadSections está causando la vista personalizada a la instancia. No puedo lanzar la vista porque la necesito como referencia para hacer la actualización de la animación. ¿Alguna idea sobre lo que puedo hacer para arreglar esto?
Gracias, esto soluciona un problema relacionado en el que quería producir un efecto animado de eliminar/insertar las mismas filas, donde, sin llamar primero a 'reloadData', solo animaría las eliminaciones, pero no animaría las inserciones. Es un problema muy extraño. – Jeremy
Buena manera ... + 1 para usted hombre – Sabby