i've got a problem with Xcode and custom TableView using .xib. Check it out.
Main controller contains three buttons connected with three separated views.
.xib file is named "vwTblCell", contains one custom Table View Cell named "cell".
TblCell.swift //class with Nib method
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)
}
}
First 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.
}
}
It works! - Working view
Second view controller - only self.title is different than first 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.
}
}
Empty tableView appears :/ 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.
}}
Empty tableView appears - same as in the second controller. You've got to believe me, i wish i could add more pictures but due to <10 reputation points i can't add more than 2 pictures.
Why the second and third table view controller are not working? Code seems fine, i guess there's some kind of rule when using .xib that i don't know about?