UIRefreshControl 下拉刷新

在自定义 Tableview 中
#import "TableViewController.h"
@interface TableViewController ()
{
NSMutableArray *_dataArray;
}
@end
@implementation TableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    _dataArray = [[NSMutableArray alloc] init];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellID"];
    //创建下拉刷新的控件
    //UIRefreshControl  下拉刷新的类
    UIRefreshControl *ref = [[UIRefreshControl alloc] init];
    //NSAttributedString  属性字符创
    NSAttributedString *string = [[NSAttributedString alloc] initWithString:@"下拉刷新"];
    //给下拉刷新的控件设置标题
    ref.attributedTitle = string;
    //绑定事件
    [ref addTarget:self action:@selector(upDate) forControlEvents:UIControlEventValueChanged];
    //把下拉刷新的控件添加到表上
    self.refreshControl = ref;
    [ref release];
    [string release];
}

-(void)upDate
{
    //当正在刷新时,把标题改为刷新中
    NSAttributedString *string = [[NSAttributedString alloc]  initWithString:@"刷新中"];
    self.refreshControl.attributedTitle  = string;
    [string release];
    //刷新的过程中会有一个耗时的操作
    //在3秒之后执行addData
    [self performSelector:@selector(addData) withObject:nil afterDelay:1];
}

-(void)addData
{
    //刷新结束
    //让刷新的控件停下来
    [self.refreshControl endRefreshing];
    //当刷新结束,改变刷新的标题
    NSAttributedString *string = [[NSAttributedString alloc  initWithString:@"下拉刷新"];
    self.refreshControl.attributedTitle = string;
    [string release];
    //数据加载完毕,接受数据
    [_dataArray addObject:@"新数据"];
    [self.tableView reloadData];
}


#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath];
    // Configure the cell...
    cell.textLabel.text = [_dataArray objectAtIndex:indexPath.row];
    return cell;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 摘要: 除了使用第三方资源库来实现下拉刷新,我们也可以使用苹果的SDK中的UIRefreshControl来实现其...
    FlyTheKite阅读 2,707评论 0 0
  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 13,002评论 3 38
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,804评论 4 61
  • 有时候我们追求榜样,不如说是在追求内心的渴望。 在人生的不同阶段,我有过不同的榜样。小时候是侠...
    抱花少女阅读 2,629评论 4 2
  • 自定义View有多种形式,可以继承自View,也可以继承自ViewGroup,还可以直接继承Andorid系统现有...
    shenhuniurou阅读 3,161评论 0 2

友情链接更多精彩内容