Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
  2. atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
  3. newIndexPath:(NSIndexPath *)newIndexPath
  4. {
  5. switch(type) {
  6. case NSFetchedResultsChangeInsert:
  7. [_insertions addObject:@(newIndexPath.row)];
  8. break;
  9. }
  10. }
  11.  
  12.  
  13. - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
  14. _insertions = @[].mutableCopy;
  15. }
  16.  
  17. - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
  18. NSMutableArray *copy = _insertions.mutableCopy;
  19.  
  20. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  21.  
  22. NSSortDescriptor *lowestToHighest = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:YES];
  23. [copy sortUsingDescriptors:[NSArray arrayWithObject:lowestToHighest]];
  24.  
  25. dispatch_sync(dispatch_get_main_queue(), ^{
  26.  
  27. for (NSNumber *i in copy) {
  28. [self p_delayedAddRowAtIndex:[i integerValue]];
  29. }
  30. [self.tableView reloadData];
  31. });
  32. });
  33. }
  34.  
  35. - (CGFloat)p_firstRowHeight {
  36. return [self tableView:[self tableView] heightForRowAtIndexPath:[NSIndexPath indexPathForRow:0
  37. inSection:0]];
  38. }
  39.  
  40. -(void)p_delayedAddRowAtIndex:(NSInteger)row {
  41.  
  42. NSIndexPath *firstVisibleIndexPath = [[self.tableView indexPathsForVisibleRows] firstObject];
  43.  
  44. if (firstVisibleIndexPath.row >= row) {
  45.  
  46. CGPoint offset = [[self tableView] contentOffset];
  47. offset.y += [self firstRowHeight];
  48.  
  49. if ([self.tableView visibleCells].count > self.tableView.frame.size.height / [self firstRowHeight]) {
  50. [[self tableView] setContentOffset:offset];
  51. }
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement