gpt4 book ai didi

ios - UITableView 更新正确数量的 managedObjects,但不会显示值

转载 作者:行者123 更新时间:2023-11-28 21:47:13 24 4
gpt4 key购买 nike

我有一个 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

最佳答案

您需要在获取请求后对 TableView 调用 reloadData(因此 viewWillAppear 中的最后一行)。

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

关于ios - UITableView 更新正确数量的 managedObjects,但不会显示值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29641287/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com