如何在iOS应用程序中实现YouTube数据API V3的分页



我正在使用Youtube数据API构建一个iOS应用程序,该应用程序获取频道的播放列表并将其显示在表视图中。但问题是我只能得到最多50个播放列表的结果。但实际播放列表的数量超过了100。当我在谷歌上搜索时,我发现要获得额外的结果,应该进行分页。如何在iOS应用程序中添加分页,以便在表格视图中显示整个播放列表。如果我想使用nextpage令牌进行另一个请求,那么我应该如何进行,以及应该修改代码的哪一部分。我使用API密钥而不是OAuth

ViewController代码

import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {


@IBOutlet weak var coursesTV: UITableView!

var a = [Item]()

override func viewDidLoad() {
super.viewDidLoad()


coursesTV.delegate = self
coursesTV.dataSource = self

Playlist.sharedObj.getPlayList { (i) in
self.a = i

DispatchQueue.main.async {
self.coursesTV.reloadData()
}

}


}



func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {


return a.count

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

if let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
{
cell.textLabel?.text = a[indexPath.row].snippet!.title!

return cell
}

return UITableViewCell()


}
}

NetworkService结构

import Foundation
public  struct Playlist
{
public static let sharedObj = Playlist()
let playlistuRL = "URL_HERE"


let session = URLSession(configuration: .default)


func getPlayList(onSuccess:@escaping([Item])-> Void)
{
let task = self.session.dataTask(with: URL(string: self.playlistuRL)!) { (data, response, error) in

do
{

let item = try JSONDecoder().decode(RootClass.self, from: data!)

print(item.items!.first!.snippet!.title!)

onSuccess(item.items)
}

catch
{
print(error)
}



}


task.resume()

}
}

模型结构

import Foundation

public struct RootClass: Codable {
public var etag : String!
public var items : [Item]!
public var kind : String!
public var nextPageToken : String!
public var pageInfo : PageInfo!

}

public struct Default: Codable {
public var height : Int!
public var url : String!
public var width : Int!

}
public struct High: Codable {
public var height : Int!
public var url : String!
public var width : Int!

}
public struct Item: Codable {
public var etag : String!
public var id : String!
public var kind : String!
public var snippet : Snippet!

}
public struct Localized: Codable {
public var descriptionField : String!
public var title : String!

}
public struct Medium: Codable {
public var height : Int!
public var url : String!
public var width : Int!

}
public struct PageInfo: Codable {
public var resultsPerPage : Int!
public var totalResults : Int!

}

public struct Snippet: Codable {
public var channelId : String!
public var channelTitle : String!
public var descriptionField : String!
public var localized : Localized!
public var publishedAt : String!
public var thumbnails : Thumbnail!
public var title : String!

}
public struct Standard: Codable {
public var height : Int!
public var url : String!
public var width : Int!

}

public struct Thumbnail: Codable {
public var defaultField : Default!
public var high : High!
public var medium : Medium!
public var standard : Standard!

}

基本上,您必须实现表视图的加载更多概念。当您到达表中的最后一行时,发出api请求。Youtube API提供NextPageToken和PreviousPageToken作为响应。所以,当您到达最后一行时,您可以使用NextPageToken发出api请求。

示例代码片段:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text=[self.array objectAtIndex:indexPath.row];
if (indexPath.row ==[array count] -1 ) { //this method will get called when you will scroll to the bottom of your table view 
[self sendAPIRequestMethod];  } return cell; }

最新更新