iOS之UITableView的使用—下拉刷新

来源:互联网 发布:淘宝上比较好的男装店 编辑:程序博客网 时间:2024/05/20 07:58

使用无界面纯代码实现

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 FKTableViewControllerNSMutableArray* 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 = [tableViewdequeueReusableCellWithIdentifier:myId];// 如果单元格为nilif(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