Saturday, November 7, 2015

UIRefreshControl - Pull down to refresh for the UITableView

This post shows how to refresh table by pulling down without using UITableViewController. An activity indicator is shown above the table while pulling it down. Tested with Xcode 6.4 and 7.1.

Edit ViewController.swift as below:

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    var tableView : UITableView!
    var tableRowNumber : Int = 5
    var refreshControl : UIRefreshControl!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView = UITableView(frame: CGRectMake(0, 30, view.bounds.width, 400))
        tableView.dataSource = self
        tableView.delegate = self
        
        view.addSubview(tableView)
        
        refreshControl = UIRefreshControl()
        refreshControl.addTarget(self, action: Selector("enlargeTable"), forControlEvents: UIControlEvents.ValueChanged)
        tableView.addSubview(refreshControl)
    }
    
    func enlargeTable() {
        tableRowNumber += 5
        tableView.reloadData()
        refreshControl.endRefreshing()
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return tableRowNumber
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "myIdentifier")
        cell.textLabel?.text = "\(indexPath.row)"
        
        return cell
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    

}

=========================
Related Post:

1 comment:

  1. https://www.youtube.com/watch?feature=player_embedded&v=eAmEaBVmqos

    ReplyDelete