使用系統自帶下拉刷新
- (void)viewDidLoad {
//** 下拉刷新
UIRefreshControl *rc = [[UIRefreshControl alloc]init];
rc.attributedTitle = [[NSAttributedString alloc]initWithString:@"想看更多"];
[rc addTarget:self action:@selector(refreshTableView) forControlEvents:(UIControlEventValueChanged)];
self.refreshControl = rc;
}
- (void)refreshTableView
{
if (self.refreshControl.refreshing) {
self.refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"加載完成"];
dispatch_sync(dispatch_get_global_queue(0, 0), ^{
self.shopArray = [NSMutableArray array];//對數據容器初始化
這裏實現新的網絡請求
[self requestwithHttpArg];
[self.refreshControl endRefreshing];
});
}
}
容易崩的地方
因爲對數據容器進行了初始化,或者刪除了數據容器,所以再未完成視圖更新時,
在以下方法中:
<pre name="code" class="html">- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
如果用到了數組容器便會崩在數組越界修改的方法是在每個用到數組容器的地方都要進行安全判斷,需要形成一種習慣 if (_shopArray.count>= indexPath.section) { cell.model = _shopArray[indexPath.section]; }