0

I have an API and when I send a request from Alamofire it returns me, for example, 500 records! I cannot take all of it and use it. Instead of it I did pagination on my API, as /page/1, /page/2, etc.

But how can I load this data in my UITableView? I want to show loading indicator when I scroll my UITableView, while I get my data, and get my data partly, append it to my main array. How can I achieve this?

Or there is another solution for it? What you can advice me?

I searched a lot about it, but most of the solutions are in Objective-C. I found this one: iOS - How to make the tableview use paginated API output?

but I did not understand the answer completely. Some variables there, I do not know what they means

Community
  • 1
  • 1
John Doe
  • 511
  • 1
  • 6
  • 13

2 Answers2

1

You will need to create a UIActivityIndicatorView and use scrollViewDidScroll(scrollView: UIScrollView) to control the data array

private let serialQueue = dispatch_queue_create("mainVCLoadingQueue", DISPATCH_QUEUE_SERIAL)

override func scrollViewDidScroll(scrollView: UIScrollView) {
    delegate?.mainCollectionViewDidScroll(scrollView)
}

func mainCollectionViewDidScroll(scrollView: UIScrollView) {
        dispatch_sync(serialQueue, {
            if scrollView.contentSize.height - scrollView.contentOffset.y - scrollView.frame.height < 1000 {
                dispatch_async(GlobalMainQueue, {
                    loadingIndicator.startAnimating()
                    if !self.isLoading {
                        self.isLoading = true
                        self.itemDataManager!.getMoreItem({ itemList in
                            self.rootView?.data = itemList
                            self.isLoading = false
                            loadingIndicator.stopAnimating()
                        })
                    }
                })
            }
        })
    }

var data: YourData? {
    didSet{
        updateUI()
    }
}

private func updateUI(){
    collectionView.dataSource = data
    collectionView.refresh()
}
Breek
  • 1,431
  • 13
  • 24
-1

First of all you gotta create instance of NSMutableArray and pagination Indicator int in your @interface in .h or .m file

NSMutableArray *_data;
int _currentPage;

And initiate it in viewDidLoad make sure it initiated before tableView.delegate or tableView.dataSource is set

_data = [[NSMutableArray alloc]init];
_currentPage = 0;

And use _data across UITableView Delegation Methods

Now you gotta think of it in this way "When ever the user reaches the end of UITableView" you gotta request the next page

Fortunately , there is a method in UITableView Delegation Methods called

- (void)tableView:(UITableView *)tableView
  willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath 

so we gonna use it in this way

- (void)tableView:(UITableView *)tableView
  willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath {
 if (indexPath.item == [_data count] - 1 /* you can add more logic here to prevent duplication of data */){
  ///////// Request With _currentPage
  //////// after successful response 202 increase page number by 1
  /////// _currentPage ++;
 }
}

Hopefully this helps ..

mustafa96m
  • 329
  • 3
  • 16