为什么我无法删除 UITableView 的底行?

2023-12-27

当用户按下“编辑”时,我的 UITableView 在顶部添加一个插入行(带有绿色加号),并将所有其他行置于删除模式(红色减号)。或者,用户可以在不按编辑按钮的情况下滑动删除。我使用几个 Ivar 来跟踪表是否处于通过滑动或按下编辑按钮的编辑模式,并采取相应的操作(例如,更新 numberOfRowsInTableView:按下“编辑”时使用额外的插入行)。

一切都完美运行,除了以下内容:在编辑模式下(即用户明确点击编辑按钮,并且插入行出现在顶部),如果用户尝试删除底行,则上一行将被删除。删除任何其他行就可以了。

EDIT -- It appears删除上面的行,但如果我立即退出并重新加载应用程序,结果发现底行毕竟已经消失了。所以我猜测我的 UITableView 与我的 NSFetchedResultsController 某处不同步。

这是我正在使用的代码:

#import "ChecklistsViewController.h"
#import "Checklist.h"

@interface ChecklistsViewController (private)
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
- (void)addingView;
@end


@implementation ChecklistsViewController

@synthesize category, managedObjectContext, fetchedResultsController;


- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        editingFromSwipe = NO;
        tableIsEditing = NO;
    }
    return self;
}

- (void)dealloc
{
    [category release];
    [managedObjectContext release];
    [fetchedResultsController release];
    [super dealloc];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    editingFromSwipe = NO;
    tableIsEditing = NO;
    self.navigationItem.rightBarButtonItem = self.editButtonItem;    
    self.tableView.allowsSelectionDuringEditing = YES;
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[self.fetchedResultsController sections] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    int rows = [sectionInfo numberOfObjects];

    if (self.editing) {
        if (!editingFromSwipe && tableIsEditing) {
            return rows +1;
        }
        return rows;
    }
    tableIsEditing = NO;
    return rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


    NSLog(@"Should go into if statement here! \n");

    if (tableView.editing) { //
        if ((indexPath.row == 0) && (!editingFromSwipe)) {
            NSLog(@"Configuring Add Button Cell while editing \n");
            cell.textLabel.text = @"Add New Checklist";
            cell.detailTextLabel.text = nil;
        }
        else {
            NSLog(@"Configuring other cells while editing \n");
            [self configureCell:cell atIndexPath:indexPath];
        }

    }
    else {
        NSLog(@"Configuring Cell Normally While Not Editing \n");
        [self configureCell:cell atIndexPath:indexPath];
    }


    return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        // Delete the managed object for the given index path
        NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];

        int numberOfRows = [self tableView:tableView numberOfRowsInSection:indexPath.section];
        int rowBeingDeleted = indexPath.row +1;

        if (tableIsEditing && !editingFromSwipe && numberOfRows == rowBeingDeleted) {
            [context deleteObject:[self.fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row-1 inSection:indexPath.section]]];
        }
        else {
            [context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
        }

        // Save the context.
        NSError *error = nil;
        if (![context save:&error])
        {
            // TO DO: Fix error code.
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    }    
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        [self addingView];        
    }   
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    int row = indexPath.row;

    if (self.editing && row == 0) {
        if (!editingFromSwipe && tableIsEditing) {
            return UITableViewCellEditingStyleInsert;
        }
        else if (editingFromSwipe) { 
            return UITableViewCellEditingStyleDelete;
        }

    }
    return UITableViewCellEditingStyleDelete;
}


- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    editingFromSwipe = YES;
    [super tableView:tableView willBeginEditingRowAtIndexPath:indexPath];
}

- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    [super tableView:tableView didEndEditingRowAtIndexPath:indexPath];
    editingFromSwipe = NO;
}


- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];

    NSArray *addRow = [NSArray arrayWithObjects:[NSIndexPath indexPathForRow:0 inSection:0], nil];
    [self.tableView beginUpdates];

    if (!editingFromSwipe) {
        if (editing) {
            tableIsEditing = YES;
            [self.tableView insertRowsAtIndexPaths:addRow withRowAnimation:UITableViewRowAnimationLeft];
        }
        else {
           [self.tableView deleteRowsAtIndexPaths:addRow withRowAnimation:UITableViewRowAnimationLeft];
        } 
    }
    [self.tableView endUpdates];
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row != 0) {
        TO DO: Code for when row is selected
    }
}


#pragma mark - Data


- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    Checklist *aChecklist = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = aChecklist.name;
    cell.detailTextLabel.text = aChecklist.category.name;
}


- (void) addingView// :(id)sender
{
    AddingViewController *viewController = [[AddingViewController alloc] initWithNibName:@"AddingViewController" bundle:nil];

    viewController.delegate = self;
    viewController.title = @"Add Checklist";

    // Create the navigation controller and present it modally
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
    [self presentModalViewController:navigationController animated:YES];

    viewController.textLabel.text = @"Enter new checklist name";

    [navigationController release];
    [viewController release];
}


