import UIKit
class TableViewController: UITableViewController {
// 数据源
var dataSource = ["a", "b", "c", "d", "e", "f"]
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
// 设置导航栏左边图标
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Edit, target: nil, action: nil)
// 设置导航栏右边图标
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: nil, action: nil)
// 设置导航栏标题
navigationItem.title = "Groceries"
// 设定refreshControl
refreshControl = UIRefreshControl()
// refreshControl 激活时调用 handleRefresh
refreshControl?.addTarget(self, action: "handleRefresh", forControlEvents: UIControlEvents.ValueChanged)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func handleRefresh() {
let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(2 * Double(NSEC_PER_SEC)))
// 两秒延迟调用模拟加载时间
dispatch_after(dispatchTime, dispatch_get_main_queue()) {
self.dataSource.append("g")
self.dataSource.append("h")
// 重载数据
self.tableView.reloadData()
// 停止动画
self.refreshControl?.endRefreshing()
}
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSource.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
// Configure the cell...
cell.textLabel?.text = dataSource[indexPath.row]
return cell
}
}