UITableView 下拉更新

前言

許多App有下拉選單,即可更新的功能.

自己使用TableView想要達到類似功能,google了一下,做了demo.

實作

使用RefreshControl即可達成目的.

首先在viewWillApper裡面加入下列code,

baclgroundColor為"上方更新畫面"的背景顏色, tintColor為 旋轉圈圈的顏色, 最後當下拉的時候會呼叫relodData()

TableViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self reloadData];

    self.refreshControl = [[UIRefreshControl alloc] init];
    self.refreshControl.backgroundColor = [UIColor whiteColor];
    self.refreshControl.tintColor = [UIColor blackColor];
    [self.refreshControl addTarget:self
                            action:@selector(reloadData)
                  forControlEvents:UIControlEventValueChanged];
}

在更新畫面中,除了轉圈之外,還有顯示更新的日期時間.

更新完成之後,記得加上endRefreshing結束刷新畫面.

[self.refreshControl endRefreshing];
TableViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
-(void)reloadData
{
    //載入資料

    if(self.refreshControl) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"MMM d, h:mm:ss a"];

        NSString *title = [NSString stringWithFormat:@"Last update: %@", [formatter stringFromDate:[NSDate date]]];
        NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIColor blackColor]
                                                                    forKey:NSForegroundColorAttributeName];

        NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:title attributes:attrsDictionary];

        self.refreshControl.attributedTitle = attributedTitle;

        [self.refreshControl endRefreshing];
    }
}

執行結果如下:

參考資料

Pull to refresh uitableviw

官方文件