최근에 사용자가 주문 목록에 항목을 추가 할 수있는 앱을 만들고 버튼을 눌러 팝업 테이블보기를 호출하고 핵심 데이터를 사용하여 저장할 수 있습니다. 자료.UITableView가 데이터를 다시로드하지 않습니다.
앱에 기본 테이블보기가 있으며 슬라이딩 메뉴가있는 Facebook 앱처럼 각 행에 다른 테이블보기가 표시됩니다.
응용 프로그램의 레이아웃은 다음과 같습니다 :
|---------------------------------------------------------------|
| ______________ |
| |Popover Button||
| -------------- |
|---------------------------------------------------------------|
|-----------------------------|First table item 1 |
|Row to call the First table | |
|-----------------------------|---------------------------------|
|Row to call the Second table |First table item 2 |
|-----------------------------| |
|Row to call the Third table |---------------------------------|
|-----------------------------|First table item 3 |
|Row to call the Fourth table | |
|-----------------------------|---------------------------------|
문제는 다음 시나리오에서 발생된다
시작 응용 프로그램.
첫 번째 표보기를 선택하십시오.
일부 항목을 목록에 추가하려면 추가 버튼을 누릅니다.
popover 테이블보기를 보려면 popover 버튼을 누릅니다 (목록이 업데이트되었습니다).
팝업 테이블보기를 닫습니다.
더 많은 항목을 목록에 추가하십시오.
popover 버튼을 누르십시오 (목록은 동일하게 유지되며 테이블보기는 업데이트되지 않았습니다).
다른 테이블보기로 이동 (또는 같은 다시 테이블보기 전화) 다시
눌러 팝 오버 버튼 (목록은 다음 업데이트되었습니다).
누구든지 나를 분류 할 수 있습니까? 감사! ,
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
//load lists
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Dish"];
fetchRequest.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"created" ascending:YES]];
self.lists = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
[self.tableView reloadData];
}
- (NSManagedObjectContext *)managedObjectContext
{
return [(AppDelegate *) [[UIApplication sharedApplication] delegate] managedObjectContext];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[self.managedObjectContext deleteObject:[self.lists objectAtIndex:indexPath.row]];
[self.managedObjectContext save:nil];
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Dish"];
fetchRequest.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"created" ascending:YES]];
self.lists = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
DetailView.m
- (IBAction)popupAddToOrderButtonPressed:(id)sender
{
OrderViewController *orderView = [[OrderViewController alloc] initWithNibName:@"OrderViewController" bundle:nil];
Dish *newList = [NSEntityDescription insertNewObjectForEntityForName:@"Dish" inManagedObjectContext:orderView.managedObjectContext];
newList.created = [NSDate date];
newList.title = self.popupTitle.text;
[orderView.managedObjectContext save:nil];
orderView.lists = [orderView.lists arrayByAddingObject:newList];
}
위의 코드를 디버깅하지 않은 이유는 테이블에 새 데이터가 표시되지 않는 이유를 확인하는 것이지만 어려운 일입니다. 'NSFetchedResultsController' 문서를 확인하십시오. 핵심 데이터 저장소의 데이터를 표시하는 'UITableView'의 데이터 소스로 설계되었으며이 구현을 훨씬 단순하게 만듭니다. – XJones