2013-11-09 2 views
0

지난 18 개월 동안 CoreData에서 데이터 테이블을 성공적으로로드 한 간단한보기가 있습니다. IOS 7으로 업그레이드 된 장치에서도 작동합니다.하지만 Xcode5로 업그레이드하고 IOS7 3.5inch 망막 시뮬레이터를 실행하면 테이블이 항상 비어 있습니다. 나는 아래 코드를 붙여 넣었고 NSLog 출력에서 ​​이것을 볼 수 있기 때문에 fetchRequests가 데이터를 리턴하고 있음을 확인할 수있다. 그러나 왜 세포를 채우는 테이블 정지가 있습니까? 난 그냥이 일을 알아낼 수 없기 때문에UITableView가 IOS7 및 Xcode5에서 데이터로드를 중지했습니다.

나는

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
     NSLog(@"listData count in numberOFRowsInSection is: %i", listData.count); 
     return [self.listData count]; 
    } 

    - (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath { 
     Sessions *info = [_fetchedResultsController objectAtIndexPath:indexPath]; 
     NSLog(@"info content is: %@", info.sport); 
     //Format cell data ready to be displayed 
     NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
     [dateFormat setDateFormat:@"EE, dd LLL yyyy"]; 
     NSString *dateString = [dateFormat stringFromDate:info.date]; 

     NSNumber *dist1Nbr = info.dist1; 
     int dist1Int = [dist1Nbr integerValue]; 
     float distIntKorM = ([dist1Nbr integerValue])/1000; 
     NSString *dist1StrMeters = [[NSString alloc] initWithFormat:@"%i", dist1Int]; 
     NSString *dist1StrKorM = [[NSString alloc] initWithFormat:@"%.01f", distIntKorM]; 


     //Select image to display 
     if ([info.sport isEqualToString:@"Run"]) { 
      UIImage *image = [UIImage imageNamed:@"trainers-15x10.png"]; 
      cell.imageView.image = image; 
      cell.textLabel.text = [[NSString alloc] initWithFormat:@"%@: (%@),", dateString, info.sport]; 
      cell.detailTextLabel.text = [[NSString alloc] initWithFormat:@"Type: %@, Dist: %@", info.sessiontype, dist1StrKorM]; 
      NSLog(@"Cell text for Runs shoudl be: %@", cell.textLabel.text); 
     } else if ([info.sport isEqualToString:@"Other"]) { 
      UIImage *image = [UIImage imageNamed:@"weights-15x10.png"]; 
      cell.imageView.image = image; 
      cell.textLabel.text = [[NSString alloc] initWithFormat:@"%@: (%@),", dateString, info.sport]; 
      cell.detailTextLabel.text = [[NSString alloc] initWithFormat:@"Type: %@, Dist: %@", info.sessiontype, dist1StrKorM]; 
     } else if ([info.sport isEqualToString:@"Swim"]) { 
      UIImage *image = [UIImage imageNamed:@"goggles-15x10.png"]; 
      cell.imageView.image = image; 
      cell.textLabel.text = [[NSString alloc] initWithFormat:@"%@: (%@),", dateString, info.sport]; 
      cell.detailTextLabel.text = [[NSString alloc] initWithFormat:@"Type: %@, Dist: %@m", info.sessiontype, dist1StrMeters]; 
      NSLog(@"Cell text for Swims shoudl be: %@", cell.textLabel.text); 
     } else if ([info.sport isEqualToString:@"Cycle"]) { 
      UIImage *image = [UIImage imageNamed:@"bike-15x10.png"]; 
      cell.imageView.image = image; 
      cell.textLabel.text = [[NSString alloc] initWithFormat:@"%@: (%@),", dateString, info.sport]; 
      cell.detailTextLabel.text = [[NSString alloc] initWithFormat:@"Type: %@, Dist: %@", info.sessiontype, dist1StrKorM]; 
     } else if ([info.sport isEqualToString:@"Brick"]) { 
      UIImage *image = [UIImage imageNamed:@"brick-15x10.png"]; 
      cell.imageView.image = image; 
      cell.textLabel.text = [[NSString alloc] initWithFormat:@"%@: (%@),", dateString, info.sport]; 
      cell.detailTextLabel.text = [[NSString alloc] initWithFormat:@"Type: %@, Dist: %@", info.sessiontype, dist1StrKorM]; 
     } 

    } 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

     static NSString *CellIdentifier = @"editSession"; 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 

      } 
      // this cell backgrond colour alternating works! 
      UIView *bgColor = [cell viewWithTag:100]; 
      if (!bgColor) { 
       CGRect frame = CGRectMake(0, 0, 320, 50); 
       bgColor = [[UIView alloc] initWithFrame:frame]; 
       bgColor.tag = 100; 
       [cell addSubview:bgColor]; 
       [cell sendSubviewToBack:bgColor]; 
      } 

      if (indexPath.row % 2 == 0) { 
       bgColor.backgroundColor = [UIColor colorWithRed:233.0/255.0 green:233.0/255.0 blue:233.0/255.0 alpha:1.0]; 
        } else { 
         bgColor.backgroundColor = [UIColor clearColor];        } 

     [self configureCell:cell atIndexPath:indexPath]; 

     return cell; 
    } 


    - (id)initWithStyle:(UITableViewStyle)style 
    { 
     self = [super initWithStyle:style]; 
     if (self) { 
      // Custom initialization 
     } 
     return self; 
    } 


    - (void)didReceiveMemoryWarning 
    { 
     // Releases the view if it doesn't have a superview. 
     [super didReceiveMemoryWarning]; 

     // Release any cached data, images, etc that aren't in use. 
    } 

    #pragma mark - View lifecycle 

    - (void)viewDidLoad 
    { 
     [super viewDidLoad]; 

     // Uncomment the following line to preserve selection between presentations. 
     // self.clearsSelectionOnViewWillAppear = NO; 

     // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
     // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
     if (_context == nil) 
     { 
      _context = [(SGK_T4T_01AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
      NSLog(@"After managedObjectContext: %@", _context); 
     } 

     NSError *error; 
     if (![[self fetchedResultsController] performFetch:&error]) { 
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
      exit(-1); 
     } else { 
      NSLog(@"After fetchedResultsController: %@", _fetchedResultsController); 
      //NSLog(@"After managedObjectContext: %@", _fetchedResultsController); 
     } 
     self.title = @"Sessions"; 


    } 

    - (void)viewDidUnload 
    { 
     [super viewDidUnload]; 
     // Release any retained subviews of the main view. 
     // e.g. self.myOutlet = nil; 

     [self setListData:nil]; 
     [self setSelectedSession:nil]; 
     [self setSessionSport:nil]; 
     //[self setRecordCount:nil]; 
     [self setFetchedResultsController:nil]; 
     [self setContext:nil]; 
    } 

    - (void)viewWillAppear:(BOOL)animated 
    { 
     [super viewWillAppear:animated]; 

     SGK_T4T_01AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
     NSManagedObjectContext *context = [appDelegate managedObjectContext]; 
     NSEntityDescription *entityDiscription = [NSEntityDescription entityForName:@"Sessions" inManagedObjectContext:context]; 
     //NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"refid" ascending:YES]; 
     NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
     [request setEntity:entityDiscription]; 


     NSError *error; 
     NSArray *objects = [context executeFetchRequest:request error:&error]; 
     if (objects == nil) { 

      NSLog(@"The fetch request returned an array == nil"); 
     } else { 
      NSLog(@"The fetch request returned an array!!!"); 
      NSLog(@"objects contents is: %@", objects); 
      NSLog(@"objects count = %i", [objects count]); 
      listData = objects; 
      NSLog(@"listData count = %i", [listData count]); 
      //NSUInteger *recordCount = [objects count]; 
      recordCount = [objects count]; 
     } 

     //reload tableView:dataSource from CoreData when view reappears... 
     if (_context == nil) 
     { 
      _context = [(SGK_T4T_01AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
      NSLog(@"After managedObjectContext: %@", _context); 
     } 

     NSError *error1; 
     if (![[self fetchedResultsController] performFetch:&error1]) { 
      NSLog(@"Unresolved error %@, %@", error1, [error1 userInfo]); 
      exit(-1); 
     } else { 
      NSLog(@"viewWillAppear: fetchedResultsController: %@", _fetchedResultsController); 
     } 
     //end of reload tableView:dataSource from CoreData when view reappears... 

     [self.tableView reloadData]; 
    } 

    - (void)viewDidAppear:(BOOL)animated 
    { 
     [super viewDidAppear:animated]; 

    } 

    - (void)viewWillDisappear:(BOOL)animated 
    { 
     [super viewWillDisappear:animated]; 
     //[self setFetchedResultsController:nil]; 
    } 

    - (void)viewDidDisappear:(BOOL)animated 
    { 
     [super viewDidDisappear:animated]; 
     //[self setFetchedResultsController:nil]; 
    } 

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
    { 
     // Return YES for supported orientations 
     return (interfaceOrientation == UIInterfaceOrientationPortrait); 
    } 

    #pragma mark - Table view data source 

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    { 
    //#warning Potentially incomplete method implementation. 
     // Return the number of sections. 
     return 1; 
    } 

    /* 
    // Override to support conditional editing of the table view. 
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     // Return NO if you do not want the specified item to be editable. 
     return YES; 
    } 
    */ 


    // Override to support editing the table view. 
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     if (editingStyle == UITableViewCellEditingStyleDelete) { 
      // Delete the row from the data source 
      [_context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]]; 

      NSError *error = nil; 
      if (![_context save:&error]) { 
       NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
       abort(); 
       } 
      } 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 
     } 
     [self.tableView reloadData]; 
    } 

    - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath 
    { 
     if (type == NSFetchedResultsChangeDelete) { 
      [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     } 
    } 

    #pragma mark - Table view delegate 

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    { 

    } 

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
     NSIndexPath *indexPath = [self.tableView indexPathForCell:sender]; 
     NSUInteger index = indexPath.row; 
     //NSLog(@" Prep4Seg indexPath.row = %u", index); 
     //NSLog(@" Prep4Seg recordCount = %u", recordCount); 
     /* 
      Subtract the row index from the row count to get the correct position in the Array (because I am sorting on date order so tableCell position 0 is position 2 in the Array (if there are 3 only items in the table and array) so if you don't invert the indexPath you end up passing the last item instead of the first or second last item instead of the second and so on... 
     */ 
     NSUInteger arrayIndex = (recordCount-index-1); 
     //NSLog(@" Prep4Seg arrayIndex = %u", arrayIndex); 
     selectedSession = [listData objectAtIndex:arrayIndex]; 
     //NSLog(@"listData = %@", listData); 
     //NSLog(@"SelectedSession = %@", selectedSession); 

     NSNumber *refId = [selectedSession valueForKey:@"refid"]; 
     NSString *refIdToSend = [[NSString alloc] initWithFormat:@"%@", refId]; 
     NSLog(@"Prep4Seg in tableView: refIdToSend = %@", refIdToSend); 

     if ([segue.identifier isEqualToString:@"editSession"]) { 
      SGK_T4T_EditSessionDetail *editSessionDetail = segue.destinationViewController; 
      editSessionDetail.delegate = (id)self; 
      editSessionDetail.returnFromDatePickerView = [[NSString alloc] initWithFormat:@"no"]; 
      editSessionDetail.recedIndex = refIdToSend; 
     } 
    } 

    - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { 
     // The fetch controller is about to start sending change notifications, so prepare the table view for updates. 
     [self.tableView beginUpdates]; 
    } 


    - (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type { 

     switch(type) { 

      case NSFetchedResultsChangeInsert: 
       [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
       break; 

      case NSFetchedResultsChangeDelete: 
       [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
       break; 
     } 
    } 


    - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { 
     // The fetch controller has sent all current change notifications, so tell the table view to process all updates. 
     [self.tableView endUpdates]; 
    } 

... 정말 바보가 된 기분 그리고 내 로그 출력은 다음과 같습니다

2013-11-09 16:04:04.034 trainForTri copy[6509:a0b] After managedObjectContext: <NSManagedObjectContext: 0xb589bc0> 
2013-11-09 16:04:04.036 trainForTri copy[6509:a0b] After fetchedResultsController: <NSFetchedResultsController: 0xb5e2c40> 
2013-11-09 16:04:04.040 trainForTri copy[6509:a0b] The fetch request returned an array!!! 
2013-11-09 16:04:04.041 trainForTri copy[6509:a0b] objects contents is: (
    "<NSManagedObject: 0xb5c8570> (entity: Sessions; id: 0xb5ac970 <x-coredata://B93EE0DE-E6FA-491C-9C8F-23692A36DD0C/Sessions/p1> ; data: <fault>)", 
    "<NSManagedObject: 0xb5d6320> (entity: Sessions; id: 0xb58a2a0 <x-coredata://B93EE0DE-E6FA-491C-9C8F-23692A36DD0C/Sessions/p2> ; data: <fault>)", 
    "<NSManagedObject: 0xb5d0370> (entity: Sessions; id: 0xb5dc270 <x-coredata://B93EE0DE-E6FA-491C-9C8F-23692A36DD0C/Sessions/p3> ; data: <fault>)", 
    "<NSManagedObject: 0xb5e3dd0> (entity: Sessions; id: 0xb5ebcc0 <x-coredata://B93EE0DE-E6FA-491C-9C8F-23692A36DD0C/Sessions/p4> ; data: <fault>)", 
    "<NSManagedObject: 0xb585e40> (entity: Sessions; id: 0xb5e4e40 <x-coredata://B93EE0DE-E6FA-491C-9C8F-23692A36DD0C/Sessions/p5> ; data: <fault>)" 
) 
2013-11-09 16:04:04.041 trainForTri copy[6509:a0b] objects count = 5 
2013-11-09 16:04:04.042 trainForTri copy[6509:a0b] listData count = 5 
2013-11-09 16:04:04.043 trainForTri copy[6509:a0b] viewWillAppear: fetchedResultsController: <NSFetchedResultsController: 0xb5a01e0> 
2013-11-09 16:04:04.043 trainForTri copy[6509:a0b] listData count in numberOFRowsInSection is: 5 
+0

'cellForRowAtIndexPath'이 (가) 호출 되었습니까? 또한,'listData.count'를 출력하고'[self.listData count]'를 반환하면 ... 그것들이 같은 것들인지 확신 할 수 있습니까? (속성 대 변수?) –

+1

나는 @PhillipMills가 옳다고 생각합니다. 명시 적으로'listData' ivar를 생성하고'@synthesize listData'를 작성하지 않았으므로, 속성에 대해 암시적인'_listData'를 사용하고 있습니다. 이것은 ** .property'를 항상 사용하고 직접 ivars **를 피해야하는 또 하나의 이유입니다. – Kevin

+0

@ 필립 밀스 및 귀하의 의견에 감사드립니다. synthesize listData를 수행하고 {return [listData count];}로 변경하더라도 아무런 차이가 없습니다. 그리고 cellForRowAtIndexPath에 NSLog를 배치하여 테스트했지만 출력을 얻지 못했습니다. 그래서 호출되지 않았다고 추측합니다.하지만 항상 Xcode5 이전에했던 이유는 무엇입니까? 내가 더 이상 쓸모 없기 때문에 더 이상 생각할 필요가 없습니다! – Sean

답변

0

확인이 함께 문제를 발견했다. 결국 Xcode5에 대한 좌절감으로 내 Macbook을 창 밖으로 던지기를 원했지만 모든 것을 종료하고 다시 시작했습니다. Xcode를 열었을 때 내 애플리케이션을 빌드하고 실행하여 빈 테이블을로드하는 시간이 길어졌습니다. 이제 NSFetchedResultsController에 문제가 있음을 나타내는 오류가 발생했습니다. 나는 캐시를 지우지 않는 (또는 변경할 수없는) 문제를 추적했다. 그래서 캐시 이름을 지우고 그것을 nil로 설정했다. 모든 것이 다시 작동한다. 이 IOS7/Xcode5 문제가 있는지 여부를 모르지만이 업그레이드까지 문제가되지 않았습니다. 비슷한 수정 프로그램을 찾고있는 사람은 다음과 같습니다.

NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:_context sectionNameKeyPath:nil cacheName:@"Root"]; 

그리고이 버전이 작동합니다!

NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:_context sectionNameKeyPath:nil cacheName:nil]; 

은 내가 따라서 만이 투쟁하는 어느 하나의 단지 thickie 아니에요 바랍니다!

관련 문제