Ios 从';didset';JSON解码数组数据
我似乎无法设置下面的电视节目和缩略图图像。我正确地生成了数据数组,因为print(shows)语句正确地打印了所有项目。有人能看出我错在哪里吗。没有错误,但我的视图是空的,没有图像或标题。 这是TvController类Ios 从';didset';JSON解码数组数据,ios,json,swift,Ios,Json,Swift,我似乎无法设置下面的电视节目和缩略图图像。我正确地生成了数据数组,因为print(shows)语句正确地打印了所有项目。有人能看出我错在哪里吗。没有错误,但我的视图是空的,没有图像或标题。 这是TvController类 class TvController: UICollectionViewController, UICollectionViewDelegateFlowLayout { var tvShows: [RecentTvList]? = [] func fetchTvItems
class TvController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
var tvShows: [RecentTvList]? = []
func fetchTvItems() {
let jsonUrlString = "https://www.what-song.com/api/recent-tv?page=1"
guard let url = URL(string: jsonUrlString) else
{return}
URLSession.shared.dataTask(with: url) { (data, response, err) in
guard let data = data else {return}
do {
let shows = try
JSONDecoder().decode(RecentTvListData.self, from: data)
print(shows)
self.tvShows = shows.data
} catch let jsonErr {
print("Error serializing JSON", jsonErr)
}
}.resume()
override func viewDidLoad() {
super.viewDidLoad()
fetchTvItems()
navigationItem.title = "TV"
navigationController?.navigationBar.isTranslucent = false
collectionView?.backgroundColor = .white
collectionView?.register(TvCell.self, forCellWithReuseIdentifier: cellId)
collectionView?.register(Header.self, forSupplementaryViewOfKind:
UICollectionElementKindSectionHeader, withReuseIdentifier: headerId)
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if let count = tvShows?.count {
return count
}
return 0
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! TvCell
cell.televisionShow = tvShows?[indexPath.item]
return cell
}
}
以下是模型文件:
struct RecentTvListData: Decodable {
var data: [RecentTvList]
}
struct RecentTvList: Decodable {
var title: String?
var poster_url: String?
}
这是单元格的视图:所有视图/子视图/约束都设置正确,我只是没有费心包含代码,因为这会使所有这些都过于臃肿。我很确定问题出在模型或控制器上
var tvShowsController: TvController?
var televisionShow: RecentTvList? {
didSet {
titleLabel.text = televisionShow?.title
setupThumbnailImage()
thumbnailImageView.image = UIImage(named: (televisionShow?.poster_url)!)
}
}
let thumbnailImageView: UIImageView = {
let imageView = UIImageView(image: #imageLiteral(resourceName: "stranger things poster"))
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.layer.cornerRadius = 6
imageView.contentMode = .scaleAspectFill
imageView.layer.masksToBounds = true
return imageView
}()
let titleLabel: UILabel = {
let title = UILabel()
title.text = "Stranger Things"
title.font = UIFont(name: "Montserrat", size: 17)
title.translatesAutoresizingMaskIntoConstraints = false
return title
}()
下载数据(即URLSession.shared.dataTask(with:url)
)后面的块中的所有代码)是一种异步操作,这意味着iOS在不同的线程上运行该代码,在不同的线程上,iOS有尽可能多的时间完成该代码,并立即继续执行当前(本例中为main)线程。当您的集合视图加载并尝试显示其数据时,它尚未完成下载,因此没有数据可显示。下载完成后,您可以通过调用reloadData
,强制collection视图加载数据,如下所示:
URLSession.shared.dataTask(with: url) { (data, response, err) in
guard let data = data else {return}
do {
let shows = try
JSONDecoder().decode(RecentTvListData.self, from: data)
print(shows)
self.tvShows = shows.data
self.collectionView?.reloadData()
} catch let jsonErr {
print("Error serializing JSON", jsonErr)
}
}.resume()
它不是UIViewController。他是UICollectionViewController的子类。没有必要设置委托和数据源啊,谢谢,我忽略了这一点。我删除了我回答的那一部分啊,太谢谢你了!真的很感激,我已经想了好几个小时了