UIRefreshControl

UIRefreshControl的使用

分类: ios开发2013-01-26 15:18 8539人阅读 评论(2) 收藏 举报

转自http://www.devdiv.com/iOS_iPhone-iOS6%E6%96%B0%E7%89%B9%E5%BE%81%EF%BC%9AUIRefreshControl_%E4%B8%8B%E6%8B%89%E5%88%B7%E6%96%B0_%E4%BD%BF%E7%94%A8%E7%A4%BA%E4%BE%8B-thread-127741-1-1.html

UIRefreshControl的使用非常简单。

1、使用范围
如果你装了xcode_4.5_developer_preview,那么在UITableViewController.h文件中你会看到,UITableViewController里面有如下声明,说明UITableViewController已经内置了UIRefreshControl控件

[cpp] view plaincopy

  1. @property (nonatomic,retain) UIRefreshControl *refreshControl NS_AVAILABLE_IOS(6_0);

【注】:UIRefreshControl目前只能用于UITableViewController,如果用在其他ViewController中,运行时会得到如下错误提示:(即UIRefreshControl只能被UITableViewController管理)

[cpp] view plaincopy

  1. 2012-06-15 14:34:34.908 DevDivUIRefreshControl[722:10103] *** Terminating app due to uncaught exception ‘NSInternalInconsistencyException‘, reason: ‘UIRefreshControl may only be managed by a UITableViewController‘
  2. *** First throw call stack:
  3. (0x186fd72 0x1066e51 0x186fb4b 0x55a559 0x57238 0x5d482 0x55ad2 0x2ebb 0xeb2a3 0xeb30e 0x10b7e9 0x10b624 0x109aef 0x10999c 0x107adc 0x1082c6 0xecf24 0xed1e0 0xee084 0x5645c 0x5cf31 0x55ad2 0x4131d 0x414f6 0x4168c 0x49871 0x10a90 0x1196a 0x222be 0x22f9f 0x153fd 0x17ccf39 0x17ccc10 0x17e5da5 0x17e5b12 0x1816b46 0x1815ed4 0x1815dab 0x1128f 0x12e71 0x29fd 0x2925)
  4. libc++abi.dylib: terminate called throwing an exception
  5. (lldb)

2、如何使用
    a)初始化
如何在UITableViewController 中使用UIRefreshControl呢,在上面给出的代码附件中,你可以很详细的知道,这里介绍一下关键的部分:

[cpp] view plaincopy

  1. self.refreshControl = [[UIRefreshControl alloc]init];
  2. //    self.refreshControl.tintColor = [UIColor blueColor];
  3. self.refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"下拉刷新"];
  4. [self.refreshControl addTarget:self action:@selector(RefreshViewControlEventValueChanged) forControlEvents:UIControlEventValueChanged];

如上面看到的代码,虽然UITableViewController已经声明了UIRefreshControl,但是貌似还没有初始化,所以需要我们自己初始化。很神奇,初始化的时候并不需要给它指定frame,UITableViewController会为我们进行管理。遗憾的时目前只看到下拉刷新功能,上拉刷新还没有,估计在最终版里面苹果会考虑加入上拉刷新功能。
我们还可以给UIRefreshControl设置tintColor和attributedTitle。

b)下拉刷新事件监听
当用户进行下拉刷新操作时,UIRefreshControl 会触发一个UIControlEventValueChanged事件,通过监听这个事件,我们就可以进行类似数据请求的操作了。如下代码:
[self.refreshControl addTarget:self action:@selector(RefreshViewControlEventValueChanged)

c)进行数据请求
在示例中,为了演示数据请求,我简单的做了一个延时处理,2秒钟后,调用handleData

[cpp] view plaincopy

  1. [self performSelector:@selector(handleData) withObject:nil afterDelay:2];

在handleData里面,就表示已经请求到了数据,在此进行UI更新即可。也需要注意的是,我们调用UIRefreshControl 的endRefreshing方法,表示刷新结束,让UIRefreshControl更新显示。

[cpp] view plaincopy

  1. - (void) handleData
  2. {
  3. NSLog(@"refreshed");
  4. [self.refreshControl endRefreshing];
  5. self.refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"下拉刷新"];
  6. self.count++;
  7. [self.tableView reloadData];
  8. }

3、官方头文件
下面是sdk中UIRefreshControl的声明,想必看了下面的代码,你已经知道如何使用了。

