I have a ViewController with a CollectionView inside and a CollectionViewCell with a TableView inside. When the user click on a TableViewCell i want to present a ViewController showing a detailed view of the user's task but i get this error "Missing argument for parameter 'coder' in call" at this line let vc = MyTasksDetailController()
.
Here's my code :
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")
}
Add(Data struct)
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
}
}