Create custom Pull-To-Refresh in your UITableView

Pull to Refresh Objective-C

In this tutorial, I will show you how to implement a simple pull to refresh feature for refreshing content in your UITableView using UIRefreshControl.

 

This snippet will add the pull to refresh as a subview to UITableView: 

-(void)addPullToRefresh{
    refreshControl = [[UIRefreshControl alloc] init];
    refreshControl.backgroundColor = [UIColor colorWithRed:173/255.0 green:31/255.0 blue:45/255.0 alpha:1.0];
    refreshControl.tintColor = [UIColor whiteColor];
    [refreshControl addTarget:self action:@selector(refreshContent) forControlEvents:UIControlEventValueChanged];
    [tableView addSubview:refreshControl];
}

-(void)refreshContent{
    [refreshControl endRefreshing];
}

 

It should look like this:

Simulator Screen Shot 28 May 2016 19.02.15

 

Then we will add a title: 

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MMM d, h:mm a"];
    NSString *title = [NSString stringWithFormat:@"Update: %@", [formatter stringFromDate:[NSDate date]]];
    NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIColor whiteColor]

    NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:title attributes:attrsDictionary];
    refreshControl.attributedTitle = attributedTitle;

 

This is how it should look like now:

Simulator Screen Shot 28 May 2016 19.03.35

Nice and easy!

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *