iOS之UITableView的使用—下拉刷新

使用无界面纯代码实现

1、AppDelegate类

//.h
#import <UIKit/UIKit.h>

@class FKTableViewController;

@interface FKAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) FKTableViewController *viewController;

@end


//.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
	self.viewController = [[FKTableViewController alloc]
		initWithStyle:UITableViewStyleGrouped];
	self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

2、FKTableViewController类

//.h
#import <UIKit/UIKit.h>

@interface FKTableViewController : UITableViewController

@end

//.m
#import "FKTableViewController.h"

@interface FKTableViewController ()

@end

@implementation FKTableViewController
NSMutableArray* list;
- (void)viewDidLoad
{
    [super viewDidLoad];
	// 初始化NSMutableArray集合
	list = [[NSMutableArray alloc] initWithObjects:@"孙悟空",
			@"猪八戒",
			@"牛魔王",
			@"蜘蛛精",
			@"白骨精",
			@"狐狸精" , nil];
	// 设置refreshControl属性,该属性值应该是UIRefreshControl控件
	self.refreshControl = [[UIRefreshControl alloc]init];
	// 设置UIRefreshControl控件的颜色
	self.refreshControl.tintColor = [UIColor grayColor];
	// 设置该控件的提示标题
	self.refreshControl.attributedTitle = [[NSAttributedString alloc]
		initWithString:@"下拉刷新"];
	// 为UIRefreshControl控件的刷新事件设置事件处理方法
	[self.refreshControl addTarget:self action:@selector(refreshData)
		forControlEvents:UIControlEventValueChanged];
}
// 该方法返回该表格的各部分包含多少行。
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section
{
	return [list count];
}
// 该方法的返回值将作为指定表格行的UI控件
- (UITableViewCell*) tableView:(UITableView *)tableView
		 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *myId = @"moveCell";
	// 获取可重用的单元格
	UITableViewCell *cell = [tableView
		dequeueReusableCellWithIdentifier:myId];
	// 如果单元格为nil
	if(cell == nil)
	{
		// 创建UITableViewCell对象
		cell = [[UITableViewCell alloc] initWithStyle:
				UITableViewCellStyleDefault reuseIdentifier:myId];
	}
	NSInteger rowNo = [indexPath row];
	// 设置textLabel显示的文本
	cell.textLabel.text = [list objectAtIndex:rowNo];
	return cell;
}
// 刷新数据的方法
- (void) refreshData
{
	// 使用延迟2秒来模拟远程获取数据
	[self performSelector:@selector(handleData) withObject:nil afterDelay:2];
}
- (void) handleData
{
	// 获取一个随机数字符串
	NSString* randStr = [NSString stringWithFormat:@"%d"
		, arc4random() % 10000];
	// 将随机数字符串添加list集合中
	[list addObject:randStr];
	self.refreshControl.attributedTitle = [[NSAttributedString alloc]
		initWithString:@"正在刷新..."];
	// 停止刷新
	[self.refreshControl endRefreshing];
	// 控制表格重新加载数据
	[self.tableView reloadData];
}
@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值