Problem with DetailView


#1

Hello everyone,

I have problems sending the data to a detail page with Prepareforsegue.
I have a TableViewController and a DetailView.

Here is my code :

import UIKit
import RealmSwift

class Table1: UITableViewController {

    @IBOutlet var tablecontroller1: UITableView!
    
    let segueID = "seguedetail"
    
    let realm = try! Realm()
    var listtasks: Results<Task>!
    var cellId = "listTaskCell"
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        displayTasks()

        }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        if let listT = listtasks{
            return listT.count
        }
        return 0
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "listTaskCell", for: indexPath)

        // Configure the cell...
        let listT = listtasks[indexPath.row]
        
        cell.textLabel?.text = listT.taskId + " " + listT.name
        cell.detailTextLabel?.text = "\(listT.name.count) Tasks"

        return cell
    }
   
    **func displayTasks() {**
**        listtasks = realm.objects(Task.self)     **
**    }**

    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        performSegue(withIdentifier: segueID, sender: listtasks[indexPath.row])
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == segueID {
            print(segueID)
            if let vueDeDetail = segue.destination as? DetailView {
                vueDeDetail.taskRecue = sender as? Task
            } else {
                print("error")
            }
        }
    }
    
}

Detail Page :

import UIKit
import RealmSwift

class DetailView: UIViewController {
    
    
    @IBOutlet weak var labelID: UILabel!
    
    @IBOutlet weak var labelNom: UILabel!
    
    @IBOutlet weak var labelStatut: UISwitch!
    
    
    var taskRecue: Task?

    
    override func viewDidLoad() {
        super.viewDidLoad()

        guard let task = taskRecue else {
            return
        }
        
        labelID.text = task.taskId
        labelNom.text = task.name
        labelStatut.isOn = task.isCompleted
    
    }

And Task Class :

import Foundation
import RealmSwift

class Task: Object {
    
    @objc dynamic var name = ""
    @objc dynamic var createdAt = NSDate()
    @objc dynamic var notes = ""
    @objc dynamic var isCompleted = false
    @objc dynamic var taskId = UUID().uuidString
    
    override static func primaryKey() -> String {
        return "taskId"
    }
    
}

when I click on a cell no data is sent.

Can you help me please ?


#2

The question is a little ambiguous as there’s not enough information. So a few questions may lead to an answer.

Can we assume you’ve set up your dataSource and delegates for the tableView so the list is displaying properly in the table?

Does the segue occur?

When you say

when I click on a cell no data is sent.

Does that mean no data, or nil data? You’re unwrapping optionals and not catching them if they are nil. Use this as a test

if let vueDeDetail = segue.destination as? DetailView {
    guard let myTask = sender as? Task else {
        print("task was nil in the master view, buddy")
        return
    }
    vueDeDetail.taskRecue = myTask

In the viewDidLoad function in the detail view what happens here?

override func viewDidLoad() {
    super.viewDidLoad()

    guard let task = taskRecue else {
        print("got to the detailView but taskRecue was nil") //add this
        return
    }

#3

Thank you for your help.

Can we assume you’ve set up your dataSource and delegates for the tableView so the list is displaying properly in the table?

Does the segue occur?

Yes.
The task list is displayed and the Detail Views opens when I click on a cell.

In the viewDidLoad function in the detail view what happens here?
The test gives this message : “got to the detailView but taskRecue was nil”


#4

Good evening,

Your remarks allowed me to understand that I had to review my way of doing things.

I recreated a controller but this time I chose “Embed in TabBarController” (yet in my course it is asked to create a Controller with “Embed in NavigationController”) and it works perfectly!