【问题标题】:Sync UITableViewCell with Core Data将 UITableViewCell 与核心数据同步
【发布时间】:2011-03-29 20:37:46
【问题描述】:

我有一些对象存储在 Core Data 中。例如,人。我将这些人与我的网络服务 (XML) 同步,并使用 NSFetchedResultsController 在 UITableView 中显示人。

一切正常,我的同步方法在后台线程中运行(同步方法只在 viewDidLoad 中调用一次)。

现在我想在 UITableViewCell 中显示一个 UISwitch。此开关应更改人员对象中的布尔值。另外,我需要显示一个带有一些图标的小工具栏。如果人是 x 则显示 y 图标,如果人是 a 则显示 b 图标...

有没有人有任何好的想法或例子来实现这个?

我的第一种方法: 在contentview中使用方法+ UISwitch作为子视图子类化和设置Core Data对象......我还需要实现layoutSubviews方法。

我不想构建一个 .nib 文件!我需要在其他单元格中重用单元格的所有部分...

但我不确定这是否是最好的方法......

【问题讨论】:

    标签: iphone core-data uitableview


    【解决方案1】:

    嗨 我离开了这条路。获取的结果控制器必须为您完成这项工作。 只需将 BOOL 核心数据状态连接到 uiswitch 并进行如下操作:

    - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
    
        UITableView *tableView = self.tableView;
        [tableView beginUpdates];
    
    
        switch(type) {
            case NSFetchedResultsChangeInsert:
            {
                NSIndexPath *newIndex = [NSIndexPath indexPathForRow:newIndexPath.row inSection:2];
    
                [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndex] withRowAnimation:UITableViewRowAnimationFade];
                break;
            }
            case NSFetchedResultsChangeDelete:
            {
                NSIndexPath *newIndex = [NSIndexPath indexPathForRow:indexPath.row inSection:2];
    
                [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:newIndex] withRowAnimation:UITableViewRowAnimationFade];
                break;
            }
            case NSFetchedResultsChangeUpdate:
            {
                NSIndexPath *newIndex = [NSIndexPath indexPathForRow:indexPath.row inSection:2];
    
                [self configureCell:(CompanyAndUserInfoCell *)[tableView cellForRowAtIndexPath:newIndex] atIndexPath:newIndex forTableView:tableView];
                break;
            }
            case NSFetchedResultsChangeMove:
            {
                NSIndexPath *newIndexStart = [NSIndexPath indexPathForRow:indexPath.row inSection:2];
                NSIndexPath *newIndexStop = [NSIndexPath indexPathForRow:newIndexPath.row inSection:2];
    
                [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:newIndexStart] withRowAnimation:UITableViewRowAnimationFade];
                [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexStop] withRowAnimation:UITableViewRowAnimationFade];
                break;
            }
        }
        [tableView endUpdates];
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-12
      • 2012-08-11
      • 2012-04-16
      • 1970-01-01
      • 2010-10-18
      • 1970-01-01
      相关资源
      最近更新 更多