我正在尝试更新通过详细视图中的 UIButton 连接到我的根视图表的 UITableViewCell 中的项目。item cell 被导入到根视图中,在 viewDidLoad 中加载并注册,在 viewDidAppear 中更新,并在 tableView:cellForRowAtIndexPath 中添加到表中。
- (IBAction)addItem:(id)sender
{
//saves new list
RoomItem *item = [[RoomItem alloc] initWithRoom:[_roomTxt text] Building:[_buildingTxt text]];
[[RoomList sharedStore] createItem:item];
//allows for immediate return to rootView upon action (save button push in this case)
[self.navigationController popToRootViewControllerAnimated:YES];
}
即使 Xcode 预测“createItem”,我也会收到警告说“'RoomList' 没有可见的@interface 声明选择器'createItem:'
我已经导入了 RoomList.h,其中:
- (RoomItem *)createItem;
+ (RoomList *)sharedStore;
在 RoomList.m 中:
- (RoomItem *)createItem
{
//tracks what number item it's creating
double order;
if ([allItems count] == 0) {
order = 1.0;
}
else
{
order = [[[allItems lastObject] objectIndex] doubleValue] + 1.0;
}
NSLog(@"Adding after %d items, order = %.2f", [allItems count], order);
RoomItem *p = [NSEntityDescription insertNewObjectForEntityForName:@"RoomItem"
inManagedObjectContext:context];
[p setRoom:[NSString string]]; //there was an order call here
[p setBuilding:[NSString string]];
[p setBuildingThumbnail:[UIImage imageNamed:[NSString string]]]; //may need to be changed to another imageNamed function
[p setBuildingThumbnailData:[NSData data]];
[p setObjectIndex:[NSNumber numberWithDouble:order]];
[allItems addObject:p];
return p;
}
+ (RoomList *)sharedStore
{
static RoomList *sharedStore = nil;
if (!sharedStore) {
sharedStore = [[super allocWithZone:nil] init];
}
return sharedStore;
}
+ (id)allocWithZone:(NSZone *)zone
{
return [self sharedStore];
}
在 RoomItem 我有:
- (id)initWithRoom:(NSString *)room Building:(NSString *)building;
我只是无法理解我在这里可能会丢失的东西。有什么想法吗?
谢谢。
编辑(写入项目单元格的根视图方法):
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BNRItem *p = [[[BNRItemStore defaultStore] allItems]
objectAtIndex:[indexPath row]];
//[[cell textLabel] setText:[p description]];
//get the new or recycled cell
HomepwnerItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomepwnerItemCell"];
[cell setController:self];
[cell setTableView:tableView];
//configure the cell with the BNRItem
[[cell nameLabel] setText:[p itemName]];
[[cell serialNumberLabel] setText:[p serialNumber]];
[[cell valueLabel] setText:[NSString stringWithFormat:@"$%d", [p valueInDollars]]];
[[cell thumbnailView] setImage:[p thumbnail]];
return cell;
}
这对我有用。看来我没有正确获取核心数据实体。但是,objectAtIndex 查询仍然是一个问题,因为它只更新了 index:0 处的 RoomItem。如何获取我要更新的 RoomItem 的索引?
- (RoomItem *)updateItemWithRoom:(NSString *)room Building:(NSString *)building
{
NSError *error = nil;
NSEntityDescription *entity = [NSEntityDescription entityForName:@"RoomItem" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
RoomItem *currentRoomItem = [[context executeFetchRequest:request error:&error] objectAtIndex:0];
request = nil;
[currentRoomItem setRoom:room];
[currentRoomItem setBuilding:building];
[self saveChanges];
return currentRoomItem;
}