I want to use a Xib for use as a View for a UITableViewCell. But i'm unsure if i'm doing it the right way (i'm not sure if its dequeuing the cells)
Option #1 where i can call the method "test"
Option #1:
class MainMenuVC: UIViewController {
@IBOutlet weak var tableView: UITableView!
let nib = UINib(nibName: "MenuEntryRomaView", bundle: nil)
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerNib(nib, forCellReuseIdentifier: "cell") // Im guessing this line actually doesnt apply here...
}
@IBAction func test(sender: AnyObject) {
println("go!")
}
}
extension MainMenuVC: UITableViewDataSource {
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = nib.instantiateWithOwner(self, options: nil).first as MenuEntryView
cell.menuImageView.image = UIImage(named: "close_button")
cell.menuLabel.text = "Texto de menu"
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1000
}
}
However i found another solution in this post: Assigning the outlet for a UITableViewCell using UINib And could apply in my example like this:
Option 2
class MainMenuVC: UIViewController {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
let nib = UINib(nibName: "MenuEntryRomaView", bundle: nil)
self.tableView.registerNib(nib, forCellReuseIdentifier: "cell")
}
@IBAction func test(sender: AnyObject) {
println("go!")
}
}
extension MainMenuVC: UITableViewDataSource {
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell") as MenuEntryView
cell.menuImageView.image = UIImage(named: "close_button")
cell.menuLabel.text = "Texto de menu"
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1000
}
}
But im no setting the owner anywhere in this solution, and when i call the method "test" from the xib i get an error. I tried to do nib.instantiateWithOwner(self, options: nil) after instantianting the nib UINib(nibName..) but with no succeed. Notice i tried both code on the phone with 1k rows and i'm not experiencing any kind of lag (5s). My questions would be: in #option 1 its really dequeing the cells? and what can i do in option 2 to set the owner to MainMenuVC to be able to call the method test from the cells?
UPDATE Here is a link with a sample project i created, im trying to call from the button inside the cell a method inside the VC.