V
V
V2015-01-16 23:11:44
Objective-C
V, 2015-01-16 23:11:44

Has anyone encountered a problem?

I use in the Core Data project. About trying to insert a new element into the table, I get an error:
*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-3318.16.14/UITableView.m:1377
2015-01-16 22:45:47.172 LinguaCard[26439:599761] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 3 into section 0, but there are only 2 rows in section 0 after the update'

There are answers on StackOverflow, but didn't find anything useful.
CardsTableViewController.m

#import "CardsTableViewController.h"
#import "Lesson.h"
#import "Card.h"
#import "CoreDataHelper.h"

@interface CardsTableViewController ()

@property (strong, nonatomic) NSMutableArray *cards;

@end

@implementation CardsTableViewController

-(NSMutableArray *)cards{
    if (!_cards)
        _cards = ;
    self.cards = [sortedCards mutableCopy];
    
}

#pragma mark - helpers
-(Card *)cardWithName:(NSString *)name{
    
    NSManagedObjectContext *context = [CoreDataHelper managedObjectContext];
    
    Card *card = [NSEntityDescription insertNewObjectForEntityForName:@"Card" inManagedObjectContext:[CoreDataHelper managedObjectContext]];
    card.name = name;
    card.lesson = self.lesson;
    
    
    NSError *error = nil;
    if (![context save:&error]){
        //we have an error
        NSLog(@"error: %@", error);
    }
    return card;
}


#pragma mark - UIAlertViewDelegate

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    if (buttonIndex == 1) {
        NSString *alertText = [alertView textFieldAtIndex:0].text;
        [self.cards addObject:[self cardWithName:alertText]];
        [self.tableView insertRowsAtIndexPaths:@ withRowAnimation:UITableViewRowAnimationFade];
        
    }

}



-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Card"];
   
    NSError *error = nil;
    NSArray *fetchedCards = [[CoreDataHelper managedObjectContext] executeFetchRequest:fetchRequest error:&error];
    
    self.cards = [fetchedCards mutableCopy];
    
    [self.tableView reloadData];
}


- (IBAction)addCard:(id)sender {
    
    UIAlertView *newCard = [[UIAlertView alloc] initWithTitle:@"Enter new card name:" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add", nil];
    
    [newCard setAlertViewStyle:UIAlertViewStylePlainTextInput];
    [newCard show];
}


#pragma mark - Table view data source

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.lesson.cards.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Card Cell" forIndexPath:indexPath];
    
    Card *selectedCard = self.cards[indexPath.row];
    cell.textLabel.text = selectedCard.name;
    return cell;
}

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
A
An, 2015-01-17
@Flanker_4

You have something utterly written there ...
But the problem is specifically in

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.lesson.cards.count;
}

Here you need to write self.cards.count
After calling
[self.tableView insertRowsAtIndexPaths:@ withRowAnimation:UITableViewRowAnimationFade];
The number of cells must match what is returned in the datasouce method. Otherwise, a fall will occur. And in your case, the insertion goes to cards (there will be 3 of them), and the number of self.lesson.cards is returned to the data source (which will remain 2)
But at the same time, the cell itself has already been shoved into the table itself through insertRow - 2!= 3 - that's falling

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question