2015-11-25 18 views
2

在我的tableviewcontroller與2部分,構建工程,但應用程序崩潰。當我打開詳細視圖控制器向數據存儲添加一個項目時,開始崩潰,因此一個部分將有一個項目,另一個部分爲零項(因爲生成這些部分的兩個不同謂詞)。我的iOS fetchrequest有兩個部分的奇怪行爲

如果我只設置一個部分,構建應用程序,存儲兩個不同的項目,每個部分一個,一切正常,每個部分的項目數相同。當我再次設置兩個部分時,重新構建應用程序,運行並創建一個新項目,這些部分的編號是不同的:代碼將保存項目,通過返回到tableview將會使應用程序崩潰。爲什麼是這樣?

#import "hommedicineTableViewController.h" 
#import "cella2.h" 

@interface hommedicineTableViewController() 

@end 

@implementation hommedicineTableViewController 
@synthesize section2Items = _section2Items; 
static NSString *CellIdentifier = @"cell3"; 

- (NSManagedObjectContext *)managedObjectContext { 
    NSManagedObjectContext *context = nil; 
    id delegate = [[UIApplication sharedApplication] delegate]; 
    if ([delegate performSelector:@selector(managedObjectContext)]) { 
     context = [delegate managedObjectContext]; 
    } 
    return context; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.view.backgroundColor = [UIColor colorWithRed:(220/255.f) green:(237/255.f) blue:(231/255.f) alpha:1.0]; 
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([cella2 class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([cella2 class])]; 
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([cella2 class]) bundle:nil] forCellReuseIdentifier:CellIdentifier]; 
} 

- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext]; 
    NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"archivio like%@", @"in*"]; 
    NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"archivio like%@", @"ar*"]; 
    NSFetchRequest *fetchRequest1 = [[NSFetchRequest alloc] initWithEntityName:@"Medicine"]; 
    NSFetchRequest *fetchRequest2 = [[NSFetchRequest alloc] initWithEntityName:@"Medicine"]; 
    NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"archivio" ascending:NO]; 
    NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"scadenza" ascending:YES]; 
    [fetchRequest1 setSortDescriptors:[NSArray arrayWithObjects: sortDescriptor1, sortDescriptor2, nil]]; 
    NSSortDescriptor *sortDescriptor3 = [[NSSortDescriptor alloc] initWithKey:@"archivio" ascending:YES]; 
    [fetchRequest2 setSortDescriptors:[NSArray arrayWithObjects: sortDescriptor3, sortDescriptor2, nil]]; 
    [fetchRequest1 setPredicate:predicate1]; 
    [fetchRequest2 setPredicate:predicate2]; 
    self.contactarray = [[managedObjectContext executeFetchRequest:fetchRequest1 error:nil] mutableCopy]; 
    _section2Items = [[managedObjectContext executeFetchRequest:fetchRequest2 error:nil] mutableCopy]; 
    [self.tableView reloadData]; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 2; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (section == 0) { 
      return [self.contactarray count]; 
    } else if (section == 1) { 
      return [_section2Items count]; 
    } else { 
    return 0; 
    } 
} 

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath { 
    cella2 *tcell = (cella2 *)cell; 
    NSManagedObject *device1 = [self.contactarray objectAtIndex:indexPath.row]; 
    NSManagedObject *device2 = [_section2Items objectAtIndex:indexPath.row]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"dd-MM-YYYY"]; 
    NSString *dateString = [dateFormatter stringFromDate:[device1 valueForKey:@"scadenza"]]; 
    if (indexPath.section == 0) { 
           tcell.immagine1.image = [[UIImage alloc] initWithData:[device1 valueForKey:@"picture"]]; 
           tcell.label1.text = [NSString stringWithFormat:@"%@", [device1 valueForKey:@"nominativo"]]; 
           tcell.label2.text = [NSString stringWithFormat:@"Scadenza: %@", dateString]; 
           tcell.label3.text = [NSString stringWithFormat:@"%@", [device1 valueForKey:@"archivio"]]; 
    } else { 
           tcell.label2.textColor = [UIColor colorWithRed:0.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:1.0f]; 
           tcell.immagine1.image = [[UIImage alloc] initWithData:[device2 valueForKey:@"picture"]]; 
           tcell.label1.text = [NSString stringWithFormat:@"%@", [device2 valueForKey:@"nominativo"]]; 
           tcell.label2.text = [NSString stringWithFormat:@"Scadenza: %@", dateString]; 
           tcell.label3.text = [NSString stringWithFormat:@"%@", [device2 valueForKey:@"archivio"]]; 
    } 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
     cella2 *cell = (cella2 *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     [self configureCell:cell atIndexPath:indexPath]; 
     return cell; 
} 

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
    return YES; 
} 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSManagedObjectContext *context = [self managedObjectContext]; 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [context deleteObject:[self.contactarray objectAtIndex:indexPath.row]]; 
     NSError *error = nil; 
     if (![context save:&error]) { 
      NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]); 
      return; 
     } 
     [self.contactarray removeObjectAtIndex:indexPath.row]; 
     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    [self performSegueWithIdentifier:@"seg2" sender:self]; 
} 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if ([[segue identifier] isEqualToString:@"seg2"]) { 
     NSManagedObject *selectedDevice = [self.contactarray objectAtIndex:[[self.tableView indexPathForSelectedRow] row]]; 
     DettaglioViewController *destViewController = segue.destinationViewController; 
     destViewController.contactdb = selectedDevice; 
    } 
} 

