-2

I need to add cell identifier to make ReusableCell for tableView. However I don't see any cell in the tableView properties and table view hierarchical. how to add a cell in the table view .

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

enter image description here

code here :

class SuggestNearTableViewCollectionViewCell: UICollectionViewCell , UITableViewDataSource,UITableViewDelegate{

    @IBOutlet weak  var suggestTableView : UITableView!


    override func awakeFromNib() {
        super.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
    }

}

2 Answers2

0

Inside tableViewController in viewDidLoad you should register it like this:

tableView.registerNib(UINib(nibName: "CustomOneCell", bundle: nil), forCellReuseIdentifier: "CustomCellOne")

And inside cellForRowAtIndexPath just declare cell:

let cell: CustomOneCell = tableView.dequeueReusableCellWithIdentifier("CustomCellOne", forIndexPath: indexPath) as! CustomOneCell

If you are not doing it inside tableViewController then you gotta connect your tableView and delegate:

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!

    ...

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        // Table view delegate
        self.tableView.delegate = self
        self.tableView.dataSource = self

        ...
Tarvo Mäesepp
  • 4,477
  • 3
  • 44
  • 92
0

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.

Umair Afzal
  • 4,947
  • 5
  • 25
  • 50