0

i want pull down to refresh UITableView in my UITableViewController.

This is my code, but don't works:

class MyTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        initComponents()
        loadData()
    }

    func refresh(sender: AnyObject) {
        loadData()
    }

    func initComponents() {
        self.refreshControl = UIRefreshControl()
        self.refreshControl?.addTarget(self, action: #selector(MyTableViewController.refresh(_:)), forControlEvents: UIControlEvents.ValueChanged)

        //other code
    }

    func loadData() {
        //code
    }
}
Matteo Serpentoni
  • 553
  • 1
  • 7
  • 19
  • Refreshing a table view with static cells doesn't make any sense; the cells are still going to have the same appearance – Paulw11 Jul 12 '16 at 08:51
  • @Paulw11 when i refreshing i want the same number of cell with different content of them. There is another solution? – Matteo Serpentoni Jul 12 '16 at 08:54
  • If you want different content then you have to use dynamic cells and supply the cells in `cellForRowAtIndexPath` – Paulw11 Jul 12 '16 at 08:55

3 Answers3

1

Try :

 yourTableView.addSubview(self.refreshControl)
 yourTableView.alwaysBounceVertical = true
Abd Aboudi
  • 331
  • 2
  • 7
1

try @Anil Varghese's answer .

You have to add target and when refresh control call event method then you can change data source and reload table view will help you to reload data.

var refreshControl: UIRefreshControl!

override func viewDidLoad() {
   super.viewDidLoad()

   refreshControl = UIRefreshControl()
   refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
   refreshControl.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
   tableView.addSubview(refreshControl) // not required when using UITableViewController
}

func refresh(sender:AnyObject) {
   // Code to refresh table view
}
Community
  • 1
  • 1
Jayesh Thanki
  • 2,037
  • 2
  • 23
  • 32
0

I test your code with a UITableViewController in storyboard. It works well! When I pull down to refresh, the function "refresh" works. So, what do you mean "don't works"? Perhaps you forgot to set the "Custom Class " in the storyboard ?

In addition, I agree with @Paulw11, it's better to use dynamic cells.

Dali Zhang
  • 61
  • 2