I will write about how to implement a custom cell using .xib when using UITableView. You can use xib to implement the description in calss of a custom cell based on Storyboard.
Change Label text in xib of UITableViewCell

Add a TableView to fill the screen.

Select Cocoa Touch Class and

Select UITableViewCell, check Also Create XIB file and create a file

Add Label to Cell and connect to Outlet

Outlet connect TableView and code and write general TableView code () in extension

What is different from the general TableView here is that Register is described.
ViewController.swift
tableView.dataSource = self
tableView.delegate = self
//Set xib as identifier in register
tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "TableViewCell")
The point is to use the identifier set here.
ViewController.swift
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
cell.titleLabel?.text = "aaa"
return cell
}
You should now be able to change the text of the UITableViewCell using xib as at the beginning. If you find it helpful, I would be grateful if you could give me LGTM!
Recommended Posts