Pull-To-Refresh in UITableView in Swift 4

UIRefreshControl providing the refreshing control on ScrollView, TableView and CollectionView. this tutorial we are study how to use UIRefreshControl over the tableview.



UIRefreshControl is use over tableView whenever the webservice data display and reload on tableview.

Full Code of UIRefreshControl + UITableView:
import UIKit

class ViewController: UIViewController , UITableViewDelegate , UITableViewDataSource {

    @IBOutlet var refreshtable : UITableView!
    var refreshControl   = UIRefreshControl()
    let data: [String] = ["Apple", "HP", "Accer"]

    override func viewDidLoad() {
        super.viewDidLoad()

        // Refresh control add in tableview.
        refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
        refreshControl.addTarget(self, action: #selector(refresh), for: .valueChanged)
        self.refreshtable.addSubview(refreshControl)

        self.refreshtable.register(UITableViewCell.self, forCellReuseIdentifier: "cell")

    }

    @objc func refresh(_ sender: Any) {
        // Call webservice here after reload tableview.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count;
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as UITableViewCell!
        cell.textLabel?.text = self.data[indexPath.row]
        return cell
    }

}

Output :



Thanks
Pull-To-Refresh in UITableView in Swift 4 Pull-To-Refresh in UITableView in Swift 4 Reviewed by KIRIT MODI on 09:11:00 Rating: 5

No comments:

Powered by Blogger.