3

Essentially I have a view controller called FirstViewController, this view controller contains a table view within it called listTableView.

I would like to tap on one of the cells in the table view listTableView and present whatever text was in the cell as the navigation controller title.

The navigation controller that appears when the cell is tapped is called showDetailsViewController.

How can this be done?

The following is what I have written in the FirstViewController

import UIKit
import AudioToolbox

class FirstViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, FeedModelProtocol  {
    var feedItems: NSArray = NSArray()
    var selectedStock : StockModel = StockModel()
    let tableView = UITableView()
    @IBOutlet weak var listTableView: UITableView!
    @IBOutlet weak var refreshButton: UIBarButtonItem!

    override func viewDidLoad() {
        super.viewDidLoad()
        //set delegates and initialize FeedModel


        self.listTableView.delegate = self
        self.listTableView.dataSource = self

        let feedModel = FeedModel()
        feedModel.delegate = self
        feedModel.downloadItems()

    }

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }

    @IBAction func reloadData(_ sender: Any) {

        print("reload pressed")
        listTableView.reloadData()
        viewDidLoad()
        _ = AudioServicesPlaySystemSound(1519)
    }



    func itemsDownloaded(items: NSArray) {

        feedItems = items
        self.listTableView.reloadData()
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // Return the number of feed items

        print("item feed loaded")
        return feedItems.count

    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        // Retrieve cell
        let cellIdentifier: String = "stockCell"
        let myCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
        myCell.textLabel?.textAlignment = .center
        myCell.textLabel?.font = .boldSystemFont(ofSize: 18)
        // Get the stock to be shown
        let item: StockModel = feedItems[indexPath.row] as! StockModel
        // Configure our cell title made up of name and price


        let titleStr = [item.customer].compactMap { $0 }.joined(separator: "-")


        print(titleStr)
        // Get references to labels of cell
        myCell.textLabel!.text = titleStr

        return myCell
    }

}

UPDATE:

What is the issue with this code:

NOTE: The restoration id of the tableview is scheduleTable

var homeworkIdentifierFromTableViewCell = ""
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        homeworkIdentifierFromTableViewCell = feedItems[indexPath.row].myCell
        self.performSegue(withIdentifier: "scheduleTable", sender: self)
        listTableView.deselectRow(at: indexPath, animated: true)

    }

UPDATE 2

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let item: StockModel = feedItems[indexPath.row] as! StockModel
    let titleStr = [item.customer].compactMap { $0 }.joined(separator: "-")
    print(titleStr)


}
  • set identifier for viewController on main Storyboard same like name on navigation title. And then instantiate controller with by passing title in identifier parameter. – Abu Ul Hassan Apr 19 '19 at 06:46

1 Answers1

0

You can use the didSelectRowAt to notice what cell was clicked and store what the text in the cell was (homeworkArray is the list of cells from a struct. Homeworkidentifier is a value in the struct).

var homeworkIdentifierFromTableViewCell = ""
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        homeworkIdentifierFromTableViewCell = homeworkArray[indexPath.row].homeworkIdentifier

        self.performSegue(withIdentifier: "homeworktoExpandHomework", sender: self)
        homeworkTableView.deselectRow(at: indexPath, animated: true)

    }

Then, you could use a prepare for a segue function to pass the text of the table view cell to the next view controller. You do this by creating a variable in the other view controller (the one that you are going to pass data to) and later accessing it from the other view controller and changing its value.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "reportBug" {
            let destinationViewController = segue.destination as! WebViewController
            destinationViewController.reason = "reportBug"

        }
        else if segue.identifier == "provideFeedback" {
            let destinationViewController = segue.destination as! WebViewController
            destinationViewController.reason = "provideFeedback"
        }
    }

Here is more about passing data between viewcontrollers : Passing data between View Controllers in Swift (From TableView to DetailViewController)

Hope this helps


EDIT: Here is the struct I am using :

struct homeworkTableViewCellData {
    let homeworkName : String!
    let className : String!
    let dateName : String!
    let colorImage : UIImage!
    let homeworkIdentifier : String!
}

I have initialized my homeworkArray with this struct. When I am calling a value from the cell, I am picking one from in the struct.

To set the table view with a struct is more organized. This is a good video that teaches you how to set it up (if you are want to do that) : https://www.youtube.com/watch?v=zAWO9rldyUE&list=LL--UalPCi7F16WzDFhMEg7w&index=20&t=921s

Rosalie W
  • 359
  • 4
  • 16
  • Rosalie I have a quick question, are you available? –  Apr 19 '19 at 03:29
  • I am available now – Rosalie W Apr 19 '19 at 03:31
  • I really appreciate your help, I don't have the most experience with Swift and cannot seem to get your code to work. Please take a look at the update I posted –  Apr 19 '19 at 03:41
  • I believe what the issue is right now is that myCell is the whole Cell right? What I am grabbing in my code (homeworkIdentifier) is one of the values in the struct of the cell. Are you using a struct? – Rosalie W Apr 19 '19 at 03:52
  • Yes myCell is the whole cell. How can I create a struct to identify the text in the cell? –  Apr 19 '19 at 04:01
  • You could check the video in the updated answer. Then, you could grab that value using the struct and save it in a variable. You would pass the value of the variable to the next view controller using the prepare for segue method :) Also, this Stack Overflow post might help you: https://stackoverflow.com/questions/31182847/how-to-detect-tableview-cell-touched-or-clicked-in-swift. – Rosalie W Apr 19 '19 at 04:02
  • Ok, I managed to turn the text from each row into a printable string value, using a slightly different method (take a look at my update)..can the value of "titleStr" be passed to the next viewController the same way? –  Apr 19 '19 at 04:32
  • When printing "titleStr" it prints the correct text value from whichever cell is selected –  Apr 19 '19 at 04:34
  • Yes then you can go ahead and pass that titleStr to another variable made in your other class using the prepare for segue method – Rosalie W Apr 19 '19 at 20:20