我正在尝试通过详细视图中的UIButton更新连接到我的根视图表的UITableViewCell中的项目。项单元格导入到根视图中,在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查询仍然是一个问题,因为它只更新indexI:处的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;
}
答案 0 :(得分:0)
您的问题标题是关于主/详细视图控制器通信。但编译器警告似乎是你问题的核心。
这是方法签名的问题。
[[RoomList sharedStore] createItem:item];
此调用方法签名为-createItem:
。您声明-(RoomItem *)createItem;
或-createItem
。请注意,没有冒号,没有传递参数。
您似乎不太可能将RoomItem
传递给名为createItem
的方法,因此您可能只想将上述调用更改为:
RoomItem *item = [[RoomList sharedStore] createItem];
// item is now the newly created entity.
或将createItem
方法更改为:
-(RoomItem *)createItemWithRoom:(NSString *)room andBuilding:(NSString *)building{
// Use room & building to create managed object
}
然后使用它:
RoomItem *item = [[RoomList sharedStore] createItemWithRoom:[_roomTxt text] andBuilding:[_buildingTxt text]];
修改强>
如果您尝试在拆分视图控制器(在iPad上)的详细视图中刷新主表视图,则可以使用此类代码重新加载主表视图。
UINavigationController *masterController = [self.splitViewController.viewControllers objectAtIndex:0];
UITableViewController *tableController = (UITableViewController *)masterController.visibleViewController;
UITableView *tableView = tableController.tableView;
[tableView reloadData];
在iPhone / iPod上,你可以获得导航控制器的根视图控制器,并用这样的代码刷新表格视图。(写得更紧凑)
UITableViewController *masterTableViewController = [self.navigationController.viewControllers objectAtIndex:0];
[masterTableViewController.tableView reloadData];
当然,如果您在插入时知道未来insertRowsAtIndexPaths:withRowAnimation:
,则可以使用indexPath(s)
之类的方法代替完全重新加载。