I'm fetching videos from Youtube channel, I'm able to display 10 videos when APP is starting.
I would like to use the following trick (using pagetoken ; Retrieve all videos from youtube playlist using youtube v3 API) to display more videos.
The idea is to scroll down to fetch the next videos, by using willDisplayCell method.
ViewController:
override func viewDidLoad() {
super.viewDidLoad()
self.model.delegate = self
model.getFeedVideo()
self.tableView.dataSource = self
self.tableView.delegate = self
}
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if !loadingData && indexPath.row == 10 - 1 {
model.getFeedVideo()
print("loading more ...")
}
}
- How to use pageToken from ViewController whereas I get the token from VideoModel class?
- How to automatically increment the "10" for indexPath attribute? Is it possible to get the current number of displayed cell?
VideoModel:
protocol VideoModelDelegate {
func dataReady()
}
class VideoModel: NSObject {
func getFeedVideo() {
var nextToken:String
nextToken = "XXXXXX"
// Fetch the videos dynamically via the Youtube Data API
Alamofire.request(.GET, "https://www.googleapis.com/youtube/v3/playlistItems", parameters: ["part":"snippet", "playlistId":PLAYLIST_ID, "key":API_KEY, "maxResults":10, "pageToken":nextToken], encoding: ParameterEncoding.URL, headers: nil).responseJSON { (response) -> Void in
if let JSON = response.result.value {
nextToken = JSON["nextPageToken"] as! String
print(nextToken)
var arrayOfVideos = [Video]()
for video in JSON["items"] as! NSArray {
// Creating video object...
// Removed to make it clearer
}
self.videoArray = arrayOfVideos
if self.delegate != nil {
self.delegate?.dataReady()
}
}
}
}
- Do I need to add a return value or should I create another method to get the pageToken? Please, suggest.