【发布时间】:2015-04-15 03:51:52
【问题描述】:
我有一个tableViewController,它在顶部显示一个“添加”(静态)单元格,我希望它列出从managedObjectContext 中提取的对象(动态单元格)的属性。我发现this post 有助于使“添加”单元工作,但现在我已将对象保存到managedObjectContext,我发现它不会显示managedObjectContext 中对象的属性。
为了“查看”发生了什么,我将“动态”单元格设为橙色。当我将类别添加到managedObjectContext 时,橙色单元格的数量会正确更新,但我的managedObject(NSString)的属性无法显示在单元格中。
在我的fetchRequest 完成后,我在其中设置了一个断点,以查看数组中是否有LocationCategories(我的NSManagedObject)--有。
CategoryTVC.h
@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, strong) LocationCategory *category;
CategoryTVC.m
#define NUMBER_OF_STATIC_CELLS 1 // this can be updated
// Sets up an array to dump LocationCategories into
@property (nonatomic, strong) NSArray *locationCategories;
// cell identifier strings
static NSString *DynamicIdentifier = @"DynamicIdentifier";
static NSString *StaticIdentifier = @"StaticIdentifier";
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.title = @"Select a Category";
// Core Data
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
self.managedObjectContext = [appDelegate managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"LocationCategory" inManagedObjectContext:self.managedObjectContext]];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"categoryName" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1];
[fetchRequest setSortDescriptors:sortDescriptors];
NSArray *categories = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
self.locationCategories = categories; // probably duplicate to line above, but modeling Apple's sample code
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:DynamicIdentifier];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:StaticIdentifier];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0) {
return @"";
} else if (section == 1) {
return @"Categories";
} else {
// This is just to shut up the compiler
return nil;
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return 1;
} else {
return self.locationCategories.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:StaticIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:StaticIdentifier];
}
cell.textLabel.text = @"Create new category";
return cell;
} else if (indexPath.section == 1) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DynamicIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DynamicIdentifier];
}
NSManagedObject *locationCategory = [self.locationCategories objectAtIndex:indexPath.row];
cell.textLabel.text = [locationCategory valueForKey:@"categoryName"];
cell.backgroundColor = [UIColor orangeColor]; // TODO: Gives cell a color to see how many self.locationCategories there are
return cell;
}
return nil;
}
为了完整起见,我在下面添加了我的 LocationCategory 类:
**LocationCategory.h**
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class PointOfInterest;
@interface LocationCategory : NSManagedObject
@property (nonatomic, retain) id categoryColor;
@property (nonatomic, retain) NSString * categoryName;
@property (nonatomic, retain) NSSet *pointOfInterest;
@end
**LocationCategory.m**
#import "LocationCategory.h"
#import "PointOfInterest.h"
@implementation LocationCategory
@dynamic categoryColor;
@dynamic categoryName;
@dynamic pointOfInterest;
@end
【问题讨论】:
标签: ios objective-c uitableview core-data