Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIRefreshControl at the bottom of the UITableView iOS6?

Is it possibile add UIRefreshControl at the bottom of the UITableView? I would use it to load more data. Please, Any suggest?

like image 738
Dany Avatar asked Feb 18 '13 17:02

Dany


4 Answers

I believe there won't be any simple solution to this problem. May be someone could write a separate library to implement this behaviour plus it will cause more complications once you cache data in tableview.

But let us break down the problem and that might help you in achieving what you want. I have used the following code to add more rows in the app:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {     float endScrolling = scrollView.contentOffset.y + scrollView.frame.size.height;     if (endScrolling >= scrollView.contentSize.height)     {         NSLog(@"Scroll End Called");         [self fetchMoreEntries];     } } 

Or you could do something like that:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {     if ((scrollView.contentOffset.y + scrollView.frame.size.height) >= scrollView.contentSize.height)     {         if (!self.isMoreData)         {             self.MoreData = YES;              // call some method that handles more rows         }     } } 

You must have seen such methods in many question as i have described above and certainly not what you have asked for. But what you can do is while the above code in in process to load more data, you can add a subview and show a couple of images similar to what UIRefreshControl offers. Add the images in the subview to be shown as a progress until the above code gets executed. Home this helps.

By the way, i will suggest you not to do that as it will just waste your time for making something so smaller unless you are just doing it for learning purposes.

like image 75
Jessica Avatar answered Oct 01 '22 10:10

Jessica


The following answer is in Xcode 8 and Swift 3.

first declare

var spinner = UIActivityIndicatorView() 

than in viewDidLoad method write the following code:

spinner = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge) spinner.stopAnimating() spinner.hidesWhenStopped = true spinner.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 60) tableView.tableFooterView = spinner 

now finally override the scrollViewDidEndDragging delegate method with following:

override func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {         let offset = scrollView.contentOffset         let bounds = scrollView.bounds         let size = scrollView.contentSize         let inset = scrollView.contentInset          let y = offset.y + bounds.size.height - inset.bottom         let h = size.height          let reloadDistance = CGFloat(30.0)         if y > h + reloadDistance {             print("fetch more data")             spinner.startAnimating()         } } 
like image 32
Chowdhury Md Rajib Sarwar Avatar answered Oct 01 '22 10:10

Chowdhury Md Rajib Sarwar


You can use UIView to customize your refreshControl at bottom. Create UIView and add it to UITableView as footerView.

UIView* footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];

[footerView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"refreshImage.png"]]];

tableView.tableFooterView = footerView;

Hide it: tableView.tableFooterView.hidden = YES;

Implement UIScrollView delegate -

(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if ((scrollView.contentOffset.y + scrollView.frame.size.height) >= scrollView.contentSize.height)
    {
             tableView.tableFooterView.hidden = NO;
            // call method to add data to tableView
    }

}

before adding data to tableView save current offset by CGPoint offset = tableView.contentOffset;

call reloadData then set previously saved offset back to tableView [tableView setContentOffset:offset animated:NO];

So that you can feel new data added at bottom.

like image 34
Rahul Mane Avatar answered Oct 01 '22 11:10

Rahul Mane


I've been stuck on the same problem recently and write a category for UIScrollView class. Here it is CCBottomRefreshControl.

like image 43
morbo Avatar answered Oct 01 '22 10:10

morbo