我有一个 TableView ,我正在执行删除操作,并且一切正常,直到我更改 TableView Controller 类中的代码以使用提供应用程序数据的类管理器,称为 LocalDataManager。
我可以滑动看到红色的删除按钮,但是当我点击它时没有任何反应。
我在调试器中发现问题出在我的 numberOfRowsInSection 方法中。
这是我的表格 View :
#import "StackTableViewController.h"
#import "Target.h"
#import "StackTableViewCell.h"
#import "HomeViewController.h"
@interface StackTableViewController () <NSFetchedResultsControllerDelegate>
@end
@implementation StackTableViewController
- (id)init {
self = [super initWithNibName"StackTableViewController" bundle:nil];
if (self) {
// Do something
localDataManager = [[LocalDataManager alloc] init];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
[localDataManager reloadData];
//[self.fetchedResultController performFetch:nil];
}
- (void)viewWillLayoutSubviews {
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewWillDisappearBOOL)animated {
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
// just to ignor a warning
- (CGFloat)tableViewUITableView *)tableView heightForRowAtIndexPathNSIndexPath *)indexPath
{
return 44;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableViewUITableView *)tableView {
// Return the number of sections.
return localDataManager.fetchedResultController.sections.count;
}
- (NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section {
// Return the number of rows in the section.
id <NSFetchedResultsSectionInfo> sectionInfo = [localDataManager.fetchedResultController sections][section];
return [sectionInfo numberOfObjects];
}
- (UITableViewCellEditingStyle)tableViewUITableView *)tableView editingStyleForRowAtIndexPathNSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (void)tableViewUITableView *)tableView commitEditingStyleUITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
Target *target = [localDataManager.fetchedResultController objectAtIndexPath:indexPath];
[[localDataManager.stack managedObjectContext] deleteObject:target];
[localDataManager.stack saveContext];
if ([_delegate respondsToSelectorselector(didDeleteObject)]) {
[_delegate didDeleteObject];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"StackTableViewCell";
Target *target = [localDataManager.fetchedResultController objectAtIndexPath:indexPath];
StackTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed"StackTableViewCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
cell.cellLabel.text = target.body;
cell.cellLabel.font = [UIFont fontWithName"Candara-Bold" size:20];
// Configure the cell...
return cell;
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
switch (type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationAutomatic];
default:
break;
}
}
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
switch (type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertRowsAtIndexPaths[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteRowsAtIndexPaths[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case NSFetchedResultsChangeUpdate:
[self.tableView reloadRowsAtIndexPaths[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
default:
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView endUpdates];
}
这是我的 LocalDataManager.m:
#import "LocalDataManager.h"
@implementation LocalDataManager
@synthesize stack,fetchedResultController;
-(id)init{
self = [super init];
if (self) {
//init
stack = [CoreDataStack defaultStack];
fetchedResultController = [self fetchedResultController];
[self reloadData];
}
return self;
}
-(void)reloadData{
[fetchedResultController performFetch:nil];
}
- (NSFetchRequest *)targetsFetchRequest {
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName"Target"];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey"time" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
return fetchRequest;
}
- (NSFetchedResultsController *)fetchedResultController {
if (fetchedResultController != nil) {
return fetchedResultController;
}
NSFetchRequest *fetchRequest = [self targetsFetchRequest];
fetchedResultController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:stack.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
fetchedResultController.delegate = self;
return fetchedResultController;
}
@end
问题也可能与表格 View 的最后 3 个方法有关,或者我从管理器类而不是表格 View 中持有我的 FetchResultController,但我不知道直接指向它。 .请帮我解决这个问题。
谢谢!
从核心数据中删除记录后,您永远不会告诉表删除该行。
你需要:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
Target *target = [localDataManager.fetchedResultController objectAtIndexPath:indexPath];
[[localDataManager.stack managedObjectContext] deleteObject:target];
[localDataManager.stack saveContext];
// Remove row from table
[tableView deleteRowsAtIndexPaths[ indexPath ] withRowAnimation:UITableViewRowAnimationFade];
if ([_delegate respondsToSelector:@selector(didDeleteObject)]) {
[_delegate didDeleteObject];
}
}
关于ios - 无法从表格 View 中删除单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27583972/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |