0

What I want to achieve: I want to click on one of the rows in my first view controller to move to another table view controller.

The problem: When I click the row, the view only shows "Label" and not the data I intend to pass through. Granted the application does not crash and the white background with the "Label" heading shows up, the data is still not being shown on the 2nd view controller.

What I have done so ffar: I have used a Struct: PageTwoItems to define the data I want to send to the 2nd View Controller.

import Foundation
import UIKit

The code for the second view controller is as follows:

class PageTwoTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!

var PageTwo = [String]()

override func viewDidLoad() {
    super.viewDidLoad()
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return PageTwo.count
}

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let Cell = self.tableView.dequeueReusableCellWithIdentifier("secondcell", forIndexPath: indexPath) as UITableViewCell

   Cell.textLabel?.text = PageTwo[indexPath.row]

    return Cell
}

}

The code for the first view controller is as follows:

@IBOutlet weak var tableView: UITableView!

let names = ["Cleaning", "Plumbing","Electrical", "Craftswork", "Automotive"]

let desc = ["Get your spotless home or office space", "Drains, Pipes, Faucets and more", "Lighting, Fans, AC's and more", "Installation, Assembly and more", "Tow Truck Hire, Tyre Replacement and more"]
let images = [UIImage(named:"pug"),UIImage(named:"pug2"),UIImage(named:"pug3"),UIImage(named:"pug4"),UIImage(named:"pug5")]

var PageTwo = [PageTwoItems]()


override func viewDidLoad() {

    PageTwo = [PageTwoItems(nametwo:["Home Cleaning", "Office Cleaning", "Moving In/Out Cleaning"], summarytwo:["Let your home sparkle","Office space cleaning right at your fingertips","New Home or Old Home? We've got you covered"],phototwo:["","",""]),
        PageTwoItems(nametwo:["Drains, Pipes & Faucets", "Showers and Bath Tubs", "Toilet and Wash Basin", "Water Heater"], summarytwo:["Fix Your Broken Pipes, Clogged Drains and Leaky Faucets","Showers and Bath Tubs working just right"," ", " "],phototwo:["","",""]),
        PageTwoItems(nametwo:["Lighting Fixtures", "Air Conditioners & Fans", "Generators"], summarytwo:["..","..",".."],phototwo:["","",""]),
        PageTwoItems(nametwo:["Furniture Assembly/Installation", "Interior Painting", "Doors, Windows & Curtains"], summarytwo:["..","...","..."],phototwo:["","",""]),
        PageTwoItems(nametwo:["Tow Truck Hire", "Tyre/Vulcanizer Help", "Auto-Consultant"], summarytwo:["...","...","..."],phototwo:["","",""])]



    super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 5

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = self.tableView.dequeueReusableCellWithIdentifier("newcell", forIndexPath:indexPath) as!CustomCell

    cell.photo.image = images[indexPath.row]
    cell.summary.text = desc[indexPath.row]
    cell.name.text = names[indexPath.row]

    return cell
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow!

    let DestViewController = segue.destinationViewController as! PageTwoTableViewController

    var PageTwoArrayTwo : PageTwoItems

    PageTwoArrayTwo = PageTwo[indexPath.row]

    DestViewController.PageTwo = PageTwoArrayTwo.nametwo
    DestViewController.PageTwo = PageTwoArrayTwo.summarytwo
    DestViewController.PageTwo = PageTwoArrayTwo.phototwo

}

}

The struct for the Page Two Items: 

struct PageTwoItems {
    var nametwo : [String]
    var summarytwo : [String]
    var phototwo : [String]
}
Mike F
  • 1,224
  • 4
  • 26
  • 44
  • Did you set the delegate and the data source of your tableview in the 2nd controller ? If yes did you check the value of PageTwo[indexPath.row] ? – Ro22e0 Mar 21 '16 at 13:14
  • @Ro22e0 yes I have set the delegate and data Source of the tableview in the 2nd controller. I'm relatively new to swift coding bro, how may I check for the value of PageTwo[indexPath.row] ? – Bobby Nwokonneya Mar 21 '16 at 13:23

2 Answers2

0

I think the segue you use is directly connected to tableview cell to your second view controller.

You should connect your segue between your 2 controllers and set an identifier to the attributes inspector like showSecondControllerSegue and use this method :

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    performSegueWithIdentifier(identifier: "showSecondControllerSegue", sender: self)
}

Edit :-

Your code should be :

For the second view controller :

class PageTwoTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!

var pageTwo = [String]()

override func viewDidLoad() {
    super.viewDidLoad()
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return pageTwo.count
}

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let Cell = self.tableView.dequeueReusableCellWithIdentifier("secondcell", forIndexPath: indexPath) as UITableViewCell

   Cell.textLabel?.text = pageTwo[indexPath.row]

    return Cell
}
}

For the first view controller :

@IBOutlet weak var tableView: UITableView!

let names = ["Cleaning", "Plumbing","Electrical", "Craftswork", "Automotive"]

let desc = ["Get your spotless home or office space", "Drains, Pipes, Faucets and more", "Lighting, Fans, AC's and more", "Installation, Assembly and more", "Tow Truck Hire, Tyre Replacement and more"]
let images = [UIImage(named:"pug"),UIImage(named:"pug2"),UIImage(named:"pug3"),UIImage(named:"pug4"),UIImage(named:"pug5")]

var pageTwo = [PageTwoItems]()


