Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 15th, 2012  |  syntax: None  |  size: 7.28 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Rows not being inserted into UITable
  2. -(void)sectionHeaderView:(SectionHeaderView*)sectionHeaderView sectionOpened:(NSInteger)section {
  3.  
  4. if (![[sectionHeaderArray objectAtIndex:section] isOpen]) {
  5.  
  6.     [[sectionHeaderArray objectAtIndex:section] setIsOpen:YES];
  7.  
  8.     NSLog(@"self.tableView: %@", self.tableView);
  9.     id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
  10.     NSInteger countOfRowsToInsert = [sectionInfo numberOfObjects];
  11.  
  12.     NSMutableArray *indexPathsToInsert = [[NSMutableArray alloc] init];
  13.     for (NSInteger i = 0; i < countOfRowsToInsert; i++) {
  14.         [indexPathsToInsert addObject:[NSIndexPath indexPathForRow:i inSection:section]];
  15.     }
  16.  
  17.     // Apply the updates.
  18.     [self.tableView beginUpdates];
  19.     NSLog(@"Count of rows to insert: %d", [indexPathsToInsert count]);
  20.     NSLog(@"Rows before insert: %d", [self.tableView numberOfRowsInSection:section]);
  21.     [self.tableView insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:UITableViewRowAnimationTop];
  22.     NSLog(@"Rows after insert: %d", [self.tableView numberOfRowsInSection:section]);
  23.     [self.tableView endUpdates];
  24. }
  25.  
  26. }
  27.  
  28.  
  29. -(void)sectionHeaderView:(SectionHeaderView*)sectionHeaderView sectionClosed:(NSInteger)section {
  30.  
  31. if ([[sectionHeaderArray objectAtIndex:section] isOpen]) {
  32.  
  33.     [[sectionHeaderArray objectAtIndex:section] setIsOpen:NO];
  34.  
  35.     NSInteger countOfRowsToDelete = [self.tableView numberOfRowsInSection:section];
  36.  
  37.     if (countOfRowsToDelete > 0) {
  38.         NSMutableArray *indexPathsToDelete = [[NSMutableArray alloc] init];
  39.         for (NSInteger i = 0; i < countOfRowsToDelete; i++) {
  40.             [indexPathsToDelete addObject:[NSIndexPath indexPathForRow:i inSection:section]];
  41.         }
  42.         [self.tableView beginUpdates];
  43.         NSLog(@"Count of rows to delete: %d", [indexPathsToDelete count]);
  44.         NSLog(@"Rows before delete: %d", [self.tableView numberOfRowsInSection:section]);
  45.         [self.tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:UITableViewRowAnimationTop];
  46.         NSLog(@"Rows after delete: %d", [self.tableView numberOfRowsInSection:section]);
  47.  
  48.     }
  49.  
  50.     [self.tableView endUpdates];
  51. }
  52. }
  53.        
  54. 2012-03-31 13:36:17.454 QuickList7[5523:fb03] Count of rows to insert: 3
  55. 2012-03-31 13:36:17.454 QuickList7[5523:fb03] Rows before insert: 0
  56. 2012-03-31 13:36:17.454 QuickList7[5523:fb03] Rows after insert: 0
  57.        
  58. 2012-03-31 13:48:35.783 QuickList7[5523:fb03] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid table view update.  The application has requested an update to the table view that is inconsistent with the state provided by the data source.'
  59.        
  60. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  61. {
  62.     // number of menus, plus 1 if a menu is open, plus 1 static cell
  63.     return [self.restaurant.menus count]+(self.menu != nil ? 1 : 0)+1;
  64. }
  65.  
  66. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  67. {
  68.     // if this section is our selected menu, return number of items, otherwise return 1
  69.     int numberOfRowsInSection = ([self indexPathIsInMenuItemSection:section] ? [[self.menu items] count] : 1);
  70.     return numberOfRowsInSection;
  71. }
  72.  
  73. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  74. {
  75.     if (indexPath.section == [tableView numberOfSections]-1) {
  76.         // ... set up and return static cell
  77.     }
  78.     if ([self indexPathIsInMenuItemSection:indexPath.section]) {
  79.         // ... set up and return menu item cell
  80.     } else  {
  81.         // ... set up and return menu name cell
  82.     }
  83. }
  84.        
  85. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  86. {
  87.     [tableView deselectRowAtIndexPath:indexPath animated:YES];
  88.     // return if it's a static cell
  89.     if (indexPath.section==[tableView numberOfSections]-1)
  90.         return;
  91.     // if it's a menu name cell, close open menu and maybe expand this menu
  92.     if (![self indexPathIsInMenuItemSection:indexPath.section]) {
  93.         BOOL reset = self.menu == m;
  94.         if (reset) [self reloadTableView:self.tableView withMenu:nil animated:YES autoscroll:NO];
  95.         else [self reloadTableView:self.tableView withMenu:m animated:YES autoscroll:YES];
  96.     }
  97. }
  98.        
  99. - (BOOL)indexPathIsInMenuItemSection:(NSInteger)section
  100. {
  101.     // returns YES if section refers to our MenuItemCells
  102.     int indexOfMenu = [self.restaurant getIndexOfMenu:self.menu];
  103.     return indexOfMenu != -1 && section == indexOfMenu+1;
  104. }
  105.  
  106. - (void)reloadTableView:(UITableView *)tableView withMenu:(Menu *)menu animated:(BOOL)animated autoscroll:(BOOL)autoscroll
  107. {
  108.     int oldIndex = [self.restaurant getIndexOfMenu:self.menu];
  109.     int newIndex = [self.restaurant getIndexOfMenu:menu];
  110.  
  111.     [tableView beginUpdates];
  112.  
  113.     if (oldIndex != -1) {
  114.         // index of [section for items] is oldIndex+1
  115.         [tableView deleteSections:[NSIndexSet indexSetWithIndex:oldIndex+1] withRowAnimation:UITableViewRowAnimationTop];
  116.     }
  117.     if (newIndex != -1) {
  118.         // index for [section for items] is newIndex+1
  119.         [tableView insertSections:[NSIndexSet indexSetWithIndex:newIndex+1] withRowAnimation:UITableViewRowAnimationTop];
  120.         [self setMenu:menu];
  121.     } else {
  122.         // no new menu
  123.         [self setMenu:nil];
  124.     }
  125.  
  126.     [tableView endUpdates];
  127.     if (autoscroll) [self autoscroll];
  128. }
  129.  
  130. - (void)autoscroll
  131. {
  132.     if (self.menu != nil) {
  133.         int section = [self.restaurant getIndexOfMenu:self.menu];
  134.         if (section != -1) {
  135.             NSUInteger indexes[] = {section,0};
  136.             NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:indexes length:2];
  137.             [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
  138.         }
  139.     }
  140. }
  141.        
  142. [self reloadTableView:self.tableView withMenu:self.menu animated:YES autoscroll:YES];
  143.        
  144. - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
  145.    atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
  146.   newIndexPath:(NSIndexPath *)newIndexPath
  147. {
  148. UITableView *tv = self.tView;
  149. switch(type) {
  150.     case NSFetchedResultsChangeInsert:
  151.         if ([[sectionHeaderArray objectAtIndex:newIndexPath.section] isOpen]) {
  152.             [tv insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
  153.         }
  154.         break;
  155.  
  156.     case NSFetchedResultsChangeDelete:
  157.         if ([[sectionHeaderArray objectAtIndex:indexPath.section] isOpen]) {
  158.             [tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
  159.         }
  160.         break;
  161.  
  162.     case NSFetchedResultsChangeUpdate:
  163.         if ([[sectionHeaderArray objectAtIndex:indexPath.section] isOpen]) {
  164.             [self configureCell:[tv cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
  165.         }
  166.         break;
  167.  
  168.     case NSFetchedResultsChangeMove:
  169.         if ([[sectionHeaderArray objectAtIndex:indexPath.section] isOpen]) {
  170.             [tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
  171.         }
  172.         if ([[sectionHeaderArray objectAtIndex:newIndexPath.section] isOpen]) {
  173.             [tv insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];
  174.         }
  175.         break;
  176. }
  177.  
  178. }