coderLIfe coderLIfe - 1 month ago 15
Swift Question

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

I need to add cell identifier to make ReusableCell for

. However I don't see any
in the
properties and table view hierarchical. how to add a cell in the table view .

note : basically i want to create a
file which should contain a
and that
should have custom

enter image description here

code here :

class SuggestNearTableViewCollectionViewCell: UICollectionViewCell , UITableViewDataSource,UITableViewDelegate{

@IBOutlet weak var suggestTableView : UITableView!

override func awakeFromNib() {
// Initialization code
self.suggestTableView.dataSource = self
self.suggestTableView.delegate = self

suggestTableView.register(UINib(nibName: "SuggestNearTableViewCell", bundle: nil), forCellReuseIdentifier: "SuggestNearTableViewCell")

func numberOfSections(in tableView: UITableView) -> Int {
return 1
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SuggestNearTableViewCell", for: indexPath) as! SuggestNearTableViewCell
return cell



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.