【问题标题】:AQGridView backed by a NSFetchedResultsController由 NSFetchedResultsController 支持的 AQGridView
【发布时间】:2011-12-01 20:25:44
【问题描述】:

我正在尝试实现一个使用获取的结果控制器作为其数据源的 AQGridView。

我不太确定如何使用网格视图处理 NSFetchedResultsController 委托方法;即内容变化的。我了解如何将 FRC 用于其他网格视图数据源委托。

有人能指出我正确的方向吗?

【问题讨论】:

  • 我看到提供了 InsertItemAtIndicies/DeleteItemAtIndicies 方法,但它们采用 indexSet。也许问题是如何将 indexPath 转换为 indexSet?
  • 在继续之前说一下:AQGridView 在更新 gridView 单元格,尤其是网格数方面有非常愚蠢的限制。在对网格进行任何更改之前,您需要完全更新您的数据源,否则会触发内置异常。

标签: objective-c ios ipad nsfetchedresultscontroller aqgridview


【解决方案1】:

结果应该看起来像这样:

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
  [gridView beginUpdates];
}

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
       atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
  switch(type)
  {
    case NSFetchedResultsChangeInsert:
      break; 
    case NSFetchedResultsChangeDelete:
      break;
  }
}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
   atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
  newIndexPath:(NSIndexPath *)newIndexPath
{

  ChannelPageViewController *currentPageController, *destinationPageController;

  NSIndexSet * indices = [[NSIndexSet alloc] initWithIndex: indexPath.row];
  NSIndexSet *newIndices = [[NSIndexSet alloc] initWithIndex:newIndexPath.row];

  switch(type) {
      case NSFetchedResultsChangeInsert:
        [gridView insertItemsAtIndices:newIndices withAnimation:AQGridViewItemAnimationNone];
      break;

      case NSFetchedResultsChangeDelete:
        [gridView deleteItemsAtIndices:indices withAnimation:AQGridViewItemAnimationNone];
        break;

      case NSFetchedResultsChangeUpdate:
        [gridView reloadItemsAtIndices:indices withAnimation:AQGridViewItemAnimationNone];
        break;

      case NSFetchedResultsChangeMove:
        [gridView deleteItemsAtIndices:indices withAnimation:AQGridViewItemAnimationNone];
        [gridView insertItemsAtIndices:newIndices withAnimation:AQGridViewItemAnimationNone];
        break;
   }
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
  [gridView endUpdates];
  if ([[frc fetchedObjects] count] == 1) {
    [gridView reloadData];
  }

}

【讨论】:

    【解决方案2】:

    由于 AQGridView 没有节,处理它的最佳方法是实现 NSFethcedresultscontroller 委托方法,并忽略与更新节相关的案例的任何代码。还要确保在没有 sectionNameKeyPath 的情况下初始化 fetchrequest。

    然后只需按照正常模式更新行,但使用 NSIndexSet 而不是 NSIndexPath 和 InsertItemAtIndicies/DeleteItemAtIndicies 而不是 insertRowAtIndexPath/deleteRowAtIndexPath

    我现在将我的 AQGridView 移动到 CoreData,所以我会在完成后立即发布我的答案的任何更新...

    【讨论】:

      【解决方案3】:

      当内容改变时我会做一个

      [self.gridView reloadData];
      

      或类似的情况。和tableview完全一样。

      【讨论】:

      • 是的,虽然我一直在寻找一种使用插入/删除方法的方法,但可以利用可用的动画。使用reloadData 只会让事情神奇地出现。
      • 那么您可以将此答案标记为正确。它也会给你带来声誉。
      猜你喜欢
      • 1970-01-01
      • 2012-10-02
      • 1970-01-01
      • 2012-01-14
      • 1970-01-01
      • 1970-01-01
      • 2020-09-04
      • 1970-01-01
      • 2021-09-27
      相关资源
      最近更新 更多