#pragma mark - AddingViewDelegate


- (void)addingViewController:(AddingViewController *)addingViewController didAdd:(NSString *)itemAdded
{
    if (itemAdded != nil) {

        // Turn off editing mode.
        if (self.editing) [self.navigationController setEditing:NO animated:NO];

        // Add the category name to our model and table view.

        // Create a new instance of the entity managed by the fetched results controller.
        NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
        NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
        Checklist *newChecklist = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];

        [category addChecklistsObject:newChecklist];

        newChecklist.name = itemAdded;        
        // [newChecklist setDateStamp:[NSDate date]];

        // Save the context.
        NSError *error = nil;
        if (![context save:&error])
        {
            TO DO: fix error code.
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }


    }

    [self dismissModalViewControllerAnimated:YES];
}


#pragma mark - Fetched results controller

- (NSFetchedResultsController *)fetchedResultsController
{
    if (fetchedResultsController != nil)
    {
        return fetchedResultsController;
    }

    // Set up the fetched results controller.

    // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Checklist" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];
    // Set 4* the predicate so we only see checklists for this category.
    NSPredicate *requestPredicate = [NSPredicate predicateWithFormat:@"category.name = %@", self.category.name];
    [fetchRequest setPredicate:requestPredicate];    
    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];    
    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];    
    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".    

    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
                                                                                                managedObjectContext:self.managedObjectContext 
                                                                                                  sectionNameKeyPath:nil 
                                                                                                           cacheName:nil];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;


    [aFetchedResultsController release];
    [fetchRequest release];
    [sortDescriptor release];
    [sortDescriptors release];

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error])
    {
       // TO DO: error stuff
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return fetchedResultsController;
} 


#pragma mark - Fetched results controller delegate


- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
    [self.tableView beginUpdates];
}


