2016-06-11 18 views
0

Ich habe ein Problem mit Xcode und benutzerdefinierte TableView mit. Xib. Hör zu.Mehrere Ansichten mit Xib und TableView

Hauptsteuerung enthält drei Tasten, die mit drei getrennten Ansichten verbunden sind.

.xib-Datei heißt "vwTblCell", enthält eine benutzerdefinierte Tabellenansichtszelle mit dem Namen "cell".

TblCell.swift // Klasse mit Nib Methode

import UIKit 
class TblCell: UITableViewCell { 

@IBOutlet weak var imgJedzName: UIImageView! //Connected from .xib file 
@IBOutlet weak var lblJedzName: UILabel! //Connected from .xib file 

override func awakeFromNib() { 
    super.awakeFromNib() 
    // Initialization code 
} 

override func setSelected(selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 
} 
} 

Erste-View-Controller

import UIKit 
class SniadanieViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

@IBOutlet weak var tableView: UITableView! 

var tableData = ["Kanapeczka", "Jablko", "Jajecznica"] 

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.title="Sniadanie" 

    // Register custom cell 
    let nib = UINib(nibName: "vwTblCell", bundle: nil) 
    tableView.registerNib(nib, forCellReuseIdentifier: "cell") 
} 

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

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

    let cell: TblCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! TblCell 
    cell.lblJedzName.text = tableData[indexPath.row] 
    cell.imgJedzName.image = UIImage(named: tableData[indexPath.row])   
    return cell 

} 

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { 
    print("Row \(indexPath.row) selected") 
} 
override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 
} 

Es funktioniert! - Working view

Second-View-Controller - nur self.title ist anders als der erste Controller

import UIKit 
class ObiadViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

@IBOutlet weak var tableView: UITableView! 

var tableData = ["Kanapeczka", "Jablko", "Jajecznica"] 

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.title="Obiad" 

    // Register custom cell 
    let nib = UINib(nibName: "vwTblCell", bundle: nil) 
    tableView.registerNib(nib, forCellReuseIdentifier: "cell") 
} 

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

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

    let cell: TblCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! TblCell 
    cell.lblJedzName.text = tableData[indexPath.row] 
    cell.imgJedzName.image = UIImage(named: tableData[indexPath.row]) 
    return cell 

} 

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { 
    print("Row \(indexPath.row) selected") 
} 
override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 
} 

Leere erscheint Tableview:/I shouldn't be empty

Third-View-Controller

import UIKit 
class KolacjaViewController: UIViewController, UITableViewDataSource { 
override func viewDidLoad() { 
    super.viewDidLoad() 
    self.title="Kolacja" 
} 

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

} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = UITableViewCell() 
    cell.textLabel!.text = "Row number \(indexPath.row)" 
    cell.backgroundColor = UIColor.blueColor() 
    return cell 

} 

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { 
    print("Row \(indexPath.row) selected") 
} 

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

}}

Leere TabelleView App Ohren - wie im zweiten Controller. Du musst mir glauben, ich wünschte, ich könnte mehr Bilder hinzufügen, aber aufgrund von < 10 Rufpunkten kann ich nicht mehr als 2 Bilder hinzufügen.

Warum funktionieren der zweite und dritte Tabellenansicht-Controller nicht? Code scheint in Ordnung, ich denke, es gibt eine Art Regel, wenn .xib verwendet wird, von dem ich nichts weiß?

Antwort

0

Sie müssen Datenquelle setzen: tableView.dataSource = self, ich sehe das nicht in Code, den Sie gepostet haben.

+1

Man ... Ich habe keine Worte, wie ich Ihnen danken kann. Ich hoffe, dass Code-Gott dich sicher und gesund hält. Prost Kumpel! – Belz123PL