使用 UIRefreshControl 刷新 TableView 内容

Posted

技术标签:

【中文标题】使用 UIRefreshControl 刷新 TableView 内容【英文标题】:Refresh TableView Content with UIRefreshControl 【发布时间】:2013-05-19 13:03:40 【问题描述】:

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

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

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

-(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 的调用应该在委托方法中,因此当文件下载并在表中刷新时它会停止刷新,但你明白了。

【讨论】:

我在 self.refreshControl = refreshControl; 对不起,我英语不好 我理解你很好。您应该在@interface 中添加@property (nonatomic,strong) UIRefreshControl *refreshControl; 我的代码中没有警告,所以在我的控制台上 2013-05-19 20:21:33.315 JSONNews[6219:f803] -[UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x6e56010 2013 -05-19 20:21:33.317 JSONNews[6219:f803] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UIView tableView:numberOfRowsInSection:]:无法识别的选择器发送到实例 0x6e56010” ***第一掷调用堆栈:(0x14b5022 0xeb5cd6 0x14b6cbd 0x141bed0 0x141bcb2 0x1f606b 0x1f8862 0xa666d 0xa6167 0xa9134 0xad623 0x56d42 0x14b6e42 0x1d86679 0x1d90579 0x1d154f7 0x1d173f6 0x1da4160 0x16e84 0x17767 0x26183 0x26c38 0x1a634 0x139fef5 0x1489195 0x13edff2 0x13ec8da 0x13ebd84 0x13ebc9b 0x16c65 0x18626 0x2b1d 0x2a45 0x1) 终止调用抛出异常(lldb)

以上是关于使用 UIRefreshControl 刷新 TableView 内容的主要内容,如果未能解决你的问题,请参考以下文章

使用 UIRefreshControl 刷新 TableView 内容

根据当前 URL 使用 UIRefreshControl 刷新

用于“上拉”刷新的 UIRefreshControl?

UIRefreshControl 刷新后不隐藏。 iOS

在uitableview中使用UIRefreshControl

使用UIRefreshControl刷新表视图