iOS UITableView(十一) tableView的下拉刷新

来源:互联网 发布:电脑软件助手下载 编辑:程序博客网 时间:2024/05/27 21:50

        如今下拉刷新有常用的几个第三方用着也不错,这里我用系统的UIRefreshControl来给大家介绍一下系统的刷新,注意此控件目前只能用于UITableViewController,大家用的时候注意。第三方择没有限制

下面我给大家展示一下代码(还是用的上次的索引Demo)

//  ViewController.m//  UItableViewIndexes////  Created by SPF on 16/1/6.//  Copyright (c) 2016年 SPF. All rights reserved.//#import "ViewController.h"@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{    UITableView *_tableView;    NSMutableArray *_dataArr;    UIRefreshControl *_refresh;    NSMutableArray *_timeArry;}@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    [self creatData];    _timeArry=[[NSMutableArray alloc]init];    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];    _tableView.delegate=self;    _tableView.dataSource=self;    [self.view addSubview:_tableView];    [self creatRefresh];}-(void)creatRefresh{    _refresh=[[UIRefreshControl alloc]init];    //刷新图形颜色    _refresh.tintColor=[UIColor orangeColor];    //刷新的标题    _refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"下拉刷新"];    // UIRefreshControl 会触发一个UIControlEventValueChanged事件,通过监听这个事件,我们就可以进行类似数据请求的操作了    [_refresh addTarget:self action:@selector(refreshTableviewAction:) forControlEvents:UIControlEventValueChanged];    //UIRefreshControl目前只能用于UITableViewController    self.refreshControl =_refresh;}-(void)refreshTableviewAction:(UIRefreshControl *)refreshs{    if (refreshs.refreshing) {        refreshs.attributedTitle = [[NSAttributedString alloc]initWithString:@"正在刷新"];        [self performSelector:@selector(refershData) withObject:nil afterDelay:2];    }}-(void)refershData{        NSString *syseTiem = nil;    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; //创建的时间格式    syseTiem = [formatter stringFromDate:[NSDate date]];        NSString *lastUpdated = [NSString stringWithFormat:@"上一次更新时间为 %@", [formatter stringFromDate:[NSDate date]]];        self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastUpdated] ;        [_timeArry addObject:syseTiem];    [self.refreshControl endRefreshing];    [_tableView reloadData];        }-(void)creatData{    _dataArr=[[NSMutableArray alloc]init];    for (int i=0; i<26; i++) {       NSMutableArray *arr=[[NSMutableArray alloc]init];                for (int j=0; j<10; j++) {            NSString *str=[NSString stringWithFormat:@"这是第%c组的第%d个标题",'A'+i,j];            [arr addObject:str];        }        [_dataArr addObject:arr];    }}#pragma mark -表格索引//返回右侧索引标题数组//这个标题的内容时和分区标题相对应-(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView{    NSMutableArray *arr=[[NSMutableArray alloc]init];    //创建26个索引标题    //标题尽量和分区相对应    [arr addObject:UITableViewIndexSearch];    for (int i = 0; i < 26; i++) {        NSString *str = [NSString stringWithFormat:@"%c",'A'+i];        [arr addObject:str];    }    return arr;}//设置 右侧索引标题 对应的分区索引- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {    //cell右侧标题    NSLog(@"title:%@",title);    //右侧标题在右侧的索引    NSLog(@"index:%ld",index);        //返回 对应的分区索引    return index-1;}//cell 内容的向右缩进 级别- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath {    return 1;}#pragma mark uitableView代理//返回多少组-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return _dataArr.count;}//每个分区有多少行-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return [_dataArr[section] count];}-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    //创建复用标识符    static NSString *cellID =@"cellID";    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellID];    if (!cell) {        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];    }    //填充cell    cell.textLabel.text=_dataArr[indexPath.section][indexPath.row];    return cell;}//设置头标-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{    return [NSString stringWithFormat:@"这是第%c组",'A'+section];}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end


我是为了演示效果将前面的继承改为下面这样,大家将来写的时候若想用系统的记得只能用于UITableViewController

#import <UIKit/UIKit.h>@interface ViewController : UITableViewController@end
其它的第三方我有时间再为大家介绍

0 0
原创粉丝点击