UITableView:滚动以加载更多Youtube项目

时间:2015-12-29 01:24:08

标签: ios swift uitableview scroll youtube-api

我从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()
            }

        }
    }
}
  1. 如何从ViewController使用pageToken,而我得到令牌 来自VideoModel类?
  2. 如何自动增加" 10"对于 indexPath属性?是否可以获得当前显示的数量 细胞
  3. 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);
          });
    
     } 
    
    1. 我是否需要添加返回值,还是应该创建另一个方法来获取pageToken?请建议。

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语法。 :)