[英]ios/coredata/nfetchresultscontroller. How do I get a tableview of records to update after adding a new one
我正在嘗試使用委托模式來獲取記錄的表視圖,以在添加新記錄后進行更新。
我在tableview控制器中有nsfetched結果控制器的代碼。 該表已作為nsfetchedresults控制器的委托進行了預訂。 我在添加新記錄控制器中有保存代碼。 我究竟做錯了什么? 謝謝。
table view controller:
header:
@interface IDTVC : UITableViewController<UITableViewDataSource, UITableViewDelegate,NSFetchedResultsControllerDelegate>
source:
#pragma mark - Fetched Results Controller
-(NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController==nil){
NSFetchRequest *fetchRequest= [[NSFetchRequest alloc] init];
//access the single managed object context through model singleton
NSManagedObjectContext *context = [IDModel sharedInstance].managedObjectContext;
//fetch request requires an entity description - we're only interested in IDModel managed objects
NSEntityDescription *entity =[NSEntityDescription entityForName:@"Dare" inManagedObjectContext:context];
fetchRequest.entity=entity;
//we'll order the IDModel ID Model objects in name sort order
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor,nil];
fetchRequest.sortDescriptors=sortDescriptors;
self.fetchedResultsController=[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:nil cacheName:nil];
// self.fetchedResultsController.delegate=self;
[self.fetchedResultsController setDelegate:self];
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
NSLog(@"Unresolved error %@,%@",error,[error userInfo]);
abort();
}
}
return _fetchedResultsController;
}
//following supposed to be sufficient for delegate pattern to work
-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
//change
}
AddRecord Controller
- (IBAction)save:(id)sender {
self.managedObjectContext = [IDModel sharedInstance].managedObjectContext;;
// Helpers
NSString *name = self.textField.text;
NSString *sub = self.subField.text;
// [[[UIAlertView alloc] initWithTitle:name message:@"Your record needs a name." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
NSLog(@"save triggered");
if (name && name.length) {
// Create Entity
NSEntityDescription *entity = [NSEntityDescription entityForName:@"newitem" inManagedObjectContext:self.managedObjectContext];
// Initialize Record
NSManagedObject *record = [[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:self.managedObjectContext];
// Populate Record
[record setValue:name forKey:@"name"];
[record setValue:sub forKey:@"sub"];
// [record setValue:[NSDate date] forKey:@"createdAt"];
// Save Record
NSError *error = nil;
if ([self.managedObjectContext save:&error]) {
// Dismiss View Controller
[self dismissViewControllerAnimated:YES completion:nil];
;
} else {
if (error) {
NSLog(@"Unable to save record.");
NSLog(@"%@, %@", error, error.localizedDescription);
}
// Show Alert View
[[[UIAlertView alloc] initWithTitle:@"Warning" message:@"Your record could not be saved." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
}
} else {
// Show Alert View
[[[UIAlertView alloc] initWithTitle:@"Warning" message:@"Your item needs a name." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
}
}
//dismiss keyboard
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"touchesBegan:withEvent:");
[self.view endEditing:YES];
[super touchesBegan:touches withEvent:event];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"Add Item";
[IDModel sharedInstance];
self.managedObjectContext = self.managedObjectContext;
}
我需要[self.tableView reloadData]; 委托模式是否應該處理更新?
將保存保存在表視圖的添加視圖控制器和nsfetchresultscontroller代碼中是否存在問題? 將不勝感激。
除了controllerDidChangeContent委托方法之外,您還將需要controllerWillChangeContent,它將啟動表視圖更新。 然后,在controllerDidChangeContent中,您將不得不在表視圖上結束更新。
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[tableView beginUpdates];
}
-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[tableView endUpdates];
}
您還需要另一個委托方法來實際處理每個更新操作。 下面:
-(void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
{
switch (type)
{
case NSFetchedResultsChangeUpdate:
[tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.row] withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case NSFetchedResultsChangeInsert:
[tableView insertSections:[NSIndexSet indexSetWithIndex:newIndexPath.row] withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.row] withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case NSFetchedResultsChangeMove:
[tableView moveSection:indexPath.row toSection:newIndexPath.row];
break;
default:
NSAssert(FALSE, @"what is this?");
break;
}
}
好的,該編輯沒有完成……使用Lee的答案,但是添加第四個FetchedResultsControllerDelegate
方法:
- (void)controller:(NSFetchedResultsController *)controller
didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex
forChangeType:(NSFetchedResultsChangeType)type
{
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:sectionIndex];
switch(type)
{
case NSFetchedResultsChangeInsert:
{
[self.tableView insertSections:indexSet
withRowAnimation:UITableViewRowAnimationFade];
break;
}
case NSFetchedResultsChangeDelete:
{
[self.tableView deleteSections:indexSet
withRowAnimation:UITableViewRowAnimationFade];
break;
}
default:
{
[self.tableView reloadSections:indexSet
withRowAnimation:UITableViewRowAnimationAutomatic];
break;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.