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

I need to add a cell id to make a ReusableCell for tableView. However, I do not see cellin the tableView properties and hierarchical representation of the table . how to add a cell as a table.

note: Basically I want to create a file Xibthat should contain tableView, and what the tableViewuser should haveUiTableViewCell

enter image description here

the code is 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
source share
2 answers

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

enter image description here

Xib Swift. Xib .

enter image description here

UITableView Xib 4- top = 0, bottom = 0, leadin = 0 trailing = 0. . .

File- > New- > Coucoa Touch Class UItableViewCell, Xib , .

enter image description here

Xib . Xib. , View .. swift . .

    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
}

.

tableView cellForRowAtIndexPath , .

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

, . , - .

0

tableViewController viewDidLoad :

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

cellForRowAtIndexPath :

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

tableViewController, tableView :

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

        ...
0

Source: https://habr.com/ru/post/1666594/


All Articles