- (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:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath
{
    UITableView *tableView = self.tableView;

    switch(type)
    {       
        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeUpdate:
            [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
            break;

        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    [self.tableView endUpdates];
}

@end

您可以将静态单元格添加到 UITableViews 中,以从 NSFetchedResultsController 获取数据。但要做到这一点,你必须调整几乎所有在其中之一中使用的 NSIndexPathsUITableViewDelegate, UITableViewDataSource or NSFetchedResultsControllerDelegate方法。

我添加了一些辅助方法,将表视图的索引路径转换为获取的结果控制器的索引路径,反之亦然。如果你想在顶部添加一行,可以使用这样的东西:

- (NSIndexPath *)tableIndexPathFromNSFRCIndexPath:(NSIndexPath *)ip {
    if (editingMode && ip.section == 0) {
        NSIndexPath *newIP = [NSIndexPath indexPathForRow:ip.row+1 inSection:ip.section];
        return newIP;
    }
    return ip;
}

- (NSIndexPath *)nsfrcIndexPathFromTableIndexPath:(NSIndexPath *)ip {
    if (editingMode && ip.section == 0) {
        NSIndexPath *newIP = [NSIndexPath indexPathForRow:ip.row-1 inSection:ip.section];
        return newIP;
    }
    return ip;
}

然后您必须更改将索引路径从表传递到 fetchedresultscontroller 或从 frc 传递到表的每个方法。我给大家举两个例子。

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath {
    newIndexPath = [self tableIndexPathFromNSFRCIndexPath:newIndexPath];
    indexPath = [self tableIndexPathFromNSFRCIndexPath:indexPath];
    switch(type) {
        case NSFetchedResultsChangeInsert:
            [self.listTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
        case NSFetchedResultsChangeDelete:
            [self.listTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
        case NSFetchedResultsChangeUpdate:
            [self configureCell:[self.listTableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
            break;
        case NSFetchedResultsChangeMove:
            [self.listTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [self.listTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [aTableView deselectRowAtIndexPath:indexPath animated:YES];
    if (editingMode && indexPath.section == 0 && indexPath.row == 0) {
        // Add New entry...
    }
    else {
        indexPath = [self nsfrcIndexPathFromTableIndexPath:indexPath];
        NSManagedObject *selectedObject = [self.fetchedResultsController objectAtIndexPath:indexPath]);
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么我无法删除 UITableView 的底行? 的相关文章

  • UIPageViewController:获取当前页面

    在过去的几天里 我一直在努力解决这个问题 经过所有这些杂耍 我发现我所需要的只是数据源方法中的当前索引 以使用当前可见页码进行更新 我有这个UIPageViewController数据源方法 我需要使用当前索引来获取委托方法的当前可见页面p
  • 保留“自我”的坏做法?

    我有一个简单的疑问 希望有人能解答 保留自我是不好的做法吗 我有一个想要创建的服务器请求对象 我希望能够通过以下方式使用它 ARequest request ARequest request someParam request delega
  • 在 Objective-C 中检查 Json 响应的空值

    AT null EA null AD2 OP null AdsLst EMs null ND MN null FN Sony LN J ZP 23456 CT 1 PP 0 cId 161464 Pos null WPExt null OS
  • 我可以/如何确定设备是否有振动?

    我有一些设置可以启用 禁用某些操作的振动 但我发现如果设备没有振动能力 则显示它们毫无意义 有没有办法检查用户是否正在使用 iPod touch 以及它是否有振动 我不确定除了进行模型检查之外还有其他方法可以做到这一点 这可能不是一个很好的
  • Xcode 5 - clang:错误:链接器命令失败,退出代码 1(使用 -v 查看调用)

    I am facing a linker error when running my source code I have attached a screenshot also I am new in iOS development and
  • 如何在iPhone应用程序中创建折线图? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 找出窗口中的 UIBarButtonItem 框架?

    UIBarButtonItem不延长UIView 所以没有像框架属性这样的东西 但有什么办法我可以得到它是什么CGRect框架 相对于应用程序UIWindow 你喜欢用吗私有API https stackoverflow com quest
  • 使 UITableView 中的动态更新内容可供 VoiceOver 访问

    我正在努力让我的应用程序更易于访问 到目前为止 标签和提示等标准可访问性正在创造奇迹 然而 我在动态更新 UITableView 中显示的内容时遇到了问题 表的每一行大约每秒更新一次 但如果我尝试在此时创建每个单元格的accessibili
  • iPhone 上的摇动视觉效果(不是摇动设备)

    在登录失败时 我宁愿避免显示警报 它太短暂了 显示警报 然后在登录屏幕上的某处显示文本似乎是重复的 因此 当用户输入错误的用户 ID 和密码时 我希望它能像 Mac 登录屏幕那样以图形方式摇动我的登录视图 有人知道是否有办法实现这一点 或者
  • 发射图像偏离 20 像素(在 y 轴上)

    我有一个名为 Default png 的 320 480 大小的启动图像 并且该应用程序配置为还显示状态栏 当我的应用程序启动时 图像的前 20 个像素被状态栏截断并隐藏 根据 Apple 的 HIG 列出的 iPhone 和 iPod T
  • Core Data + CloudKit 无法在其他设备上自动刷新?

    我的 SwiftUI 应用程序与 Core Data CloudKit 一起使用 我可以从 Mac 或 iPhone 将新条目保存到数据库中 该应用程序对所有设备使用完全相同的项目 代码库 唯一的问题是我需要关闭应用程序并重新打开它才能查看
  • UIFont Woes(一些自定义字体加载,但其他字体不加载)

    我在加载某些自定义字体时遇到问题 我遵循了这个问题的 400 多个赞同的传统答案中的建议 并且它非常适合一个项目 然而 在我正在从事的另一个项目中 我遇到了加载 UIFont 的问题 这些问题与帖子中发现的问题有些相似向 UIAppFont
  • 使用NSString的drawAtPoint方法代替CGContextShowGlyphsAtPoint问题

    在我的应用程序中 我试图沿着路径渲染文本 这对于大多数字符来说都很好 但对于日语 或任何非 Mac Roman 就不行了 建议我使用 NSString drawAtPoint 它在我的 CATiledLayer 中显示正确的字符 然而 它们
  • iPhone 上的加速器范围是多少?

    我似乎无法在网上找到任何关于此的文档 而且我正在谷歌搜索的内容给了我很多相互矛盾的信息 From iphonedevsdk com http www iphonedevsdk com forum iphone sdk development
  • 如何像键盘一样呈现选择器视图?

    我希望当我按下按钮 就像键盘一样 时显示 UIPickerView 然后在用户点击屏幕上的任意位置时消失 我怎样才能做到这一点 谢谢 更多背景信息 我在 UITableViewCell 中有一个名为 Months 的 UITextField
  • 如何让 iPhone 发出蜂鸣声?

    什么代码可以让我在 iPhone 上发出标准的蜂鸣声 好吧 这取决于您想要什么样的声音 以下是如何使用 AVFoundation 音频框架播放声音 import
  • 推送通知中的设备令牌

    我只想向某些用户发送推送通知 根据我在苹果文档中所经历的内容 注册推送通知的代码是这样的 void applicationDidFinishLaunching UIApplication app other setup tasks here
  • 如何在 xcode 中从 nib 文件创建视图?

    我有以下代码来创建视图并将其放入滚动视图中以允许分页代码工作正常 但是我不能做的是从 nib 文件加载视图 换句话说 我想使用 initWithNibName 而不是 initWithFrame void createPageWithCol
  • cllocation 和 mkreversegeocoder

    我尝试使用 cllocation 和 mkreversegeocoder 检索城市名称 在我的 viewdidload 方法中 我是 cllocationmanager self locManager CLLocationManager a
  • 将蒙版图像作为 PNG 文件写入磁盘

    基本上 我从网络服务器下载图像 然后将它们缓存到磁盘上 但在这样做之前 我想屏蔽它们 我正在使用每个人似乎都指出的屏蔽代码 可以在这里找到 http iosdevelopertips com cocoa how to mask an ima

随机推荐