因為用 CocoaPods 一陣子,馬上就用關鍵字搜尋一下,發現有幾款不錯,但總是覺得差那麼一點,例如超過一年沒更新,當年熱門的甚至四年沒更新了 Orz 想了一會兒,才驚醒是不是變成內建了 XD 才想到之前去年底複習 CS193P 時,就看到教學過,那時是使用 Storyboard 的用法,隨意勾一勾就有的東西。
總之,回到主題,純寫 code 的方式,以 UITableViewController 而已,就是配置 self.refreshControl 就對了!
- (void)refreshData
{
self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Loading..."];
dispatch_async(dispatch_queue_create("loading...", NULL), ^{
[NSThread sleepForTimeInterval:5];
dispatch_async(dispatch_get_main_queue(), ^{
[self.refreshControl endRefreshing];
self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to refresh"];
});
});
}
- (void)viewWillAppear:(BOOL)animated
{
if (!self.refreshControl)
self.refreshControl = [[UIRefreshControl alloc] init];
if (!self.refreshControl.isRefreshing) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView setContentOffset:CGPointMake(0, -self.refreshControl.frame.size.height - self.navigationController.navigationBar.frame.size.height) animated:YES];
[self.refreshControl beginRefreshing];
[self refreshData];
});
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
if (!self.refreshControl)
self.refreshControl = [[UIRefreshControl alloc] init];
if (self.refreshControl) {
self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to refresh"];
[self.refreshControl addTarget:self action:@selector(refreshData) forControlEvents:UIControlEventValueChanged];
}
}
雖然很簡單,還是筆記一下 Orz
沒有留言:
張貼留言