2016-03-21 12 views
0

Was ich erreichen möchte: Ich möchte auf eine der Zeilen in meinem ersten View-Controller klicken, um zu einem anderen Tabellenansicht-Controller zu wechseln.textLabel? .text zeigt nur "Label" an, aber nicht die tatsächlichen Daten aus vorheriger Tabellenansicht

Das Problem: Wenn ich auf die Zeile klicke, zeigt die Ansicht nur "Label" an und nicht die Daten, die ich durchgehen möchte. Wenn die Anwendung nicht abstürzt und der weiße Hintergrund mit der Überschrift "Label" angezeigt wird, werden die Daten auf dem Controller der 2. Ansicht immer noch nicht angezeigt.

Was ich so getan habe ffar: Ich habe ein Struct: PageTwoItems verwendet, um die Daten zu definieren, die ich an den 2. View Controller senden möchte.

import Foundation 
import UIKit 

Der Code für die zweite View-Controller ist wie folgt:

@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 

} 

}

:

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 
} 

}

Der Code für den ersten View-Controller wie folgt ist

The struct for the Page Two Items: 

struct PageTwoItems { 
    var nametwo : [String] 
    var summarytwo : [String] 
    var phototwo : [String] 
} 
+0

Haben Sie die Delegierten und die Datenquelle Ihrer Tableview in der 2. Controller? Wenn ja, haben Sie den Wert von PageTwo [indexPath.row] überprüft? – Ro22e0

+0

@ Ro22e0 ja Ich habe den Delegaten und die Datenquelle der Tabellenansicht im 2.Controller eingestellt. Ich bin relativ neu bei der schnellen Codierung von Bro. Wie kann ich nach dem Wert von PageTwo [indexPath.row] suchen? –

Antwort

0

Ich denke, Ihre Daten werden nicht richtig organisiert.Was ich denke, Sie suchen etwas wie folgt aus:

  1. Reinigung

    • Wohnungsreinigung
    • Büroreinigung
    • Etc
  2. Klempner

    • Drains, Rohre & Hähne
    • Duschen und Badewannen
    • Etc
  3. Elektrische

    • Beleuchtung
    • Klimageräte & Fans
    • Generatoren

So würde ich Datenstrukturen erstellen, um dies zu unterstützen.

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


} 

Im Detail Regler: -

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 
    } 
} 
+0

danke Mann, aber wie kann ich das Array von Elementen übergeben, ohne die vales zu überschreiben? –

+0

Was genau möchten Sie auf dem zweiten Bildschirm zeigen? Ich weiß, dass es eine Tabellenansicht ist. Aber was soll es enthalten? Anders gefragt. Welche Werte von Bildschirm 1 möchten Sie auf Bildschirm 2? – ryantxr

+0

Ich habe die Felder "Reinigung", "Sanitär", "Elektrik", "Handwerk", "Automotive" und wenn ich auf eines der Felder klicke, möchte ich, dass es zu anderen Feldern führt, je nachdem, worauf du klickst Für "Reinigung" sollte "Home Cleaning", "Office Cleaning", "Moving In/Out Cleaning" für das erste Etikett und eine Zusammenfassung "Lass dein Haus funkeln", "Büroraumreinigung direkt an deinen Fingerspitzen", " New Home oder Old Home? Wir bieten Ihnen "für jeden Reinigungsservice und ein Bild für jeden Reinigungsservice. –

0

Ich denke, das von Ihnen verwendete Segment ist direkt mit der Tableview-Zelle Ihres zweiten View-Controllers verbunden.

Sie sollten Ihre segue zwischen 2-Controller verbinden und eine Kennung an den Attributen Inspektor wie showSecondControllerSegue gesetzt und diese Methode verwenden:

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

Edit: -

Ihr Code sein sollte:

Für die zweite Ansicht 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 
} 
} 

Zum ersten 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 

} 
+0

okay Ich habe gerade die Änderungen vorgenommen, aber das Klicken auf die Zeile führt jetzt zu keiner Aktion. Nur um klar zu sein, sollte die Methode, die Sie empfohlen haben, in der swift-Datei des 2. View-Controllers platziert werden, und zwischenzeitlich sollte das Segment ein Push sein, oder? –

+0

Bearbeiten. ist egal, die Art der Überleitung – Ro22e0

+0

Danke Bro, aber es hat nicht geholfen. Geht immer noch nicht zum nächsten View-Controller. –

Verwandte Themen