swifttextlabel

textLabel?.text displays only "Label" but not the actual data passed from previous Table View


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]
}

Solution

  • 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
        }
    }