現在使用多數的App,若資料是以列表呈現,多會實作列表下拉更新,因為使用體驗非常親民直覺,所以沿用至今,於是我也想實現這功能啦⋯⋯
在此我不想用UITableViewController,想在任何UITableView就可以使用,可以怎麼做呢?其實幾行程式碼就能搞定!
/** Theme: Pull Down to Refresh IDE: Xcode 7 Language: Objective C Date: 105/04/01 Author: HappyMan Blog: https://cg2010studio.wordpress.com/ */ - (void)viewDidLoad { [super viewDidLoad]; UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init]; [refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged]; [displayTableView addSubview:refreshControl]; } - (void)refresh:(UIRefreshControl *)refreshControl { // 實作重新撈資料 [refreshControl endRefreshing]; }
最後我只要在觸發點實作重新撈資料,接著顯示新的資料即可!
那如果往下滑到底,沒有後續資料時,我們還可以繼續撈下一資料片段,並合成資料列表來顯示。
參考:Pull to refresh UITableView without UITableViewController。
隨意留個言吧:)~