【发布时间】:2022-01-28 23:44:28
【问题描述】:
我有一个内部带有 CollectionView 的 ViewController 和一个带有 TableView 内部的 CollectionViewCell。当用户单击 TableViewCell 时,我想显示一个 ViewController 来显示用户任务的详细视图,但我在此行 let vc = MyTasksDetailController() 处收到此错误“调用中的参数 'coder' 缺少参数”。
这是我的代码:
ProfileController
final class ProfileController: UIViewController {
private var collectionView: UICollectionView?
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
layout.minimumLineSpacing = 1
layout.minimumInteritemSpacing = 1
layout.sectionInset = UIEdgeInsets(top: 0, left: 1, bottom: 0, right: 1)
let size = (view.width - 4)/3
layout.itemSize = CGSize(width: size, height: size)
collectionView = UICollectionView(frame: .zero,
collectionViewLayout: layout)
// Headers
collectionView?.register(ProfileInfoHeader.self,
forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader,
withReuseIdentifier: ProfileInfoHeader.identifier)
collectionView?.register(MyTasksCollectionCell.self,
forCellWithReuseIdentifier: MyTasksCollectionCell.identifier)
collectionView?.delegate = self
collectionView?.dataSource = self
guard let collectionView = collectionView else {
return
}
view.addSubview(collectionView)
}
extension ProfileController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MyTasksCollectionCell.identifier,
for: indexPath)as! MyTasksCollectionCell
return cell
}
MyTaskCollectionCell
class MyTasksCollectionCell: UICollectionViewCell, UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let task = {() -> Add in
switch (displayedTask) {
case .current:
// First segment tapped
return self.tasks[indexPath.row]
case past:
// Second segment tapped
return self.pastTasks[indexPath.row]
}
}()
let vc = MyTasksDetailController() //ERROR HERE: Missing argument for parameter 'coder' in call : Insert 'coder: <#NSCoder#>'
self.present(vc, animated: true, completion: nil)
}
MyTaskTableCell
class MyPostsTableCell: UITableViewCell {
var setdescriptionTitleLabel: String? {
didSet {
descriptionTitleLabel.text = setdescriptionTitleLabel ?? ""
}
}
var setdateLabel: String? {
didSet {
dateLabel.text = setdateLabel ?? ""
}
}
var sethourLabel: String? {
didSet {
hourLabel.text = sethourLabel ?? ""
}
}
var setDateIcon: UIImage? {
didSet {
dateIcon.image = UIImage()
}
}
var setHourIcon: UIImage? {
didSet {
hourIcon.image = UIImage()
}
}
MyTasksDetailController
class MyTasksDetailController: UIViewController {
internal var task: Add? {
didSet {
if let task = task {
setDescriptionLabel = task.description
setDescriptionTitleLabel = task.descriptionTitle
}
}
}
var setDescriptionLabel: String? {
didSet {
descriptionLabel.text = setDescriptionLabel ?? ""
}
}
var setdescriptionTitleLabel: String? {
didSet {
descriptionTitleLabel.text = setdescriptionTitleLabel ?? ""
}
}
let descriptionLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.font = UIFont.systemFont(ofSize: 28, weight: .bold)
label.textAlignment = .center
return label
}()
let descriptionTitleLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.font = UIFont.systemFont(ofSize: 18, weight: .bold)
label.textAlignment = .center
label.numberOfLines = 3
return label
}()
let container: UIView = {
let v = UIView()
v.translatesAutoresizingMaskIntoConstraints = false
v.clipsToBounds = true
v.backgroundColor = .white
v.layer.cornerRadius = 24
v.backgroundColor =
// 1
UIColor { traitCollection in
// 2
switch traitCollection.userInterfaceStyle {
case .dark:
// 3
v.layer.borderColor = UIColor.label.cgColor
return UIColor.systemBackground
default:
// 4
v.layer.borderColor = UIColor.black.cgColor
return UIColor.systemBackground
}
}
return v
}()
lazy var stackContainer: UIStackView = {
let stackContainer = UIStackView(arrangedSubviews: [stackDesLabel, stackDesTitLabel])
stackContainer.translatesAutoresizingMaskIntoConstraints = false
stackContainer.axis = .vertical
stackContainer.distribution = UIStackView.Distribution.fillEqually
return stackContainer
}()
lazy var stackDesLabel: UIStackView = {
let stackDesLabel = UIStackView(arrangedSubviews: [descriptionLabel])
stackDesLabel.translatesAutoresizingMaskIntoConstraints = false
stackDesLabel.axis = .vertical
stackDesLabel.distribution = UIStackView.Distribution.fillProportionally
return stackDesLabel
}()
lazy var stackDesTitLabel: UIStackView = {
let stackDesTitLabel = UIStackView(arrangedSubviews: [descriptionTitleLabel])
stackDesTitLabel.translatesAutoresizingMaskIntoConstraints = false
stackDesTitLabel.axis = .horizontal
stackDesTitLabel.distribution = UIStackView.Distribution.fillEqually
return stackDesTitLabel
}()
override func viewDidLoad() {
view.addSubview(stackDesLabel)
view.addSubview(stackDesTitLabel)
stackContainer.leadingAnchor.constraint(equalTo: container.leadingAnchor).isActive = true
stackContainer.trailingAnchor.constraint(equalTo: container.trailingAnchor).isActive = true
stackContainer.centerYAnchor.constraint(equalTo: container.centerYAnchor).isActive = true
stackContainer.centerXAnchor.constraint(equalTo: container.centerXAnchor).isActive = true
stackContainer.heightAnchor.constraint(equalTo: container.heightAnchor).isActive = true
stackContainer.widthAnchor.constraint(equalTo: container.widthAnchor).isActive = true
stackDesTitLabel.topAnchor.constraint(equalTo: stackContainer.topAnchor, constant: 50).isActive = true
stackDesTitLabel.widthAnchor.constraint(equalTo: stackContainer.widthAnchor).isActive = true
stackDesTitLabel.centerXAnchor.constraint(equalTo: stackContainer.centerXAnchor).isActive = true
stackDesLabel.topAnchor.constraint(equalTo: stackDesTitLabel.bottomAnchor, constant: 50).isActive = true
stackDesLabel.leadingAnchor.constraint(equalTo: stackContainer.leadingAnchor, constant: 5).isActive = true
stackDesLabel.widthAnchor.constraint(equalTo: stackContainer.widthAnchor).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
添加(数据结构)
struct Add {
static var details: Add = Add()
var descriptionTitle: String = ""
var description: String = ""
var id: String?
var date: String = ""
var hour: String = ""
func getDict() -> [String: Any] {
let dict = [
"descriptionTitle": self.descriptionTitle,
"description": self.description,
"date": self.date,
"hour": self.hour,
] as [String : Any]
return dict
}
}
【问题讨论】:
-
你确定这是你所有的代码吗?我觉得你的
MyTasksDetailController不完整,你可能有一些我们看不到的自定义init。 -
嘿,这不是我删除了很多以便更清楚,我在
TasksDetailController中添加了更多内容
标签: swift inheritance uiviewcontroller initialization