ios - 无法从表格 View 中删除单元格
<p><p>我有一个 TableView ,我正在执行删除操作,并且一切正常,直到我更改 TableViewController 类中的代码以使用提供应用程序数据的类管理器,称为 LocalDataManager。</p>
<p>我可以滑动看到红色的删除按钮,但是当我点击它时没有任何反应。</p>
<p>我在调试器中发现问题出在我的 numberOfRowsInSection 方法中。</p>
<p>这是我的表格 View :</p>
<pre><code>#import "StackTableViewController.h"
#import "Target.h"
#import "StackTableViewCell.h"
#import "HomeViewController.h"
@interface StackTableViewController () <NSFetchedResultsControllerDelegate>
@end
@implementation StackTableViewController
- (id)init {
self = ;
if (self) {
// Do something
localDataManager = [ init];
}
return self;
}
- (void)viewDidLoad {
;
self.navigationItem.rightBarButtonItem = self.editButtonItem;
;
//;
}
- (void)viewWillLayoutSubviews {
;
}
- (void)viewWillDisappear:(BOOL)animated {
;
}
// just to ignor a warning
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44;
}
- (void)didReceiveMemoryWarning {
;
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return localDataManager.fetchedResultController.sections.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
id <NSFetchedResultsSectionInfo> sectionInfo = ;
return ;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
Target *target = ;
[ deleteObject:target];
;
if () {
;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"StackTableViewCell";
Target *target = ;
StackTableViewCell *cell = ;
if (!cell)
{
NSArray *topLevelObjects = [ loadNibNamed:@"StackTableViewCell" owner:self options:nil];
cell = ;
}
cell.cellLabel.text = target.body;
cell.cellLabel.font = ;
// Configure the cell...
return cell;
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
switch (type) {
case NSFetchedResultsChangeInsert:
withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case NSFetchedResultsChangeDelete:
withRowAnimation:UITableViewRowAnimationAutomatic];
default:
break;
}
}
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
;
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
switch (type) {
case NSFetchedResultsChangeInsert:
withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case NSFetchedResultsChangeDelete:
withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case NSFetchedResultsChangeUpdate:
withRowAnimation:UITableViewRowAnimationAutomatic];
default:
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
;
}
</code></pre>
<p>这是我的 LocalDataManager.m:</p>
<pre><code>#import "LocalDataManager.h"
@implementation LocalDataManager
@synthesize stack,fetchedResultController;
-(id)init{
self = ;
if (self) {
//init
stack = ;
fetchedResultController = ;
;
}
return self;
}
-(void)reloadData{
;
}
- (NSFetchRequest *)targetsFetchRequest {
NSFetchRequest *fetchRequest = ;
NSSortDescriptor *sortDescriptor = [ initWithKey:@"time" ascending:NO];
NSArray *sortDescriptors = [ initWithObjects:sortDescriptor, nil];
;
return fetchRequest;
}
- (NSFetchedResultsController *)fetchedResultController {
if (fetchedResultController != nil) {
return fetchedResultController;
}
NSFetchRequest *fetchRequest = ;
fetchedResultController = [ initWithFetchRequest:fetchRequest managedObjectContext:stack.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
fetchedResultController.delegate = self;
return fetchedResultController;
}
@end
</code></pre>
<p>问题也可能与表格 View 的最后 3 个方法有关,或者我从管理器类而不是表格 View 中持有我的 FetchResultController,但我不知道直接指向它。 .请帮我解决这个问题。</p>
<p>谢谢!</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>从核心数据中删除记录后,您永远不会告诉表删除该行。</p>
<p>你需要:</p>
<pre><code>- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
Target *target = ;
[ deleteObject:target];
;
// Remove row from table
withRowAnimation:UITableViewRowAnimationFade];
if () {
;
}
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 无法从表格 View 中删除单元格,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/27583972/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/27583972/
</a>
</p>
页:
[1]