使用UIRefreshControl刷新TableView内容

时间:2013-05-19 13:03:40

标签: ios xcode tableview uirefreshcontrol

请我刷新我的表格的内容,但它不起作用。

- (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

}

先谢谢。

1 个答案:

答案 0 :(得分:3)

您没有为刷新控件设置操作。试试这个:

-(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];
}

好吧,对-endRefreshing的调用应该在委托方法中,因此当在表中下载和刷新文件时它会停止刷新,但是你明白了。