0
import Foundation
import UIKit

class ServiceLines: UIViewController{

    @IBOutlet weak var servicesTableView: UITableView!

    var ServiceA: [Service] = []
    var identities = [String]()

    override func viewDidLoad(){
        super.viewDidLoad()

        ServiceA = Services()
        identities = ["Service1","Service2","Service3"]

    }
    func Services() -> [Service]{

        var releaseServices: [Service] = []

        let Service1 = Service(titled: "Test1", description: "Test", image: #imageLiteral(resourceName: "Test"))
        let Service2 = Service(titled: "Test2", description: "Test", image: #imageLiteral(resourceName: "Test"))
        let Service3 = Service(titled: "Test3", description: "Test", image: #imageLiteral(resourceName: "Test"))

        releaseServices.append(Service1)
        releaseServices.append(Service2)
        releaseServices.append(Service3)

        return releaseServices
    }
}

extension ServiceLines: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return ServiceA.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let ServiceB = ServiceA[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier:"ServicesTableViewCell") as! ServicesTableViewCell
        cell.setServices(service: ServiceB)

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        func prepareForSegue(segue: UIStoryboardSegue, sender: Any) {

            let vcName = identities[indexPath.row]
            let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
            self.navigationController?.pushViewController(viewController!, animated: true)

        }

    }

}

the cell is filled with the correct data but when I click on a cell in the simulator it does not move to the corresponding view controller above is the code. also how can I move specific data that is filled inside of the cell such as text and image assets to the corresponding view controller (should I add it in each corresponding swift file for that specific view or is their an easier way)?

1 Answers1

0

First, you need to make sure your ViewController is set as a delegate for the tableView. Second, remove the prepareForSegue from the didSelectRow.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let vcName = identities[indexPath.row]
    let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
    // Here you can pass the data you need to the new ViewController
    let service = releaseServices[indexPath.row]
    viewController?.service = service 
    // or if you need only the image
    viewController?.serviceImage = service.image
    self.navigationController?.pushViewController(viewController!, animated: true)
}

I'm assuming there is a property called "service" or "image" in your viewController.

Peyotle
  • 2,255
  • 1
  • 15
  • 16
  • the tableview is set as a delegate for the viewController its placed in and I deleted prepareForSegue but it still does not show the alternate viewControllers when I attempt to press on a service. – Omar Al-Eisa Feb 04 '18 at 21:54
  • Is tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) fired when you press the cell? If it is, is self.navigationController non-nil in this function call? – Peyotle Feb 04 '18 at 21:58
  • how do I know if its fired or not? Yes a navigation controller is in place between the tab bar controller and the view controller with the tableview holding the prototype cell. and what is non-nil??? – Omar Al-Eisa Feb 04 '18 at 22:02
  • Use the debugger. Place a breakpoint inside the function. If execution stops on the breakpoint when you're pressing the cell then it was fired. At the same time, when debugger stops on the breakpoint, you can check if the property self.navigationController? is nil or not. Alternatively, if you have problems with debugging, you can temporarily place print(self.navigationController) inside the function. But I would recommend understanding debugging first. – Peyotle Feb 04 '18 at 22:08
  • the execution did not stop when I pressed the cell with the breakpoint in place on the (func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)) – Omar Al-Eisa Feb 04 '18 at 23:54
  • It means that the delegate is not set properly on the tableView. I can’t tell more based on the code you provided. Double check in the storyboard that the delegate is connected. – Peyotle Feb 05 '18 at 01:04
  • I got it the cell did not have selecting enabled – Omar Al-Eisa Feb 05 '18 at 03:48
  • I hope my answer was helpful. – Peyotle Feb 05 '18 at 07:10
  • it was helpful, you gave me an idea on how I can pass the data, I was having issues with that because of the init I setup under Service. Im having an issue with the cell selection though. when I select a cell and it brings me to the specified controller as I was intending, when I press the back button, the cell is still highlighted as if its selected until I select an alternate cell, then the same issue persists with the other selected cell. any idea on why this is occurring? – Omar Al-Eisa Feb 06 '18 at 19:23
  • It is an unrelated question. You can mark my answer as accepted and then start a new question. Check this answer and the comments below. There are a few different approaches: https://stackoverflow.com/a/1840757/3055378 – Peyotle Feb 06 '18 at 20:30