Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pull to refresh on UITableView Inside a UIViewController

I am trying to implement the pull to refresh functionality in my application. The architecture is such that the there is a UITableView inside a UIViewController. I want to be able to refresh the tableview on pull down. I tried the code below in the viewDidLoad method, but it does not work. Can any one tell me where am I wrong in implementation?

UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
    refresh.tintColor = [UIColor grayColor];
    refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to Refresh"];
    [refresh addTarget:self action:@selector(get_vrns) forControlEvents:UIControlEventValueChanged];
    [self.vrnTable addSubview:refresh];
like image 370
Hassan Zaheer Avatar asked Nov 22 '13 06:11

Hassan Zaheer


1 Answers

For Swift 3 and iOS backward compatibility

var refreshControl = UIRefreshControl()
let string = "Pull to refresh"
let attributedString = NSMutableAttributedString(string: string)
    attributedString.addAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 16)),NSForegroundColorAttributeName:UIColor.white], range: NSRange.init(location: 0, length: string.characters.count))
self.refreshControl.attributedTitle = attributedString
self.refreshControl.tintColor = UIColor.white
self.refreshControl.addTarget(self,
                             action: #selector(self.pulledDownForRefresh),
                             for: .valueChanged)
if #available(iOS 10.0, *) {
    self.accountSummaryTableView.refreshControl = refreshControl
} else {
    self.accountSummaryTableView.addSubview(refreshControl)
}

func pulledDownForRefresh() {
     //do some opertaion and then call
    self.refreshControl.endRefreshing()
}
like image 54
Satheesh Avatar answered Sep 17 '22 11:09

Satheesh