Cell未从CoreData插入

时间:2014-03-30 03:23:51

标签: ios objective-c uitableview core-data

我正在研究这个从一个CoreData对象获取信息的tableview,当我尝试将一个对象添加到CoreData并且tableview不起作用时,我希望这个是动画的,这就是我正在尝试的:

在CoreDataBase(核心数据方法)中:

+(void)insertNewTask:(NSString *) task
            withnote:(NSString *) note
         withnotDate:(NSDate *) notdate{

    [MagicalRecord saveUsingCurrentThreadContextWithBlock:^(NSManagedObjectContext *localContext) {

        Task *thetask = [Task MR_createEntity];
        thetask.task = task;
        thetask.note = note;
        thetask.date = [NSDate date];
        thetask.notdate = notdate;

} completion:^(BOOL success, NSError *error) {
        NSLog(@"Accounts:%d", [CoreDataBase numberofTasks]);
    }];

}

在tableview控制器中,更新发生在从文本字段返回时,该文本是要添加到tableview和coredata中的文本(仅添加到coredata中,因为当我重新打开时我可以看到所有对象在表中):

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    NSDate *date = [NSDate dateWithTimeIntervalSince1970:1];

    [CoreDataBase insertNewTask:textField.text withnote:textField.text withnotDate:date];

    [self reloadCards];

    textField.text = @"";
    NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:(0) inSection:0];

    [self.tableview beginUpdates];


    [self.tableview insertRowsAtIndexPaths:[NSArray arrayWithObject:[self keyForIndexPath:newIndexPath]] withRowAnimation:UITableViewRowAnimationLeft];

    [self.tableview endUpdates];



    [textField addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:NULL];
    [textField resignFirstResponder];

    [CoreDataBase logAllTasks];

    return YES;
}

-(void)reloadCards{

    self.allTasks = [[CoreDataBase allTasksWithDateSort:NO] mutableCopy];
    [self.tableview reloadData];
}

这就是我将信息显示在表格中的方式(正如我所说的只在Reopen中加载,而不是在文本字段的返回中):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    MCSwipeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.delegate = self;
        [tableView setRowHeight:73];
        Task *task = self.allTasks[indexPath.row];
        cell.taskTitle.text = [task task];
}

所以我希望我能很好地解释我的问题,我正在使用MagicalRecord所以计算行的方法和所有不复杂的方法,这就是为什么我不发布它们,谢谢并希望你帮助我

1 个答案:

答案 0 :(得分:0)

您应该在viewcontroller上创建NSFetchedResultsController实例。然后使viewcontroller成为NSFetchedResultsController对象的委托。为此,您应该在viewcontroller上支持NSFetchedResultsControllerDelegate协议。在这里阅读更多https://developer.apple.com/library/ios/documentation/CoreData/Reference/NSFetchedResultsController_Class/Reference/Reference.html