• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 无法从表格 View 中删除单元格

[复制链接]
菜鸟教程小白 发表于 2022-12-13 04:09:07 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个 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,但我不知道直接指向它。 .请帮我解决这个问题。

谢谢!



Best Answer-推荐答案


从核心数据中删除记录后,您永远不会告诉表删除该行。

你需要:

- (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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap