0

have got my app doing the motion of refreshing using ODRefreshcontrol : https://github.com/Sephiroth87/ODRefreshControl How to actually make this refresh my Table view?, heres the code from the .m file:

#import "ThirdViewController.h"
#import "ODRefreshControl.h"

@interface ThirdViewController ()

@end




@implementation ThirdViewController

@synthesize tableView = _tableView;

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (BOOL)shouldAutorotate
{
return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}

-(void)TableView:(UITableView *)TableView didFailLoadWithError:(NSError *)error {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Can't    connect. Please check your internet Connection" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];

}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{ 
[super viewDidLoad];
ODRefreshControl *refreshControl = [[ODRefreshControl alloc] initInScrollView:self.tableView];
[refreshControl addTarget:self action:@selector(dropViewDidBeginRefreshing:) forControlEvents:UIControlEventValueChanged];

// Do any additional setup after loading the view.
[self fetchTweets];
self.tableView.dataSource = self;
self.tableView.delegate = self;
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
[tempImageView setFrame:self.tableView.frame];

self.tableView.backgroundView = tempImageView;



}

- (void)dropViewDidBeginRefreshing:(ODRefreshControl *)refreshControl
{
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    [refreshControl endRefreshing];
});
}



- (void)fetchTweets
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSData* data = [NSData dataWithContentsOfURL:
                    [NSURL URLWithString: @"http://search.twitter.com/search.json?q=from:user"]];

    NSError* error;

    tweets = [NSJSONSerialization JSONObjectWithData:data
                                             options:kNilOptions
                                               error:&error];

    NSLog(@"Tweets %@", tweets);

    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
    });
});
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return tweets.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"TweetCell";

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

NSArray *tweetsArray = [tweets valueForKey:@"results"];
NSDictionary *tweet = [tweetsArray objectAtIndex:indexPath.row];


NSString *text = [tweet objectForKey:@"text"];
//NSString *name = [[tweet objectForKey:@"user"] objectForKey:@"name"];

cell.textLabel.text = text;
//cell.detailTextLabel.text = [NSString stringWithFormat:@"by %@", name];

return cell;

}

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

NSLog(@"Row %d selected", indexPath.row);
}


- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

what do i need to add to make the twitter feed in my table view refresh?

Michael Celey
  • 12,645
  • 6
  • 57
  • 62
rexr
  • 89
  • 11

1 Answers1

1

You need to call fetchTweets method while refreshing

- (void)dropViewDidBeginRefreshing:(ODRefreshControl *)refreshControl
{
    double delayInSeconds = 2.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

        //ADD
        [self fetchTweets];

        [refreshControl endRefreshing];
});
}
Saravana Vijay
  • 572
  • 3
  • 11
  • thanks a lot but i had just managed to figure that out literally 2 minutes before your reply. Thanks though!! – rexr Feb 13 '13 at 14:19
  • 1 more thing i can't figure out how to change the colour of the loading icon that comes up to white, I have a dark background so it cant be seen? – rexr Feb 13 '13 at 14:34
  • Change the activity indicator style to **UIActivityIndicatorViewStyleWhite** in ODRefreshControl.m `_activity = activity ? activity : [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];` – Saravana Vijay Feb 13 '13 at 15:30
  • not sure how to put that in please view this: http://stackoverflow.com/questions/14857447/how-to-change-colour-of-loading-icon thanks :) – rexr Feb 13 '13 at 15:51