[cpp] view plaincopy

  1. //
  2. //  UIRefreshControl.h
  3. //  UIKit
  4. //
  5. //  Copyright 2012 Apple Inc. All rights reserved.
  6. //
  7. #import <Foundation/Foundation.h>
  8. #import <UIKit/UIControl.h>
  9. #import <UIKit/UIKitDefines.h>
  10. NS_CLASS_AVAILABLE_IOS(6_0) @interface UIRefreshControl : UIControl
  11. /* The designated initializer
  12. * This initializes a UIRefreshControl with a default height and width.
  13. * Once assigned to a UITableViewController, the frame of the control is managed automatically.
  14. * When a user has pulled-to-refresh, the UIRefreshControl fires its UIControlEventValueChanged event.
  15. */
  16. - (id)init;
  17. @property (nonatomic, readonly, getter=isRefreshing) BOOL refreshing;
  18. @property (nonatomic, retain) UIColor *tintColor UI_APPEARANCE_SELECTOR;
  19. @property (nonatomic, retain) NSAttributedString *attributedTitle UI_APPEARANCE_SELECTOR;
  20. // May be used to indicate to the refreshControl that an external event has initiated the refresh action
  21. - (void)beginRefreshing NS_AVAILABLE_IOS(6_0);
  22. // Must be explicitly called when the refreshing has completed
  23. - (void)endRefreshing NS_AVAILABLE_IOS(6_0);
  24. @end
    • 上一篇s
时间: 2024-07-29 13:33:35

UIRefreshControl的相关文章

新浪微博客户端(18)-集成下拉刷新控件UIRefreshControl

HomeViewController.m - (void)setupPullToRefreshView { UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init]; [refreshControl addTarget:self action:@selector(refreshNewData:) forControlEvents:UIControlEventValueChanged]; [self.tableView a

IOS6.0自带下拉刷新控件UIRefreshControl

1.UIRefreshControl必须要在IOS6.0以后才能使用,同时他只能在UITableViewController类中才可以使用 2.使用比较简单 self.refreshControl = [[UIRefreshControl alloc]initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 100)]; [self.refreshControl addTarget:self action:@selector(

iOS:下拉刷新控件UIRefreshControl的详解

下拉刷新控件:UIRefreshControl 1.具体类信息: @interface UIRefreshControl : UIControl //继承控制类 - (instancetype)init; @property (nonatomic, readonly, getter=isRefreshing) BOOL refreshing; //是否可以刷新 @property (nonatomic, retain) UIColor *tintColor; //控件颜色 @property (

[iOS] 使用UIRefreshControl 实现 UITableView下拉刷新(Swift版本)

首先,在viewDidLoad中初始化相关数据: override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. //添加刷新 refreshControl.addTarget(self, action: "refreshData", forControlEvents: UIControlEvents.ValueChanged) refreshCon

[IOS]你用过原生的 UIRefreshControl 吗?

第三方太过于强大,上拉刷新,下拉刷下,左边,右边,各种刷新, 先介绍几个第三方: JHRefresh: https://github.com/Jiahai/JHRefresh 可以自定义动画的上.下拉刷新,Demo效果类似大众点评的动画效果,动画是帧动画实现的 那就自备图片组吧 ~ ~ ~ MJRefresh: https://github.com/CoderMJLee/MJRefresh 用法比较简单,同时支持TableView.CollectionView:详情见 github 连接 AAP

iOS - Storyboard 使用 UIRefreshControl 下拉刷新

1. 使用条件: 在UITableViewController中使用 2.可以通过代码 : [self.refreshControl addTarget:self action:@selector(refreshControlAction:) forControlEvents:UIControlEventValueChanged]; - (void)refreshControlAction:(UIRefreshControl *)sender { [self.tableView reloadDa

UIRefreshControl --- IOS中用于刷新UITableView等的控件

IOS开发中, 经常需要添加UITableView的下拉刷新功能, 使用UIRefreshControl就可以非常方便得实现. UIRefreshControl 下边是UIRefreshControl的头文件. import Foundation import UIKit // // UIRefreshControl.h // UIKit // // Copyright 2012-2014 Apple Inc. All rights reserved. // @availability(iOS,

UIRefreshControl系统下拉刷新

1.目前只对UITableviewController有用: 2.只能下拉刷新,不能上拉刷新: 3.init或者viewdidload中创建UIRefreshControl,设置文字,颜色等信息: 4.系统自动管理UIRefreshControl,自动添加到tableview视图中: 5.给UIRefreshControl添加方法,当值改变的时候调用,方法用于数据请求: 6.该方法中请求数据确认完成之后,调用endRefreshing方法,关闭刷新: - (void)creatRefreshin

UITableViewCell左滑的时候添加多个按钮的方法(iOS8+)以及UIRefreshControl(iOS6+)的使用。

之前想在cell左滑的时候添加更多的按钮而不是只有‘删除’按钮如下所示,貌似不是一件简单的事.但是现在只要实现几个方法就行了. 代码写的比较垃圾,重在理解这个知识.. . 具体代码: // //  TableViewController.m //  ios8_tableview(左滑添加按钮) // //  Created by mudy on 15/8/21. //  Copyright (c) 2015年 mudy. All rights reserved. // #import "Tabl

利用UIRefreshControl实现tableView下拉刷新

- (void)viewDidLoad { [super viewDidLoad]; // 此处的self->ViewController继承于UITableViewController UIRefreshControl *refresh = [[UIRefreshControl alloc] initWithFrame:CGRectZero]; refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"下