override func viewDidLoad() {
    super.viewDidLoad()

    pageTwo = [PageTwoItems(nametwo:["Home Cleaning", "Office Cleaning", "Moving In/Out Cleaning"], summarytwo:["Let your home sparkle","Office space cleaning right at your fingertips","New Home or Old Home? We've got you covered"],phototwo:["","",""]),
        PageTwoItems(nametwo:["Drains, Pipes & Faucets", "Showers and Bath Tubs", "Toilet and Wash Basin", "Water Heater"], summarytwo:["Fix Your Broken Pipes, Clogged Drains and Leaky Faucets","Showers and Bath Tubs working just right"," ", " "],phototwo:["","",""]),
        PageTwoItems(nametwo:["Lighting Fixtures", "Air Conditioners & Fans", "Generators"], summarytwo:["..","..",".."],phototwo:["","",""]),
        PageTwoItems(nametwo:["Furniture Assembly/Installation", "Interior Painting", "Doors, Windows & Curtains"], summarytwo:["..","...","..."],phototwo:["","",""]),
        PageTwoItems(nametwo:["Tow Truck Hire", "Tyre/Vulcanizer Help", "Auto-Consultant"], summarytwo:["...","...","..."],phototwo:["","",""])]    
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 5
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = self.tableView.dequeueReusableCellWithIdentifier("newcell", forIndexPath:indexPath) as!CustomCell

    cell.photo.image = images[indexPath.row]
    cell.summary.text = desc[indexPath.row]
    cell.name.text = names[indexPath.row]

    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    performSegueWithIdentifier(identifier: "showSecondControllerSegue", sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow!

    let DestViewController = segue.destinationViewController as! PageTwoTableViewController

    var pageTwoArrayTwo : PageTwoItems

    pageTwoArrayTwo = PageTwo[indexPath.row]

    DestViewController.pageTwo = pageTwoArrayTwo.nametwo
    DestViewController.pageTwo = pageTwoArrayTwo.summarytwo
    DestViewController.pageTwo = pageTwoArrayTwo.phototwo

}
Ro22e0
  • 364
  • 1
  • 10
0

I think your data isn't organized correctly. What I think you are looking for is something like this:

  1. Cleaning

    • Home cleaning
    • Office cleaning
    • Etc
  2. Plumbing

    • Drains, Pipes & Faucets
    • Showers and Bath Tubs
    • Etc
  3. Electrical

    • Lighting Fixtures
    • Air Conditioners & Fans
    • Generators

This is how I would create data structures to support this.

// This represents a single row on the detail screen
struct PageTwoItem {
    var name : String
    var summary : String
    var photo : String
}
// this represents a single row on the main screen
struct PageData {
    var name: String            // goes into the table on main screen
    var subitems: [PageTwoItem] // send this to the detail
}

class MasterViewController: UITableViewController {

    var detailViewController: DetailViewController? = nil
    // Make an array of PageData
    var objects = [PageData]()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        setupData()
    }


    func setupData() {
        objects = [
            PageData(name: "Cleaning", subitems: [
                PageTwoItem(name: "Cleaning 1", summary: "", photo: ""),
                PageTwoItem(name: "Cleaning 2", summary: "", photo: ""),
                PageTwoItem(name: "Cleaning 3", summary: "", photo: ""),
                PageTwoItem(name: "Cleaning 4", summary: "", photo: "")
                ] ),
            PageData(name: "Plumbing", subitems: [] ),
            PageData(name: "Electrical", subitems: [] ),
            PageData(name: "Craftswork", subitems: [] ),
            PageData(name: "Automotive", subitems: [] ),
        ]
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        print(segue.identifier)
        if segue.identifier == "showDetail" {
            if let indexPath = self.tableView.indexPathForSelectedRow {
                let object = objects[indexPath.row]
                let controller = segue.destinationViewController as! DetailViewController
                // tell the detail controller what we want her to show
                controller.detailItem = object
            }
        }
    }

    // MARK: - Table View

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return objects.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

        let object = objects[indexPath.row]
        cell.textLabel!.text = object.name
        return cell
    }


}

In the detail controller :-

import UIKit

class DetailViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var tableView: UITableView!
    // here is where we put the data we want to show
    var detailItem: PageData?

    // MARK: - Table View

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let detailItem = detailItem {
            print("count = \(detailItem.subitems.count)")
            return detailItem.subitems.count
        }
        return 0

    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("DetailCell", forIndexPath: indexPath)

        if let object = detailItem?.subitems[indexPath.row] {
            cell.textLabel!.text = object.name
        }
        return cell
    }
}
ryantxr
  • 4,119
  • 1
  • 11
  • 25
  • thanks man, but how can I pass the array of items without overwriting the vales? – Bobby Nwokonneya Mar 21 '16 at 14:09
  • What exactly do you want to show on the second screen? I know it is a table view. But what should it contain. Asked another way. What values from screen 1 do you want on screen 2? – ryantxr Mar 21 '16 at 14:49
  • I have the fields "Cleaning", "Plumbing","Electrical", "Craftswork", "Automotive" and when i click on one of the fields, I want it to lead to other fields depending on what you click, for example for "Cleaning" it should show "Home Cleaning", "Office Cleaning", "Moving In/Out Cleaning" for the first Label and a summary: "Let your home sparkle","Office space cleaning right at your fingertips","New Home or Old Home? We've got you covered" for each cleaning service and an image for each cleaning service. – Bobby Nwokonneya Mar 21 '16 at 14:59
  • I redid my answer. See if this makes sense to you. – ryantxr Mar 21 '16 at 16:18
  • @ryantxr Okay so i tweaked it and it doesn't crash anymore, thanks a lot!! – Bobby Nwokonneya Mar 21 '16 at 20:45