Swift 集合视图总是 return 来自模型的第一个默认值
Swift Collection View always return first default value from Model
我有一个包含 struct 和 array 的模型。 Form Hit struct ,我想像这样 tagLabel.text = photoviewModel?.hits.tags
访问标签 属性 ,这样我就可以将 属性 的值显示到标签中。 例如,当我单击第一个集合视图单元格时,它应该 return 图像和标签正确,但问题是我得到了正确的图像,但总是 return 相同的标签值。 截图添加到下面..
这是模型。
import Foundation
struct Photo: Codable {
let total, totalHits: Int
let hits: [Hit]
}
struct Hit: Codable {
let id: Int
let pageURL: String
let type, tags: String
let previewURL: String
let previewWidth, previewHeight: Int
let webformatURL: String
let webformatWidth, webformatHeight: Int
let largeImageURL: String
let imageWidth, imageHeight, imageSize, views: Int
let downloads, collections, likes, comments: Int
let userID: Int
let user: String
let userImageURL: String
enum CodingKeys: String, CodingKey {
case id, pageURL, type, tags, previewURL, previewWidth, previewHeight, webformatURL, webformatWidth, webformatHeight, largeImageURL, imageWidth, imageHeight, imageSize, views, downloads, collections, likes, comments
case userID = "user_id"
case user, userImageURL
}
}
这是视图模型。
import Foundation
class PhotoViewModel {
private let networkManager: NetworkManagerProtocol
var hits = [Hit]()
private var cache = [String: Data]()
// var net = NetworkManager()
weak var delegate: PhotoViewable?
init(networkManager: NetworkManagerProtocol) {
self.networkManager = networkManager
}
var rows: Int {
return hits.count
}
func fecthPhotoRecord(){
let networkUrl = NetworkURLs.baseURL
networkManager.getModel(Photo.self, from: networkUrl) { [weak self] result in
switch result{
case.success(let photo):
self?.hits = photo.hits
self?.delegate?.refreshUI()
case.failure(let error):
print(error)
self?.delegate?.showError()
}
}
}
func downloadImage(row: Int, completion: @escaping (Data) -> Void) {
let hit = hits[row]
let hitpath = hit.previewURL
if let data = cache[hitpath] {
completion(data)
return
}
networkManager
.getData(from: hitpath) { result in
switch result {
case .success(let data):
self.cache[hitpath] = data
DispatchQueue.main.async {
completion(data)
}
case .failure(let error):
print(error)
}
}
}
这是视图控制器中的代码。
import UIKit
class PhtotoDetailsViewController: UIViewController {
var photoviewModel : PhotoViewModel?
var peopleDetailsViewModel:PeopleDetailsViewModel?
private var stackView: UIStackView = {
let stackView = UIStackView()
stackView.axis = .vertical
stackView.distribution = .fill
stackView.alignment = .fill
stackView.spacing = 5
stackView.translatesAutoresizingMaskIntoConstraints = false
return stackView
}()
private let imageView: UIImageView = {
let imageView = UIImageView(frame: .zero)
imageView.contentMode = .scaleAspectFill
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()
private let tagLabel: UILabel = {
let label = UILabel(frame: .zero)
label.textAlignment = .center
label.numberOfLines = 0
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
view.addSubview(stackView)
view.addSubview(tagLabel)
view.addSubview(imageView)
setUpUI()
setPhoto()
setContrain()
}
private func setContrain(){
NSLayoutConstraint.activate([
stackView.topAnchor.constraint(equalTo: view.topAnchor,constant: 100),
stackView.bottomAnchor.constraint(equalTo: view.bottomAnchor,constant: -200),
stackView.leftAnchor.constraint(equalTo: view.leftAnchor , constant: 10),
stackView.rightAnchor.constraint(equalTo: view.rightAnchor,constant: 10),
stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor )
])
stackView.addArrangedSubview(imageView)
stackView.addArrangedSubview(tagLabel)
}
private func setUpUI(){
tagLabel.text = photoviewModel?.hits.first?.tags
}
var rowSelected = 0
private func setPhoto(){
photoviewModel?.downloadImage(row: rowSelected) { [weak self] data in
DispatchQueue.main.async {
let image = UIImage(data: data)
self?.imageView.image = image
}
}
}
}
我在这条线上有问题..
private func setUpUI(){
tagLabel.text = photoviewModel?.hits.first?.tags
}
这是应用首次加载时的屏幕截图,它有图像和标签 属性 ...
当我单击第三个单元格表单集合时它具有正确的图像但正确的标签属性。
在你的 setupUI()
中用这个
替换行 tagLabel.text = photoviewModel?.hits.first?.tags
tagLabel.text = photoviewModel?.hits[rowSelected].tags
我有一个包含 struct 和 array 的模型。 Form Hit struct ,我想像这样 tagLabel.text = photoviewModel?.hits.tags
访问标签 属性 ,这样我就可以将 属性 的值显示到标签中。 例如,当我单击第一个集合视图单元格时,它应该 return 图像和标签正确,但问题是我得到了正确的图像,但总是 return 相同的标签值。 截图添加到下面..
这是模型。
import Foundation
struct Photo: Codable {
let total, totalHits: Int
let hits: [Hit]
}
struct Hit: Codable {
let id: Int
let pageURL: String
let type, tags: String
let previewURL: String
let previewWidth, previewHeight: Int
let webformatURL: String
let webformatWidth, webformatHeight: Int
let largeImageURL: String
let imageWidth, imageHeight, imageSize, views: Int
let downloads, collections, likes, comments: Int
let userID: Int
let user: String
let userImageURL: String
enum CodingKeys: String, CodingKey {
case id, pageURL, type, tags, previewURL, previewWidth, previewHeight, webformatURL, webformatWidth, webformatHeight, largeImageURL, imageWidth, imageHeight, imageSize, views, downloads, collections, likes, comments
case userID = "user_id"
case user, userImageURL
}
}
这是视图模型。
import Foundation
class PhotoViewModel {
private let networkManager: NetworkManagerProtocol
var hits = [Hit]()
private var cache = [String: Data]()
// var net = NetworkManager()
weak var delegate: PhotoViewable?
init(networkManager: NetworkManagerProtocol) {
self.networkManager = networkManager
}
var rows: Int {
return hits.count
}
func fecthPhotoRecord(){
let networkUrl = NetworkURLs.baseURL
networkManager.getModel(Photo.self, from: networkUrl) { [weak self] result in
switch result{
case.success(let photo):
self?.hits = photo.hits
self?.delegate?.refreshUI()
case.failure(let error):
print(error)
self?.delegate?.showError()
}
}
}
func downloadImage(row: Int, completion: @escaping (Data) -> Void) {
let hit = hits[row]
let hitpath = hit.previewURL
if let data = cache[hitpath] {
completion(data)
return
}
networkManager
.getData(from: hitpath) { result in
switch result {
case .success(let data):
self.cache[hitpath] = data
DispatchQueue.main.async {
completion(data)
}
case .failure(let error):
print(error)
}
}
}
这是视图控制器中的代码。
import UIKit
class PhtotoDetailsViewController: UIViewController {
var photoviewModel : PhotoViewModel?
var peopleDetailsViewModel:PeopleDetailsViewModel?
private var stackView: UIStackView = {
let stackView = UIStackView()
stackView.axis = .vertical
stackView.distribution = .fill
stackView.alignment = .fill
stackView.spacing = 5
stackView.translatesAutoresizingMaskIntoConstraints = false
return stackView
}()
private let imageView: UIImageView = {
let imageView = UIImageView(frame: .zero)
imageView.contentMode = .scaleAspectFill
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()
private let tagLabel: UILabel = {
let label = UILabel(frame: .zero)
label.textAlignment = .center
label.numberOfLines = 0
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
view.addSubview(stackView)
view.addSubview(tagLabel)
view.addSubview(imageView)
setUpUI()
setPhoto()
setContrain()
}
private func setContrain(){
NSLayoutConstraint.activate([
stackView.topAnchor.constraint(equalTo: view.topAnchor,constant: 100),
stackView.bottomAnchor.constraint(equalTo: view.bottomAnchor,constant: -200),
stackView.leftAnchor.constraint(equalTo: view.leftAnchor , constant: 10),
stackView.rightAnchor.constraint(equalTo: view.rightAnchor,constant: 10),
stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor )
])
stackView.addArrangedSubview(imageView)
stackView.addArrangedSubview(tagLabel)
}
private func setUpUI(){
tagLabel.text = photoviewModel?.hits.first?.tags
}
var rowSelected = 0
private func setPhoto(){
photoviewModel?.downloadImage(row: rowSelected) { [weak self] data in
DispatchQueue.main.async {
let image = UIImage(data: data)
self?.imageView.image = image
}
}
}
}
我在这条线上有问题..
private func setUpUI(){
tagLabel.text = photoviewModel?.hits.first?.tags
}
这是应用首次加载时的屏幕截图,它有图像和标签 属性 ...
当我单击第三个单元格表单集合时它具有正确的图像但正确的标签属性。
在你的 setupUI()
中用这个
tagLabel.text = photoviewModel?.hits.first?.tags
tagLabel.text = photoviewModel?.hits[rowSelected].tags