iOS7 Displaying a Refresh Control for TableViews

####iOS7 Programming Cookbook 第四章 Deleting Cells and Sections from TableViews

#####ViewController.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

static NSString *CellIdentifier = @"Cell";

@interface ViewController ()
//可变数组
@property (nonatomic, strong) NSMutableArray *allTimes;
//iOS6下啦刷新
@property (nonatomic, strong) UIRefreshControl *refreshControl;
@end

@implementation ViewController

-(void)viewDidLoad{
[super viewDidLoad];
self.edgesForExtendedLayout = UIRectEdgeNone;
}

- (void) handleRefresh:(id)paramSender{

// 不仅仅是做更新动画
int64_t delayInSeconds = 1.0f;
//GCD 抽象的表示时间
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);

dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

//  添加当前日期的日期列表 表视图刷新时,将出现一个新的项目
[self.allTimes addObject:[NSDate date]];
//停止刷新
[self.refreshControl endRefreshing];

NSIndexPath *indexPathOfNewRow =
[NSIndexPath indexPathForRow:self.allTimes.count-1 inSection:0];
//添加行
[self.tableView
insertRowsAtIndexPaths:@[indexPathOfNewRow]
withRowAnimation:UITableViewRowAnimationAutomatic];
});

}

- (id)initWithStyle:(UITableViewStyle)style{
self = [super initWithStyle:style];
if (self) {
//注册cell类
[self.tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:CellIdentifier];
//可变数组添加对象
self.allTimes = [NSMutableArray arrayWithObject:[NSDate date]];

/* 创建refresh control */
self.refreshControl = [[UIRefreshControl alloc] init];
self.refreshControl = self.refreshControl;
//添加刷新事件
[self.refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];

}
return self;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}

//allTimes.count rows
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section{
return self.allTimes.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

cell.textLabel.text = [NSString stringWithFormat:@"%@",self.allTimes[indexPath.row]];

return cell;
}

@end

Reference

坚持原创技术分享,您的支持将鼓励我继续创作!
0%