0

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.

http://www.ruiznadal.com/TestXibCell.zip

Community
  • 1
  • 1
Godfather
  • 4,040
  • 6
  • 43
  • 70
  • the __Option#2__ makes more sense to me, you don't need to keep the `nib` alive explicitly after you registered it for the `UITableView`, and you trust in it to present reusable cells for you. – holex Mar 11 '15 at 09:03
  • and how do i set the owner for the nib to be MainMenuVC which is this VC? cause with Option#2 which also makes more sense to me, i can't call test() from a UIButton inside the xib. – Godfather Mar 11 '15 at 09:04
  • Option#1 doesn't look like a cell dequeue. – Gaurav Singh Mar 11 '15 at 09:06
  • You can set the button action where you are creating your cell, not inside.. – iphonic Mar 11 '15 at 09:06
  • the cell's button's action should be handled inside the `nib`'s implementation, and you may notify other classes about such user interaction via e.g. a _delegate_, maybe. – holex Mar 11 '15 at 09:15
  • I'm not handling anything inside the nib's implementation, i just want to call a custom method inside the VC. The only way which i don like is setting the target of the button manually after dequeing the cell: cell.aButton.addTarget(<#target: AnyObject?#>, action: <#Selector#>, forControlEvents: <#UIControlEvents#>) – Godfather Mar 11 '15 at 09:23
  • @holex i think creating a action inside the nib implementation and creating a protocol just for calling a method for the owner is veeeery consufing since this button is just calling a function... i'll go adding setting the target after dequeuing the cell for the button to the VC – Godfather Mar 11 '15 at 09:50
  • 1
    @user588125, you just called the standard MVC architecture confusing? that does not sound good... – holex Mar 11 '15 at 10:10
  • @holex so basically the right way would be: creating a protocol inside the nib implementaion: "protocol CellView", adding a method inside the protocol: didClickedButton, adding an action inside the nib implemetation which notifies the delegate for didClickedButton, and then in the VC set the delegate to self, and implement the didClickedButton, right? – Godfather Mar 11 '15 at 10:13
  • 1
    @user588125, roughly, something like this would be an appropriate implementation. – holex Mar 11 '15 at 10:17
  • ok i'll follow this pattern, thank you! – Godfather Mar 11 '15 at 10:19

0 Answers0