SHARE
TWEET

Untitled

a guest Apr 17th, 2018 148 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. - (void) viewDidLoad
  2. {
  3.     [super viewDidLoad];
  4.  
  5.     UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
  6.     [refreshControl addTarget:self action:@selector(doLoad) forControlEvents:UIControlEventValueChanged];
  7.     self.refreshControl = refreshControl;
  8. }
  9.  
  10. - (void) doLoad
  11. {
  12.     dispatch_async(dispatch_get_global_queue(0, 0), ^{
  13.             // Instead of sleeping, I do a webrequest here.
  14.             [NSThread sleepForTimeInterval: 5];
  15.  
  16.             dispatch_async(dispatch_get_main_queue(), ^{
  17.                 [tableView reloadData];
  18.                 [self.refreshControl endRefreshing];
  19.             });
  20.     });
  21. }
  22.    
  23. - (void)viewDidAppear:(BOOL)animated
  24. {
  25.     [super viewDidAppear:animated];
  26.     [self.refreshControl beginRefreshing];
  27. }
  28.    
  29. - (void) viewWillAppear: (BOOL) animated
  30. {
  31.     [super viewWillAppear: animated];
  32.  
  33.     self.tableView.contentOffset = CGPointMake(0, -self.refreshControl.frame.size.height);
  34.     [self.refreshControl beginRefreshing];    
  35.  
  36.     // kick off your async refresh!
  37.     [self doLoad];
  38. }
  39.    
  40. @interface TSTableViewController : UITableViewController
  41. @end
  42.    
  43. #import "TSTableViewController.h"
  44.  
  45. @implementation TSTableViewController
  46. {
  47.     NSMutableArray*    _dataItems;
  48. }
  49.  
  50. - (void) viewDidLoad
  51. {
  52.     [super viewDidLoad];
  53.  
  54.     self.refreshControl = [UIRefreshControl new];
  55.  
  56.     [self.refreshControl addTarget: self
  57.                             action: @selector( onRefresh: )
  58.                   forControlEvents: UIControlEventValueChanged];
  59. }
  60.  
  61. - (void) viewWillAppear:(BOOL)animated
  62. {
  63.     [super viewWillAppear: animated];
  64.  
  65.     self.tableView.contentOffset = CGPointMake(0, -self.refreshControl.frame.size.height);
  66.  
  67.     [self.refreshControl beginRefreshing];
  68.     [self onRefresh: nil];
  69. }
  70.  
  71. - (void) onRefresh: (id) sender
  72. {
  73.     double delayInSeconds = 2.0;
  74.     dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
  75.     dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
  76.  
  77.         _dataItems = [NSMutableArray new];
  78.  
  79.         for ( int i = 0 ; i < arc4random() % 100 ; i++ )
  80.         {
  81.             CFUUIDRef uuid = CFUUIDCreate( NULL );
  82.  
  83.             [_dataItems addObject: CFBridgingRelease(CFUUIDCreateString( NULL, uuid)) ];
  84.  
  85.             CFRelease( uuid );
  86.         }
  87.  
  88.         [self.refreshControl endRefreshing];
  89.  
  90.         [self.tableView reloadData];
  91.     });
  92. }
  93.  
  94. #pragma mark - Table view data source
  95.  
  96. - (NSInteger) numberOfSectionsInTableView: (UITableView *) tableView
  97. {
  98.     return 1;
  99. }
  100.  
  101. - (NSInteger) tableView:(UITableView *) tableView numberOfRowsInSection: (NSInteger) section
  102. {
  103.     return _dataItems.count;
  104. }
  105.  
  106. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  107. {
  108.     UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault
  109.                                                    reuseIdentifier: nil];
  110.  
  111.     cell.textLabel.text = [_dataItems objectAtIndex: indexPath.row];
  112.  
  113.     return cell;
  114. }
  115.  
  116. @end
  117.    
  118. - (void) viewDidAppear: (BOOL) animated
  119.    {
  120.           [super viewDidAppear: animated];
  121.           UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
  122.           [refreshControl addTarget:self action:@selector(doLoad) forControlEvents:UIControlEventValueChanged];
  123.           self.refreshControl = refreshControl;
  124.           [self.refreshControl beginRefreshing];    
  125.  
  126.    }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top