salman.oceans
salman.oceans

Reputation: 11

how to add xib file in table view cell?

i have create expandable table view in that tableview cell i have build xib file but i want to know that how i can twiddle for in loop of that xib file?

Custom Cell1

@IBOutlet weak var middleLabel: UILabel!
    @IBOutlet weak var leftLabel: UILabel!
    @IBOutlet weak var rightLabel: UILabel!


ViewController
let items = ["Item 1", "Item2", "Item3", "Item4"]

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.registerNib(UINib(nibName: "CustomOneCell", bundle: nil), forCellReuseIdentifier: "CustomCellOne")
    }

    // MARK: - UITableViewDataSource

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("CustomCellOne", forIndexPath: indexPath) as! CustomOneCell

        cell.middleLabel.text = items[indexPath.row]
        cell.leftLabel.text = items[indexPath.row]
        cell.rightLabel.text = items[indexPath.row]

        return cell
    }

}

Upvotes: 1

Views: 1924

Answers (3)

SE. Mahmoud
SE. Mahmoud

Reputation: 117

You can follow the following tut. easy and understandable

https://medium.com/@musawiralishah/creating-custom-uitableviewcell-using-nib-xib-files-in-xcode-9bee5824e722

Upvotes: 0

Rohit Makwana
Rohit Makwana

Reputation: 4905

Please try below code

class ViewController: UIViewController {

   @IBOutlet weak var tableView: UITableView!
   let items = ["Item 1", "Item2", "Item3", "Item4"]

   override func viewDidLoad() {
       super.viewDidLoad()

       self.tableView.register(UINib(nibName: "CustomOneCell", bundle: nil), forCellReuseIdentifier: "CustomOneCell")
       self.tableView.dataSource = self
       self.tableView.delegate   = self
  }

  //MARK:- UITableView DataSource & Delegate
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

      return self.items.count
  }

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "CustomOneCell") as! CustomOneCell

    cell.middleLabel.text = items[indexPath.row]
    cell.leftLabel.text = items[indexPath.row]
    cell.rightLabel.text = items[indexPath.row]

    return cell
  }

  func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 44.0 //give height you want
  }
}

Upvotes: 1

Codetard
Codetard

Reputation: 2605

I guess you have xib and custom UITableViewCell class, according to the information provided. This is what you need.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = Bundle.main.loadNibNamed("CellXib", owner: self, options: nil)?.first as! XibCustomCell
        return cell
}

Upvotes: 0

Related Questions