代码之家  ›  专栏  ›  技术社区  ›  user8202800

如何使用按钮或按钮刷新tableView和JSON数据?

  •  0
  • user8202800  · 技术社区  · 7 年前

    我是swift的新手,我找不到如何同时刷新TableView和JSON数据。刷新TableView后,首先刷新Json。我该怎么做?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Kegham K.    7 年前

    您需要向 UITableView

    private let refreshControl = UIRefreshControl()
    var tableView: UITableView!
    

    然后在 ViewDidLoad 将其作为子视图添加到 tableView 你甚至可以改变它的颜色和文字。

    override func viewDidLoad() {
    
         if #available(iOS 10.0, *) {
            tableView.refreshControl = refreshControl
        } else {
            tableView.addSubview(refreshControl)
        }
        refreshControl.tintColor = UIColor.blue
        refreshControl.attributedTitle = NSAttributedString(string: "Fetching Data ...", attributes: [NSAttributedStringKey.foregroundColor : UIColor(red:0/255, green:168/255, blue:225/255, alpha:1.0)])
        refreshControl.addTarget(self, action: #selector(refreshData(_:)), for: .valueChanged)
    
    }
    

    当值发生如上所示的变化时,使用选择器函数向其添加一个目标。

    @objc private func refreshData(_ sender: Any) {
    
       //load the data from the server and 
       //parse and put it in an array then reload your table view
    
    }