I'm trying to update the items in a UITableViewCell connected to my root view table via a UIButton in my detail view. The item cell is imported into the root view, loaded and registered in viewDidLoad, updated in viewDidAppear, and added to the table in 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];

}

Even though Xcode predicts "createItem", I get a warning that "No visible @interface for 'RoomList' declares the selector 'createItem:'

I've imported RoomList.h, where:

- (RoomItem *)createItem;
+ (RoomList *)sharedStore;

In 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];
}

In RoomItem I have:

- (id)initWithRoom:(NSString *)room Building:(NSString *)building;

I just can't wrap my head around what I could be missing here. Any thoughts?

Thanks.

Edit (root view method that writes to item cell):

- (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;
}

This is what worked for me. It seems I was not properly fetching the core data entity. However, the objectAtIndex query is still an issue since it only updates the RoomItem at index:0. How can a get the index of the RoomItem I'm trying to update?

- (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;
}
有帮助吗?

解决方案

Your question title is about master/detail view controller communication. But a compiler warning seems to be at the heart of your question.

It's a matter of method signatures.

[[RoomList sharedStore] createItem:item];

This calls method signature is -createItem:. You declare -(RoomItem *)createItem; or -createItem. Note there's no colon, no passed in parameter.

It seems unlikely that you would pass a RoomItem in to a method named createItem so you likely want to just change the above call to:

RoomItem *item = [[RoomList sharedStore] createItem];
// item is now the newly created entity.

Or change the createItem method to something like:

-(RoomItem *)createItemWithRoom:(NSString *)room andBuilding:(NSString *)building{
    // Use room & building to create managed object
}

And then use it like:

RoomItem *item = [[RoomList sharedStore] createItemWithRoom:[_roomTxt text] andBuilding:[_buildingTxt text]];

EDIT

If you're trying to refresh the master tableview in the detail view of a split view controller (on iPad) you can use code like this to reload the master tableview.

UINavigationController *masterController = [self.splitViewController.viewControllers objectAtIndex:0];
UITableViewController *tableController = (UITableViewController *)masterController.visibleViewController;
UITableView *tableView = tableController.tableView;
[tableView reloadData];

On the iPhone/iPod you can obtain the navigation controllers root view controller and refresh the table view with code like this.(written a bit more compactly)

UITableViewController *masterTableViewController = [self.navigationController.viewControllers objectAtIndex:0];
[masterTableViewController.tableView reloadData];

Of course instead of a full reload you could use a method like insertRowsAtIndexPaths:withRowAnimation: if you know the future indexPath(s) at the time of insertion.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top