Load a xib to my .swift

0

I'm trying to use my class called: TableViewCellPropia that extends to TableViewCell as cell prototype of my TableView . To my prototype cell, link it with a .xib so that it can be better graphed.

Images of the project:

How can I, in the following method, instantiate my prototype cell to use it?

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

    }
    
asked by MatiEzelQ 10.04.2016 в 02:50
source

1 answer

1

First of all, you need to relate the XIB of the cell, with the subclass of UITableViewCell to be able to instantiate it later. To do this, you have to do the following:

Next, you have to create the @IBOutlets needed to access the UILabel and the UIButton that the custom cell has.

class CeldaPersonalizadaCell: UITableViewCell {

    // MARK: - Properties
    @IBOutlet weak var leftLabel: UILabel!
    @IBOutlet weak var rightButton: UIButton!

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

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

        // Configure the view for the selected state
    }

}

Once all these steps have been done, you can now instantiate the cell in the UIViewController where you have the UITableView . First of all, you have to register the XIB :

// Registramos la celda en el tableView
let nib = UINib(nibName: "CeldaPersonalizadaCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "customCell")

And then, in the delegate method, you can instantiate the cell:

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

    let cell = tableView.dequeueReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as! CeldaPersonalizadaCell

    // Configuración de la celda

    return cell

}

Finally, remember to assign the corresponding delegates of UITableView such that UITableViewDataSource and UITableViewDelegate

    
answered by 10.04.2016 / 10:50
source