【问题标题】:How to use an NSOutlineview with Coredata, grouping entities by their attributes?如何将 NSOutlineview 与 Coredata 一起使用,按属性对实体进行分组?
【发布时间】:2013-08-25 15:58:08
【问题描述】:

我炸了。我已经被困在这两天了,谷歌搜索,阅读,谷歌搜索和阅读。

我有一个名为 Session 的实体和一个名为 sessionYear (NSNumber) 的属性。

我想创建一个 NSOutlineView,按年份对它们进行分组,然后按月份对它们进行排序,sessionMonth (NSString)。像这样:

1984

十月
十一月
十二月

1989

一月
二月

2002

三月
七月
十月

我发现了很多关于将实体分组到其他实体下的信息,以及使用数组控制器和字典的各种方法。不幸的是,我发现的大部分内容已经过时或表面上不适用于我的情况。一般来说,我是开发和 coredata 的新手,如果有任何关于这方面的指导,我将不胜感激。

同样,年份是实体的属性。

最终,我希望能够单击这些实体并填充一个表格视图。如果可能的话,我有兴趣使用绑定来执行此操作,但我一直无法找到可靠的信息。感谢任何帮助或资源链接。

编辑:

我使用了 NSOutlineViewDelegate 方法和附加的四种方法。但是,现在似乎 sessionMonths 在年份扩大时出现在视图中,然后立即消失。如果另一个(不同的)年份被扩展,所有的 sessionMonths 都会出现在它们应该出现的地方,然后又会立即消失。我已经走了这么远……只是一瞥。关于从哪里开始有什么建议吗?

- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
    if (item == nil) {
        return [savedSessionsOutlineViewData count];
    }

    return [item count];
}

- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
    if (item == nil) {
        item = savedSessionsOutlineViewData;
    }

    if ([item isKindOfClass:[NSMutableArray class]]) {
        return [item objectAtIndex:index];
    }
    else if ([item isKindOfClass:[NSDictionary class]]) {
        NSArray *keys = [item allKeys];
        return [item objectForKey:[keys objectAtIndex:index]];
    }
    return nil;
}

- (id)outlineView:(NSOutlineView *)outline objectValueForTableColumn:(NSTableColumn *)column byItem:(id)item {

    // If the item is a "yearArray" holding sessions.
   if ([item isKindOfClass:[NSMutableArray class]]) {
        NSArray *keys = [savedSessionsOutlineViewData allKeysForObject:item];
        return [keys objectAtIndex:0];
   } else if ([item isKindOfClass:[Session class]]) {
       //NSLog (@"Item returned isKindOfClass:Session");
       return [item valueForKey:@"sessionMonth"];
   }

    return nil;
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
    if ([item isKindOfClass:[NSMutableArray class]] || [item isKindOfClass:[NSDictionary class]]) {
        if ([item count] > 0) {
            return YES;
        }
    }

    return nil;
}

【问题讨论】:

    标签: objective-c core-data nsoutlineview


    【解决方案1】:

    在 NSOutline 视图的属性检查器中,将突出显示设置为源列表。

    【讨论】:

      【解决方案2】:

      最好的方法可能是为顶级组和每个子组创建单独的数组。因此,获取您想要显示的所有独特年份的列表。您可以使用集合运算符 @distinctUnionOfObjects.sessionYear 从现有的 Session 数组中获取 NSNumber 数组。

      下面的一些示例代码 - 我没有运行它,但从现有应用程序中获取了一些代码并重命名以适合您的模型。可能有一些错误..

          @implementation SessionOutlineViewController <NSOutlineViewDataSource> {
      
              NSArray *sortedYearsArray;  
              NSMutableDictionary *childrenDictionary;  // for each key (year) we put an array of sessions in this dictionary
      
          }
          @end
      
          @implementation SessionOutlineViewController
      
          - (void)initialise {
              NSArray* sessions = [self getData:@"Session" sortKey:@"date" predicate:nil];
      
              // Get an array of distinct years
              NSArray *yearsArray = [sessions valueForKeyPath:@"@distinctUnionOfObjects.sessionYear"];
      
              NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"description" ascending:YES];
              NSArray * sortedYearsArray =[yearsArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
      
      
              for (NSNumber year in sortedYearsArray) {
                  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"sessionYear == %@", year];
      
                  NSArray * sessionForYear = [self getData:@"Session" sortKey:@"date" predicate:predicate];
      
                  [childrenDictionary setObject:sessionsForYear forKey:year];
      
              }
      
          }
      
      
      - (NSArray*)getData:(NSString*)entityName sortKey:(NSString*)sortKey predicate:(NSPredicate*)predicate
      {
      
          NSFetchRequest *req = [[NSFetchRequest alloc] init];
      
          NSEntityDescription * entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:_managedObjectContext];
      
          if (entity == nil) {
              NSLog(@" error entity %@ NOT FOUND!", entityName);
              return nil;
          }
      
          [req setEntity:entity];
          [req setIncludesPropertyValues:YES];
          if (predicate != nil)
              [req setPredicate:predicate];
          if (sortKey != nil) {
              NSSortDescriptor *indexSort = [[NSSortDescriptor alloc] initWithKey:sortKey ascending:YES];
              NSArray *sorters = [NSArray arrayWithObject:indexSort]; indexSort = nil;
      
              [req setSortDescriptors:sorters];
          }
      
          NSError *error;
      
          NSArray *result = [managedObjectContext executeFetchRequest:req error:&error];
          if (result == nil)
              return nil;
      
          return result;
      }
      @end
      
      @implementation SessionOutlineViewController (NSOutlineViewDataSource)
      
      - (NSArray *)childrenForItem:(id)item {
          NSArray *children;
          if (item == nil) {
              children = sortedYearsArray;
          } else {
              children = [childrenDictionary objectForKey:item];
          }
          return children;
      }
      
      - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
          return [[self childrenForItem:item] objectAtIndex:index];
      }
      
      - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
          if ([outlineView parentForItem:item] == nil) {
              return YES;
          } else {
              return NO;
          }
      }
      
      - (NSInteger) outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
          return [[self childrenForItem:item] count];
      }
      @end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-06-30
        • 2021-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多