INITIAL GOAL :
有一个垂直定位的单元格列表视图,显示一些信息。用户单击单元格以显示包含更多信息的新视图。
道路如此之快(咖喱对我任性的儿子!):
结构:
struct Detail: Decodable {
let name: String?
let surname: String?
let address: String?
let description: String?
}
我使用以下数据进行测试(测试完成后,我将从API调用中获取此数据)。我将它放在 ViewController :
中let details: [Detail] = [Detail(name: "Chris", surname: "Doe", address: "Neverland 31", description: "This is a description about Chris Doe"), Detail(name: "Tony", surname: "Cross", address: "Galaxy Road 1", description: "This is a description about Tony Cross")]
使用上述信息和方法创建单元格:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
还有:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! Cell
由于该方法要求我们返回UICollectionViewCell,我首先通过执行以下操作将相关信息发送到Cell:
cell.details = details[indexPath.item]
return cell
在单元格内,我使用 didSet 创建了以下属性,以帮助我检索信息:
var details: Detail? {
didSet {
guard let details = details else { return }
guard let name = details.name else { return }
....
....
}
如您所知,使用来自ViewController的信息我动态构建了每个单元格。
此时一切都很好。
然后我尝试在单击单元格时显示详细视图。为此,我在方法中遵循相同的做法:
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let detailView = DetailView()
detailView.details = details[indexPath.item]
let detailViewController = DetailViewController()
detailViewController.modalTransitionStyle = .coverVertical
self.present(detailViewController, animated: true, completion: nil)
}
同样,在 DetailView 中,我使用相同的方法来获取与所选项目相关联的数据。这样我就可以访问用户选择的单元格数据,如下所示:
import UIKit
class DetailView: UIView {
var dismissDetailViewAction: (() -> Void)?
var details: Detail? {
didSet {
// get details
guard let details = details else { return }
guard let name = details.name else { return }
guard let surname = details.surname else { return }
guard let address = details.address else { return }
guard let description = details.description else { return }
// print description and it shows in the console but not in the view
print(description)
let attributedTextDescription = NSMutableAttributedString(string: description, attributes: [NSAttributedStringKey.font: UIFont.FontBook.AvertaRegular.of(size: 20), NSAttributedStringKey.foregroundColor: UIColor.white])
briefDescription.attributedText = attributedTextDescription
briefDescription.textAlignment = .center
briefDescription.textColor = .white
briefDescription.numberOfLines = 0
}
}
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not yet been implemented")
}
fileprivate func setupView() {
setupDescriptionText()
setupCloseButton()
}
let briefDescription: UITextView = {
let text = UITextView()
text.textColor = .red
return text
}()
let closeButton: UIButton = {
let button = UIButton(title: "Close", font: UIFont.FontBook.AvertaRegular.of(size: 18), textColor: .white, cornerRadius: 5)
button.backgroundColor = .black
button.addTarget(self, action: #selector(closeDetailView), for: .touchUpInside)
return button
}()
fileprivate func setupDescriptionText() {
self.addSubview(briefDescription)
briefDescription.translatesAutoresizingMaskIntoConstraints = false
briefDescription.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 5).isActive = true
briefDescription.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -5).isActive = true
briefDescription.topAnchor.constraint(equalTo: self.topAnchor, constant: 10).isActive = true
briefDescription.heightAnchor.constraint(equalToConstant: 300).isActive = true
}
fileprivate func setupCloseButton() {
self.addSubview(closeButton)
closeButton.translatesAutoresizingMaskIntoConstraints = false
closeButton.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
closeButton.bottomAnchor.constraint(equalTo: self.safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true
closeButton.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 40).isActive = true
closeButton.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -40).isActive = true
closeButton.heightAnchor.constraint(equalToConstant: 60).isActive = true
}
@objc func closeDetailView() {
dismissDetailViewAction?()
}
}
所以,我实际做的是设计didSet外部视图的静态部分,以及didSet中的动态部分。这适用于collectionView的单元格。
我使用 DetailViewController 显示 DetailView ,并在用户点击"关闭"按钮。
import UIKit
class DetailViewController: UIViewController {
// reference DetailView view
var detailView: DetailView!
override func viewDidLoad() {
super.viewDidLoad()
// setup view elements
setupView()
}
fileprivate func setupView() {
let mainView = DetailView(frame: self.view.frame)
self.detailView = mainView
self.view.addSubview(detailView)
self.homeDetailView.dismissDetailViewAction = dismissDetailView
// pin view
self.detailView.translatesAutoresizingMaskIntoConstraints = false
self.detailView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
self.detailView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
self.detailView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
self.detailView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
}
fileprivate func dismissDetailView() {
// dismiss current (DetailViewController) controller
self.dismiss(animated: true, completion: nil)
}
}
我这样做的原因是我希望尽可能保持我的ViewControllers干净(Massive View Controller,而不是我的东西)。
问题
整个构建没有任何问题,但是当我单击一个单元格转到DetailView时,没有显示任何信息。
THE WEIRD PART
在DetailView内部 - > didSet,当我使用print(name)时,它工作正常(你在控制台中看到正确的名字)。但是当我尝试在视图中使用该值时,它将不会显示。
我知道我的DetailView很好,因为如果我在其中使用硬编码值,它可以工作(你看到正确的结果)。
有什么建议为什么这不能正常工作?
PS:我正在以编程方式构建整个事物。没有故事板。
提前致谢并对丢失的帖子感到抱歉。
答案 0 :(得分:1)
如前所述,您detailView
未在detailViewController
内引用。相反,您在DetailView
内创建了另一个DetailViewController
实例,但其中没有Detail
。
控制台消息是从detailView
内部调用的,但detailViewController
内是另一个未调用此消息的实例,因为默认情况下Detail
设置为nil。
简而言之,要解决这个问题,您只需进行以下更改:
import UIKit
class DetailViewController: UIViewController {
var detail: Detail!
private lazy var detailView: DetailView = {
let mainView = DetailView(frame: self.view.frame)
mainView.details = detail
return mainView
}
override func viewDidLoad() {
super.viewDidLoad()
setupView()
}
fileprivate func setupView() {
self.view.addSubview(detailView)
self.homeDetailView.dismissDetailViewAction = dismissDetailView
// pin view
self.detailView.translatesAutoresizingMaskIntoConstraints = false
self.detailView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
self.detailView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
self.detailView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
self.detailView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
}
fileprivate func dismissDetailView() {
// dismiss current (DetailViewController) controller
self.dismiss(animated: true, completion: nil)
}
}
在collectionView(...)
func:
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let detailViewController = DetailViewController()
detailViewController.detail = details[indexPath.item]
detailViewController.modalTransitionStyle = .coverVertical
self.present(detailViewController, animated: true, completion: nil)
}
答案 1 :(得分:0)
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let detailView = DetailView()
detailView.details = details[indexPath.item]
let detailViewController = DetailViewController()
detailViewController.modalTransitionStyle = .coverVertical
self.present(detailViewController, animated: true, completion: nil)
}
你在这里制作DetailView
,将你的details
传递给它......然后不做任何事情。
通常DetailView
属于DetailViewController
的属性,您可以将details
传递给视图控制器,视图控制器会显示它。
这里发生的事情是,当您可能应该使用DetailView
拥有或应拥有的那个时,您正在创建,配置和丢弃DetailViewController
。