@end 
+2

應用程序崩潰時收到的錯誤是什麼? – hdost

+0

修改核心數據後,如何觸發表重裝? – Avi

+0

您是否嘗試向項目添加異常斷點(斷點選項卡/ + /添加異常斷點)? – turingtested

回答

0

我懷疑問題出在你configureCell:atIndexPath:方法,特別是這兩行:

NSManagedObject *device1 = [self.contactarray objectAtIndex:indexPath.row]; 
NSManagedObject *device2 = [_section2Items objectAtIndex:indexPath.row]; 

如果一款具有比其他更多的項目,這些線路之一將試圖訪問數組索引越界(即indexPath.row將大於或等於數組數)。只要將這些線回落的有關條款中的if/else語句:

if (indexPath.section == 0) { 
    NSManagedObject *device1 = [self.contactarray objectAtIndex:indexPath.row]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"dd-MM-YYYY"]; 
    NSString *dateString = [dateFormatter stringFromDate:[device1 valueForKey:@"scadenza"]]; 
    tcell.immagine1.image = [[UIImage alloc] initWithData:[device1 valueForKey:@"picture"]]; 
    tcell.label1.text = [NSString stringWithFormat:@"%@", [device1 valueForKey:@"nominativo"]]; 
    tcell.label2.text = [NSString stringWithFormat:@"Scadenza: %@", dateString]; 
    tcell.label3.text = [NSString stringWithFormat:@"%@", [device1 valueForKey:@"archivio"]]; 
} else { 
    NSManagedObject *device2 = [_section2Items objectAtIndex:indexPath.row]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"dd-MM-YYYY"]; 
    NSString *dateString = [dateFormatter stringFromDate:[device2 valueForKey:@"scadenza"]]; 
    tcell.label2.textColor = [UIColor colorWithRed:0.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:1.0f]; 
    tcell.immagine1.image = [[UIImage alloc] initWithData:[device2 valueForKey:@"picture"]]; 
    tcell.label1.text = [NSString stringWithFormat:@"%@", [device2 valueForKey:@"nominativo"]]; 
    tcell.label2.text = [NSString stringWithFormat:@"Scadenza: %@", dateString]; 
    tcell.label3.text = [NSString stringWithFormat:@"%@", [device2 valueForKey:@"archivio"]]; 
} 

有編碼它的整潔的方式,但我希望你的想法。

+0

謝謝pbasdf,它運行! – whateveryouwant