gpt4 book ai didi

ios - 来自url的swift 3 json无法获取单元格uitableview

转载 作者:行者123 更新时间:2023-11-30 13:05:22 24 4
gpt4 key购买 nike

来自 url 的 swift 3 json 无法获取单元格 uitableview 的问题

Episode.swift

    import Foundation

class Episode
{
var title: String?
var description: String?
var thumbnailURL: URL?
var createdAt: String?
var author: String?
var url: URL?
var episodes = [Episode]()

init(title: String, description: String, thumbnailURL: URL, createdAt: String, author: String)
{
self.title = title
self.description = description
self.thumbnailURL = thumbnailURL
self.createdAt = createdAt
self.author = author
}

init(espDictionary: [String : AnyObject])
{
self.title = espDictionary["title"] as? String

description = espDictionary["description"] as? String
thumbnailURL = URL(string: espDictionary["thumbnailURL"] as! String)
self.createdAt = espDictionary["pubDate"] as? String
self.author = espDictionary["author"] as? String
self.url = URL(string: espDictionary["link"] as! String)
}

static func downloadAllEpisodes() -> [Episode]
{
var episodes = [Episode]()
let url = URL(string:"http://pallive.xp3.biz/DucBlog.json")

URLSession.shared.dataTask(with: url!) { (data, response, error) in

if error != nil {
print(error)
return
}

else {

if let jsonData = data ,let jsonDictionary = NetworkService.parseJSONFromData(jsonData) {

let espDictionaries = jsonDictionary["episodes"] as! [[String : AnyObject]]

for espDictionary in espDictionaries {


let newEpisode = Episode(espDictionary: espDictionary)
episodes.append(newEpisode)

}
}
}
}

.resume()

return episodes
}
}

EpisodeTableViewCell.swift

import UIKit

class EpisodeTableViewCell: UITableViewCell
{
var episode: Episode! {
didSet {
self.updateUI()
print(episode)
}
}

func updateUI()
{
titleLabel.text = episode.title
print(episode.title)
authorImageView.image = UIImage(named: "duc")
descriptionLabel.text = episode.description
createdAtLabel.text = "yosri hadi | \(episode.createdAt!)"

let thumbnailURL = episode.thumbnailURL
let networkService = NetworkService(url: thumbnailURL!)
networkService.downloadImage { (imageData) in
let image = UIImage(data: imageData as Data)
DispatchQueue.main.async(execute: {
self.thumbnailImageView.image = image
})
}

authorImageView.layer.cornerRadius = authorImageView.bounds.width / 2.0
authorImageView.layer.masksToBounds = true
authorImageView.layer.borderColor = UIColor.white.cgColor
authorImageView.layer.borderWidth = 1.0
}

@IBOutlet weak var thumbnailImageView: UIImageView!
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var createdAtLabel: UILabel!
@IBOutlet weak var authorImageView: UIImageView!
}

EpisodesTableViewController.swift

import UIKit
import SafariServices

class EpisodesTableViewController: UITableViewController
{
var episodes = [Episode]()

override func viewDidLoad()
{
super.viewDidLoad()

episodes = Episode.downloadAllEpisodes()
print(Episode.downloadAllEpisodes())
self.tableView.reloadData()

tableView.estimatedRowHeight = tableView.rowHeight
tableView.rowHeight = UITableViewAutomaticDimension
tableView.separatorStyle = .none
}

override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int
{
return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return episodes.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "Episode Cell", for: indexPath) as! EpisodeTableViewCell
let episode = self.episodes[(indexPath as NSIndexPath).row]

cell.episode = episode
return cell
}

// MARK: - UITableViewDelegate

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath:
IndexPath)
{
let selectedEpisode = self.episodes[(indexPath as NSIndexPath).row]

// import SafariServices
let safariVC = SFSafariViewController(url: selectedEpisode.url! as URL)
safariVC.view.tintColor = UIColor(red: 248/255.0, green: 47/255.0, blue: 38/255.0, alpha: 1.0)
safariVC.delegate = self
self.present(safariVC, animated: true, completion: nil)
}

// MARK: - Target / Action

@IBAction func fullBlogDidTap(_ sender: AnyObject)
{
// import SafariServices
let safariVC = SFSafariViewController(url: URL(string: "http://www.ductran.io/blog")!)
safariVC.view.tintColor = UIColor(red: 248/255.0, green: 47/255.0, blue: 38/255.0, alpha: 1.0)
safariVC.delegate = self
self.present(safariVC, animated: true, completion: nil)
}
}

extension EpisodesTableViewController : SFSafariViewControllerDelegate
{
func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
controller.dismiss(animated: true, completion: nil)
}
}

为什么无法在应用程序的 UITableviewCell 中显示解析 json问题出在哪里。

最佳答案

您只需将 EpisodedownloadAllEpisodes 方法更改为这样的闭包即可。

static func downloadAllEpisodes(completion: ([Episode]) -> ()) {
var episodes = [Episode]()
let url = URL(string:"http://pallive.xp3.biz/DucBlog.json")
URLSession.shared.dataTask(with: url!) { (data, response, error) in

if error != nil {
print(error)
completion(episodes)
}
else {
if let jsonData = data ,let jsonDictionary = NetworkService.parseJSONFromData(jsonData) {
let espDictionaries = jsonDictionary["episodes"] as! [[String : AnyObject]]
for espDictionary in espDictionaries {

let newEpisode = Episode(espDictionary: espDictionary)
episodes.append(newEpisode)
}
}
completion(episodes)
}

}.resume()
}

现在像这样调用这个方法。

Episode.downloadAllEpisodes() {(episodes) -> () in
if episodes.count > 0 {
print(episodes)
self.episodes = episodes
self.tableView.reloadData()
}
}

关于ios - 来自url的swift 3 json无法获取单元格uitableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39453631/

24 4 0
文章推荐: Swift 3 第一个参数名称
文章推荐: c# - Excel 中的错误消息
文章推荐: javascript - 使用 Google Maps API v3 添加多个标记
文章推荐: javascript - jquery 移动转义页面
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com