Pregunta

He mirado a través de casi todos los resultados de la búsqueda, pero mi error no se va.Tengo una vista de tabla inicializada con 2 secciones y 1 fila cada una (desde una matriz).Tengo un botón para la vista de encabezado de la sección.Al hacer clic en un botón, quiero agregar una fila a la 1ª sección.Aquí está el código:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [arr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   
    UITableViewCell *cell = (UITableViewCell*)[self.myTableView dequeueReusableCellWithIdentifier:@"DetailCell"];
    cell.textLabel.text=[arr objectAtIndex:indexPath.row];
    cell.backgroundColor=[UIColor clearColor];
    return cell; 
} 

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            btnReleases=[UIButton buttonWithType:UIButtonTypeCustom];
            [btnReleases setFrame:CGRectMake(10, 0, 30, 39)];
            [btnReleases setImage:[UIImage imageNamed:@"buttonr.png"] forState:UIControlStateNormal];
            [btnReleases addTarget:self action:@selector(loadReleases) forControlEvents:UIControlEventTouchUpInside];
            return btnReleases;
            break;
        case 1:
            btnLinks=[UIButton buttonWithType:UIButtonTypeCustom];
            [btnLinks setFrame:CGRectMake(10, 0, 30, 39)];
            [btnLinks setImage:[UIImage imageNamed:@"buttonl.png"] forState:UIControlStateNormal];
            [btnLinks addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
            return btnLinks;
            break;
        default:
            break;
    }

}
-(void)loadReleases
{

    [self.myTableView beginUpdates];
        [arr addObject:@"WTF"];
    NSArray *insert0 = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]]; NSLog(@"%@",insert0);
    [self.myTableView insertRowsAtIndexPaths:insert0 withRowAnimation:UITableViewRowAnimationBottom];
    [self.myTableView endUpdates];
}

heres el error:

* Fallo de afirmación en - [uitableview _endcellanimationswithcontext:], /sourcecache/uikit_sim/uikit-1912.3/uitableview.m:1046

¿Fue útil?

Solución

Dado que está utilizando [arr count] como el valor de retorno para tableView:numberOfRowsInSection:, al final del bloque de beginUpdates…endUpdates, la TablaView está esperando que haya dos filas en cada una de sus secciones de TableView, pero su llamada a insertRowsAtIndexPaths: solo indica que una filaestá estar a la sección 0.

Necesita solucionar su tableView:numberOfRowsInSection: para devolver los valores diferentes según la sección:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return [arr count];
    } else {
        return 1;
    }
}

Tenga en cuenta que hay muchas cosas de pescado que están pasando con su TablaView: tiene dos secciones, pero está mostrando la misma fila exacta 0 en cada sección.Y el comportamiento de inserción dentro de sus secciones es bastante bueno: comienza a mostrar solo una fila, correspondiente a la caja 0 en su switch, luego indica que está insertando una fila en la fila 0, después de lo cual le mostrará el caso0 fila en la fila 0 y la caja 1 fila en la fila 1. Para que realmente debería estar insertando una fila en la fila 1 en lugar de la fila 0:

NSArray *insert0 = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:1 inSection:0]]; 
[self.myTableView insertRowsAtIndexPaths:insert0 withRowAnimation:UITableViewRowAnimationBottom];

Otros consejos

Intente con el código inferior

NSIndexPath *indexpath_add = [NSIndexPath indexPathForRow:0 inSection:0];
     [[self myTableView] beginUpdates];
     [[self myTableView]  deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexpath_add] withRowAnimation:UITableViewRowAnimationRight];
     [[self myTableView] endUpdates];

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top