简体   繁体   中英

Refresh TableView Content with UIRefreshControl

Please I would like to refresh the contents of my table but it does not work.

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Last News";
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    //
    NSURL *url = [NSURL URLWithString:@"http://www.apinae.com/json.php"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];

    // test refresh
    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    refreshControl.tintColor = [UIColormagentaColor];
    self.refreshControl = refreshControl;
    // test refresh

}

Thanks in Advance.

You didn't set the action for the refresh control. Try this instead:

-(void) viewDidLoad
{
    [super viewDidLoad];
    UIRefreshControl *refreshControl = [UIRefreshControl new];
    [refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
    refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to refresh..."];
    self.refreshControl = refreshControl;
}

- (void)refresh:(UIRefreshControl *)sender 
{
    // ... your refresh code
    NSURL *url = [NSURL URLWithString:@"http://www.apinae.com/json.php"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];

    [sender endRefreshing];
}

Well, the call to -endRefreshing should be in the delegate method so it stops refreshing when the file is downloaded and refreshed in the table, but you get the idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM