将数据从 JSON 传递到 table 查看 Swift 中的单元格 3
Passing data from JSON to table view cell in Swift 3
我正在尝试将数据从 JSON 响应传递到 table 视图单元格。我在捕获在 URLSession.shared.dataTask
.
中提取的响应值时遇到问题
func callYouTubeAPIToGetAllVideos() {
let url = URL(string: "https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=XYZ&maxResults=50&order=date&key=ABC")
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil {
print(error!)
} else {
if let usableData = data {
let json = try? JSONSerialization.jsonObject(with: usableData, options: [])
if let dictionary = json as? [String: Any?] {
if let array = dictionary["items"] as? [Any] {
for object in array {
if let objectAsDictionary = object as? [String: Any?] {
if let objectWithKindAndVideoId = objectAsDictionary["id"] as? [String: String] {
if let videoId = objectWithKindAndVideoId["videoId"] {
//pass data to table cell
}
}
if let snippet = objectAsDictionary["snippet"] as? [String: Any] {
if let description = snippet["description"] {
//pass data to table cell
}
}
}
}
}
}
}
}
}
task.resume()
}
我尝试将值附加到实例变量,但没有成功。
抱歉代码乱七八糟,这是我第一次在 Swift 中使用 JSON。
首先从不将收到的JSON字典声明为[String:Any?]
。收到的字典值不能是 nil
.
声明自定义结构Video
。
struct Video {
let videoId : String
let description : String
}
声明一个数据源数组。
var videos = [Video]()
将JSON解析到数组中并在主线程上重新加载table视图。
func callYouTubeAPIToGetAllVideos() {
let url = URL(string: "https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=XYZ&maxResults=50&order=date&key=ABC")
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil {
print(error!)
} else {
do {
if let dictionary = try JSONSerialization.jsonObject(with: data!) as? [String: Any],
let array = dictionary["items"] as? [[String: Any]] {
for object in array {
if let objectWithKindAndVideoId = object["id"] as? [String: String],
let snippet = object["snippet"] as? [String: Any] {
let videoId = objectWithKindAndVideoId["videoId"] ?? ""
let description = snippet["description"] as? String ?? ""
let video = Video(videoId: videoId, description: description)
self.videos.append(video)
}
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
} catch {
print(error)
}
}
}
task.resume()
}
在 cellForRow 中将值分配给文本属性
let video = videos[indexPath.row]
cell.textLabel!.text = video.videoId
cell.detailTextLabel?.text = video.description
我正在尝试将数据从 JSON 响应传递到 table 视图单元格。我在捕获在 URLSession.shared.dataTask
.
func callYouTubeAPIToGetAllVideos() {
let url = URL(string: "https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=XYZ&maxResults=50&order=date&key=ABC")
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil {
print(error!)
} else {
if let usableData = data {
let json = try? JSONSerialization.jsonObject(with: usableData, options: [])
if let dictionary = json as? [String: Any?] {
if let array = dictionary["items"] as? [Any] {
for object in array {
if let objectAsDictionary = object as? [String: Any?] {
if let objectWithKindAndVideoId = objectAsDictionary["id"] as? [String: String] {
if let videoId = objectWithKindAndVideoId["videoId"] {
//pass data to table cell
}
}
if let snippet = objectAsDictionary["snippet"] as? [String: Any] {
if let description = snippet["description"] {
//pass data to table cell
}
}
}
}
}
}
}
}
}
task.resume()
}
我尝试将值附加到实例变量,但没有成功。
抱歉代码乱七八糟,这是我第一次在 Swift 中使用 JSON。
首先从不将收到的JSON字典声明为[String:Any?]
。收到的字典值不能是 nil
.
声明自定义结构
Video
。struct Video { let videoId : String let description : String }
声明一个数据源数组。
var videos = [Video]()
将JSON解析到数组中并在主线程上重新加载table视图。
func callYouTubeAPIToGetAllVideos() { let url = URL(string: "https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=XYZ&maxResults=50&order=date&key=ABC") let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in if error != nil { print(error!) } else { do { if let dictionary = try JSONSerialization.jsonObject(with: data!) as? [String: Any], let array = dictionary["items"] as? [[String: Any]] { for object in array { if let objectWithKindAndVideoId = object["id"] as? [String: String], let snippet = object["snippet"] as? [String: Any] { let videoId = objectWithKindAndVideoId["videoId"] ?? "" let description = snippet["description"] as? String ?? "" let video = Video(videoId: videoId, description: description) self.videos.append(video) } } DispatchQueue.main.async { self.tableView.reloadData() } } } catch { print(error) } } } task.resume() }
在 cellForRow 中将值分配给文本属性
let video = videos[indexPath.row] cell.textLabel!.text = video.videoId cell.detailTextLabel?.text = video.description