[Solved] How to detected nib file table view cell to make ReusableCell?


First of all go to File-> new -> Cocoa Touch Class and create class of UIViewCOntroller. Name your class accordingly.

enter image description here

Now you will have a Xib file and a Swift file. Xib would look something like this.

enter image description here

Now drag and drop a UITableView on the Xib and give it 4-pin constraints as top=0, bottom=0, leadin=0, and trailing=0. Now create an outlet of your tableView in your newly created swift file. Connect data Source and delegate as well.

Now again go to File->New-> Coucoa Touch Class and create a class for UItableViewCell also create a Xib file like below.

enter image description here

Now you will have a Xib for your cell and a swift file for your cell. Just design your cell as your need in this Xib. Lets say If you want to put an imageView or a label etc. Then create outlets of all components in swift file of your custom cell. Now add this function in swift file of your custom cell.

    class func cellForTableView(tableView: UITableView, atIndexPath indexPath: NSIndexPath) -> YourCustomTableViewCell {
    let kYourCustomTableViewCellIdentifier = "kYourCustomTableViewCellIdentifier"
    tableView.registerNib(UINib(nibName: "YourCustomTableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: kYourCustomTableViewCellIdentifier)

    let cell = tableView.dequeueReusableCellWithIdentifier(kYourCustomTableViewCellIdentifier, forIndexPath: indexPath) as! YourCustomTableViewCell

    return cell
}

Your custom cell is ready to use.

Now go to the swift file of your tableView and in your cellForRowAtIndexPath just use use this cell like below.

let cell = YourCustomTableViewCell.cellForTableView(tableView, atIndexPath: indexPath)
cell.myImageView.image = "something"
// Do something with your cell

I hope it would be helpfull. Let me know if you find any difficulty.

0

solved How to detected nib file table view cell to make ReusableCell?