我从Youtube频道获取视频,我可以在APP启动时显示10个视频。
我想使用以下技巧(使用pagetoken; PhotoView)来显示更多视频。
我们的想法是使用willDisplayCell方法向下滚动以获取下一个视频。
的ViewController :
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()
}
}
}
}
VideoModel :
var counter = 0;
function Initialize () {
document.getElementById(name).addEventListener('input', UpdateSlider);
}
function UpdateSlider()
{ if ( counter == 0)
{ var cols = document.getElementsByClassName(classname);
RevealTextLines (cols);
}
function RevealTextLines (cols)
{
[].forEach.call(cols, function(el) {
timer = el.length;
var snippet = el.innerHTML;
el.innerHTML = '';
el.style.display = 'inline';
(function addNextCharacter(h) {
el.innerHTML = snippet.substr(0,h);
h++;
if (h <= snippet.length) {
setTimeout(function() {
addNextCharacter(h);
timer--;
UpdateSlider();
}, delay);
}
})(1);
});
}
答案 0 :(得分:0)
我将就如何完成它给你一个大致的想法。
首先:检查用户是否已使用if !loadingData && indexPath.row == self.videos.count - 1
滚动到最后一个元素
秒(编辑):
//VideoModel.swift
if self.delegate != nil {
//Pass fetched videos and token to the delegate method.
self.delegate?.dataReady(self.videoArray, nextToken)
}
现在,委托方法的变化很少:
//ViewController.swift
func dataReady(Array->Videos,String->Token) {
//Define a variable globally and assign received token for later use
tokenGlobal = Token
//add received array of videos to the current array of videos
[self.videos addObjectsFromArray:Videos];
//Here are two options to populate new videos,
//First, Reload whole table view, not a recommended approach, since it will take much more time to load
self.tableView.reloadData()
//Second, add cells to the existing tableView, instead of loading whole table it will added new cells according to the newly added videos into the array.
}
现在当用户到达tableView的末尾时,传递我们保存在全局变量中的标记,如下所示:
if !loadingData && indexPath.row == self.videos - 1 {
model.getFeedVideo(tokenGlobal)
print("loading more ...")
}
现在,使用此tokenGlobal
使用“getFeedVideo”方法获取更多视频。
查找SO以了解如何向表视图添加新单元格。但是,对于测试,您可以继续reloadData
。
P.S :我不知道Swift语法。 :)