UITableView更新了正确数量的托管对象,但不显示值

时间:2015-04-15 03:51:52

标签: ios objective-c uitableview core-data

我有tableViewController在顶部显示“添加”(静态)单元格,我希望它列出从managedObjectContext拉出的对象(动态单元格)的属性。我发现this post有助于“添加”单元格工作,但是现在我已经将对象保存到managedObjectContext,我发现它没有显示对象的属性。 managedObjectContext

为了“看到”正在发生的事情,我将“动态”细胞变为橙色。当我向managedObjectContext添加类别时,橙色单元格的数量会正确更新,但我无法在单元格中显示managedObjectNSString)的属性。< / p>

我在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

2 个答案:

答案 0 :(得分:2)

您需要在获取请求后调用表视图上的reloadData(所以viewWillAppear中的最后一行)。

此外,当您为单元格注册一个类时,不需要if(cell == nil)子句,因为您的单元格永远不会为零。

答案 1 :(得分:0)

虽然我很想删除这个问题,但我会留下它以防万一其他人犯了同样的(愚蠢的)错误。

@rdelmar发现了一个更新问题,该问题通过在viewWillAppear中向CategoryTVC.m添加以下行来解决:

[self.tableView reloadData];

我有另一个名为AddCategoryViewController的ViewController,它具有以下属性。

@property (strong, nonatomic) IBOutlet UITextField *categoryNameTextField;
@property (strong, nonatomic) NSString *categoryName;

当我保存新的LocationCategory时,我的保存就是这样做的:

- (IBAction)saveCategory:(id)sender {
    NSLog(@"Save Button Clicked");
    NSError *error;
    error = nil;
    LocationCategory *newCategory = [NSEntityDescription insertNewObjectForEntityForName:@"LocationCategory" inManagedObjectContext:self.managedObjectContext];
    newCategory.categoryName = self.categoryName; // THIS IS WRONG!!!!
    newCategory.categoryColor = self.categoryColor;
    [self.managedObjectContext save:&error];
    [[self navigationController] popViewControllerAnimated:YES];
    NSLog(@"AddCategoryViewController popped off");
}

上面的错误行应为:

newCategory.categoryName = self.categoryNameTextField.text;

我的错误的净影响是它将乱码丢弃到我的managedObjectContext中,并使它看起来好像有self.locationCategories.count的目的。