Swift tableview自带的刷新控件
import UIKit class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { let tableview = UITableView(frame: ScreenRect, style: .plain) let refreshControl = UIRefreshControl() let cell = "cell" var arraymutable = ["11","22","33","44","55"] let array = ["22","33","55","88"] override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1) CreatUi() // Do any additional setup after loading the view. } func CreatUi(){ tableview.separatorStyle = .singleLine tableview.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1) tableview.delegate = self tableview.dataSource = self tableview.tableFooterView = UIView() tableview.refreshControl = refreshControl refreshControl.backgroundColor = UIColor.gray refreshControl.attributedTitle = NSAttributedString(string: "刷新一下:\(NSDate())", attributes: [NSForegroundColorAttributeName:UIColor.white])//设置文字颜色 refreshControl.tintColor = UIColor.green //小菊花的颜色 refreshControl.tintAdjustmentMode = .dimmed //色彩调整模式 refreshControl.addTarget(self, action: #selector(addcount), for: .valueChanged) view.addSubview(tableview) } func addcount(){ arraymutable.append(contentsOf: array) tableview.reloadData() refreshControl.endRefreshing() } override var preferredStatusBarStyle: UIStatusBarStyle{ return .default } //tableview 代理 func numberOfSections(in tableView: UITableView) -> Int { return 2 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return arraymutable.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let celll = tableView.dequeueReusableCell(withIdentifier: cell) ?? UITableViewCell(style: .default, reuseIdentifier:cell) celll.textLabel?.text = arraymutable[indexPath.row] celll.textLabel?.textColor = UIColor.red celll.textLabel?.font = UIFont.boldSystemFont(ofSize: 20) return celll } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 80 } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }