7.

refreshControl

let refreshControl = UIRefreshControl()
   
 tableView.refreshControl = refreshControl
        refreshControl.backgroundColor = .gray
        refreshControl.attributedTitle = NSAttributedString(string: "最后一次更新:\(NSDate())",attributes: [NSForegroundColorAttributeName: UIColor.white])//文字的颜色
        refreshControl.tintColor = .orange//菊花的颜色
        refreshControl.addTarget(self, action: #selector(addContent), for: .valueChanged)

<li><h1>学习代码:<h1></li>

ViewController.swift


import UIKit

let YHRect = UIScreen.main.bounds
let YHHeight = YHRect.size.height
let YHWidth = YHRect.size.width

class ViewController: UIViewController {
    
    
    let tableView = UITableView(frame: YHRect, style: .plain)
    let refreshControl = UIRefreshControl()
    
    var contents = ["下下下下下","拉拉拉拉拉","刷刷刷刷刷","新新新新新","哟哟哟哟哟"]
    let news = ["1111","2222","3333","4444","5555","6666"]
    let cellIdentifier = "RefreshCell"
    

    override func viewDidLoad() {
        super.viewDidLoad()
        setupView()
    }
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .default
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    func setupView() {
        
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.tableFooterView = UIView()
        
        tableView.refreshControl = refreshControl
        refreshControl.backgroundColor = .gray
        refreshControl.attributedTitle = NSAttributedString(string: "最后一次更新:\(NSDate())",attributes: [NSForegroundColorAttributeName: UIColor.white])//文字的颜色
        refreshControl.tintColor = .orange//菊花的颜色
        refreshControl.addTarget(self, action: #selector(addContent), for: .valueChanged)
        
        view.addSubview(tableView)
    }
    
    func addContent() {
        contents.append(contentsOf: news)
        tableView.reloadData()
        refreshControl.endRefreshing()
    }

}

//将代理写到扩展里面
extension ViewController:UITableViewDataSource, UITableViewDelegate {
    //MARK: - DataSource
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return contents.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) ?? UITableViewCell(style: .default, reuseIdentifier: cellIdentifier)
        cell.textLabel?.text = String(indexPath.row+1) + ":" + contents[indexPath.row]
        cell.textLabel?.textColor = .black
        cell.textLabel?.backgroundColor = .clear
        cell.textLabel?.font = UIFont.systemFont(ofSize: 30, weight: 10)
        return cell
    }
    
    //MARK: - Delegate
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容