' NSInternalInconsistencyException',原因:'尝试将第3行插入第0部分,但在更新后第0部分只有2行'

时间:2015-01-16 19:52:55

标签: objective-c core-data

我在项目中使用Core Data,在尝试添加新行时,出现错误:

断言失败 - [UITableView _endCellAnimationsWithContext:],/ SourceCache / UIKit_Sim / UIKit-3318.16.14 / UITableView.m:1377 2015-01-16 22:45:47.172 LinguaCard [26439:599761]由于未捕获异常'NSInternalInconsistencyException'而终止应用程序,原因:'尝试将第3行插入第0部分,但更新后第0部分只有2行“

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 = [[NSMutableArray alloc] init];
    return _cards;

}

- (void)viewDidLoad {
    [super viewDidLoad];

    NSSet *unordereCards = self.lesson.cards;
    NSSortDescriptor *nameDescr = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
    NSArray *sortedCards = [unordereCards sortedArrayUsingDescriptors:@[nameDescr]];
    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:@[[NSIndexPath indexPathForRow:[self.cards count]-1 inSection:0]] 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 the number of sections.
    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;
}

1 个答案:

答案 0 :(得分:0)

当您的UIAlertViewDelegate方法添加卡片时,会将其添加到控制器属性中。当您报告部分中的行数时,请使用lesson对象,该对象不了解控制器的cards