Reputation: 377
i have a problem with UITable. When I used main UITableViewController it worked fine when I deleted row, it hide. But now I have problem because I have UITableView
(_domainTableView) on UIViewController(MainViewController). It works fine but when I wanna delete row it doesnt hide it stay and when I shut down and shut up app this row is deleted, but i wanna automatically and with animation than before. Have you any solution?
#import "MainViewController.h"
@interface MainViewController ()
@property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController;
@end
@implementation MainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
CAGradientLayer *bgLayer = [backgroundGradient blueGradient];
bgLayer.frame = self.view.bounds;
[self.view.layer insertSublayer:bgLayer atIndex:0];
UIImage *image = [UIImage imageNamed: @"logo"];
UIImageView *imageview = [[UIImageView alloc] initWithImage: image];
self.navigationItem.titleView = imageview;
[self.fetchedResultsController performFetch:nil];
_domainTableView.dataSource = self;
_domainTableView.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.fetchedResultsController.sections.count;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
Domain *domain = [self.fetchedResultsController objectAtIndexPath:indexPath];
CoreDataStack *coreData = [CoreDataStack defaultStack];
[[coreData managedObjectContext] deleteObject:domain];
[coreData saveContext];
// [self.domainTableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section];
return [sectionInfo numberOfObjects];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
Domain *domain = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = domain.name;
return cell;
}
- (NSFetchRequest *)entryListFetchRequest {
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Domain"];
fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:NO]];
return fetchRequest;
}
- (NSFetchedResultsController *)fetchedResultsController
{
if(_fetchedResultsController != nil)
{
return _fetchedResultsController;
}
CoreDataStack *coreDataStack = [CoreDataStack defaultStack];
NSFetchRequest *fetchRequest = [self entryListFetchRequest];
_fetchedResultsController = [[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest managedObjectContext:coreDataStack.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
// _fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.domainTableView beginUpdates];
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.domainTableView endUpdates];
}
@end
Upvotes: 0
Views: 238
Reputation: 1415
You'll want to use the - (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
method in order to actually animate the deletion of the row. Your commitEditingStyle
method should look something like this:
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
Domain *domain = [self.fetchedResultsController objectAtIndexPath:indexPath];
CoreDataStack *coreData = [CoreDataStack defaultStack];
[[coreData managedObjectContext] deleteObject:domain];
[coreData saveContext];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]
}
}
Some documentation which might be helpful:
Inserting and Deleting Rows and Sections
Upvotes: 0
Reputation: 1996
Looks like fetchedResultsController
doesn't have any delegate attached. Did you try to uncomment the following line? Was it commented out on purpose?
// _fetchedResultsController.delegate = self;
Upvotes: 1