列表下拉/上拉刷新:(三)强制刷新

来源:互联网 发布:有哪些是人工智能 编辑:程序博客网 时间:2024/06/06 04:25

一进入列表界面总不能是空的,这时候就要异步获取数据,也就是进行第一次刷新。因此要在这个并没有下拉拖动的时候就强制显示刷新header。

在基类中增加强制刷新接口:

[cpp] view plaincopy
  1. // force to refresh  
  2. -(void)showRefreshHeader:(BOOL)animated;  

实现:

[cpp] view plaincopy
  1. -(void)showRefreshHeader:(BOOL)animated{  
  2.     if (animated)  
  3.     {  
  4.         [UIView beginAnimations:nil context:NULL];  
  5.         [UIView setAnimationDuration:0.2];  
  6.         self.tableView.contentInset = UIEdgeInsetsMake(60.0f, 0.0f, 0.0f, 0.0f);// set the Inset  
  7.         // scroll the table view to the new top region  
  8.         [self.tableView scrollRectToVisible:CGRectMake(0, 0.0f, 1, 1) animated:NO];   
  9.         [UIView commitAnimations];  
  10.     }  
  11.     else  
  12.     {  
  13.         self.tableView.contentInset = UIEdgeInsetsMake(60.0f, 0.0f, 0.0f, 0.0f);  
  14.         [self.tableView scrollRectToVisible:CGRectMake(0, 0.0f, 1, 1) animated:NO];  
  15.     }  
  16.     if (_refreshHeaderView) {  
  17.         [_refreshHeaderView showRefreshingHeader:YES];  
  18.     }  
  19.   
  20. }  

关键点在于:1、设置滚动区域;2、将refreshHeaderView设置为loading状态,从而使其显示刷新中的UI效果。

如上,setState函数是私有方法,还是不要直接改成实例方法的好,毕竟只有这一种情况下需要人为设定状态,因此自己写了个showRefreshingHead的方法,在里面设置状态。

这样就可以在一进入列表,还是空的时候,就显示顶部的refreshHeader。实际应用中,将空列表换成一个自定以的loadingView背景图片之类的,UI效果就更好一些。


原创粉丝点击