11-24-2013 : 좀 더 디버깅을했는데 removeProject가 잘 작동한다는 것을 알게되었습니다. (제거 전후의 모든 프로젝트를 인쇄했습니다)-(NSInteger) tableView : (UITableView *) tableView numberOfRowsInSection : (NSInteger) 섹션으로 돌아올 때만 카운트는 (total-1) 대신 0입니다.
============
나는 잠시 동안이 오류에 갇혀 있습니다. Core Data를 더 잘 이해하기 위해 고객과 그의 프로젝트를 입력 할 수있는이 테스트 프로젝트를 만들었습니다. 프로젝트는 클라이언트와 관련이 있습니다. 프로젝트의 경우 클라이언트의 ViewController를 복사하고 약간의 변경을 수행했습니다. 여러 클라이언트를 입력 한 후 삭제할 수 있습니다. 관련 프로젝트를 삭제하고 싶을 때 문제가 시작됩니다. 프로젝트가 하나 뿐인 클라이언트가있는 경우 오류없이 삭제할 수 있습니다. 클라이언트에 두 개 이상의 프로젝트가있는 경우이 클라이언트에서 프로젝트를 삭제할 수 없습니다. 삭제하면이 오류가 발생합니다.
2013-10-30 10:00:23.145 [6160:70b] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2903.2/UITableView.m:1330
2013-10-30 10:00:23.147 [6160:70b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (0) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
*** First throw call stack:
(
.....
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Program ended with exit code: 0
이 사이트에서 많은 답변을 읽은 후 numberOfRowsInSection 메서드에서 코드를 변경해야한다고 생각하지만 변경해야 할 내용이 표시되지 않습니다.
내 코드 :
Datastore를 만들었습니다.
DataStore.m
#import "BITDataStore.h"
#import "Client.h"
#import "Project.h"
@implementation DataStore
+ (BITDataStore *)sharedStore
{
static BITDataStore *sharedStore = nil;
if (!sharedStore)
sharedStore = [[super allocWithZone:nil]init];
return sharedStore;
}
-(void)removeClient:(Client *)client
{
// remove from NSManagedObjectContext
[context deleteObject:client];
// remove from allClients array
[allClients removeObjectIdenticalTo:client];
NSLog(@"remove client");
}
-(void)removeProject:(Project *)project
{
// remove from NSManagedObjectContext
[context deleteObject:project];
// remove from allProjects array
[allProjects removeObjectIdenticalTo:project];
// remove from relatedProjects array
[relatedProjects removeObjectIdenticalTo:project];
NSLog(@"remove project %@", [project project]);
}
-(NSArray *)relatedProjects:(Client *)client;
{
NSFetchRequest *request = [[NSFetchRequest alloc]init];
NSEntityDescription *e = [[model entitiesByName] objectForKey:@"Project"];
[request setEntity:e];
// Check if client is related to Project
[request setPredicate: [NSPredicate predicateWithFormat:@"clients == %@", client.objectID]];
NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"project"
ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sd]];
NSError *error;
NSArray *result = [context executeFetchRequest:request error:&error];
if (!result) {
[NSException raise:@"Fetch failed"
format:@"Reason: %@", [error localizedDescription]];
}
relatedProjects = [[NSMutableArray alloc] initWithArray:result];
return relatedProjects;
}
@end
ProjectDataViewController.m
@synthesize client, project;
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[self tableView] reloadData];
}
#pragma mark - Table view data source
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[[BITDataStore sharedStore]relatedProjects:client]count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil){
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
Project *p = [[[BITDataStore sharedStore]relatedProjects:client]
objectAtIndex:[indexPath row]];
[[cell textLabel] setText:[p project]];
return cell;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
BITDataStore *ds = [BITDataStore sharedStore];
NSArray *selectedProjects = [ds relatedProjects:client];
Project *pr = [selectedProjects objectAtIndex:[indexPath row]];
NSLog(@"Deleting project %@", [pr project]);
[ds removeProject:pr];
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
// Finally, reload data in view
[[self tableView] reloadData];
NSLog(@"Reload data");
}
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
}
}
@end
정보가 충분하기를 바랍니다. 그렇지 않은 경우 알려 주시기 바랍니다.
내 삭제 규칙을 계단식에서 무효화로 변경했고 작동했습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다