Question

J'ai examiné presque tous les résultats de recherche, mais mon erreur ne disparaît pas. J'ai une vue de table initialisée avec 2 sections et 1 ligne chacune (à partir d'un tableau). J'ai un bouton pour la vue d'en-tête de section. En cliquant sur un bouton, je souhaite ajouter une ligne à la 1ère section. Voici le code:

- (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];
}

Voici l'erreur:

* Échec de l'affirmation dans - [UableView _endCellanImations withContext:], /sourceCache/Uikit_sim/uikit-1912.3/UiableView.m:1046

Était-ce utile?

La solution

Puisque vous utilisez [arr count] Comme la valeur de retour pour tableView:numberOfRowsInSection:, à la fin de beginUpdates…endUpdates Block the TableView s'attend à ce qu'il y ait deux lignes dans chacune de vos sections TableView, mais votre appel à insertRowsAtIndexPaths: Indique seulement qu'une ligne se situe à la section 0.

Vous devez réparer votre tableView:numberOfRowsInSection: Pour retourner différentes valeurs en fonction de la section:

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

Notez qu'il y a beaucoup de trucs de poisson avec votre table View: vous avez deux sections, mais vous montrez la même ligne exacte 0 dans chaque section. Et le comportement d'insertion dans vos sections est assez bancal: vous commencez à afficher une seule ligne, correspondant à Case 0 dans votre switch, alors vous indiquez que vous insérez une ligne à la ligne 0, après quoi vous afficherez la ligne du boîtier 0 à la ligne 0 et le cas 1 ligne à la ligne 1. Vous devriez donc vraiment insérer une ligne à la ligne 1 à la place de la ligne 0:

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

Autres conseils

Essayez avec le code ci-dessous

NSIndexPath *indexpath_add = [NSIndexPath indexPathForRow:0 inSection:0];
     [[self myTableView] beginUpdates];
     [[self myTableView]  deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexpath_add] withRowAnimation:UITableViewRowAnimationRight];
     [[self myTableView] endUpdates];
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top