Search code examples
uitableviewarchivemaster-detail

how to update archive and table view cell


I'm working on a MasterDetail app, and having an issue updating my itemCells. Using the code from the DetailViewController below, I had hoped to be able to access the detail view after touching the cell (for which there is a custom ItemCell), edit the contents in the text fields, hit the save button, update the item data, and pop back to the MasterViewController.

From DetailViewController.m

- (IBAction)addItem:(id)sender

{

RoomItem *item = [[RoomItem alloc] initWithName:[_roomTxt text] Building:[_buildingTxt text]];

//add item is an addObject method i've placed in RoomList.m
[[RoomList sharedStore] addItem:item];

[self.navigationController popToRootViewControllerAnimated:YES];

}

It's probably obvious that this is only creating a new table view item and not updating the original, but I'm a silly noob and can't locate anything in the documentation to help me figure this out.

Ideas? Please let me know if I'm not providing enough information/code to explain the situation accurately.

Also, is there a way to achieve the same functionality through a UITableViewAccessory?

Thanks.

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?

Edit:

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

Solution

  • Try to call on viewdidappear to Reload the Data from the table view with reload data.