Search code examples
iosuitableviewswiftuirefreshcontrol

iOS swift pull to refresh mixes with tableview


In my table view controller, i have implemented pull to refresh (UIRefreshControl). The problem is that I do not know why it is mix with the tableView (UITableViewController). For details, see the screenshot. Thank you for your assistance!

pull to refresh mix with tableView


Solution

  • You can implement refresh control like this.

    class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
        @IBOutlet var tableView: UITableView!
        var refreshControl : UIRefreshControl!
    
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
    
        self.refreshControl = UIRefreshControl()
        self.refreshControl.backgroundColor = UIColor.clearColor()
        self.refreshControl.tintColor = UIColor.blackColor()
    
        self.refreshControl.addTarget(self, action: "methodPullToRefresh:", forControlEvents: UIControlEvents.ValueChanged) 
    
        self.tableView.addSubview(self.refreshControl)
    
    }
    
    func methodPullToRefresh(sender:AnyObject)
    {
        self.refreshControl?.beginRefreshing()
    
    }
    

    enter image description here

    // Once you are done with your task
    self.refreshControl?.endRefreshing()
    
    // Main queue thread is only required when refresh controls comes or goes off with delay, if it works quickly then no need to add this
    dispatch_async(dispatch_get_main_queue()) {
    
    }
    

    enter image description here

    Hope, this will resolve your problem.

    All the best.