Where can I call register(_:forCellReuseIdentifier:) if I am not using a UITableViewController?

Currently I am placing a UITableView inside my UIViewController, with multiple other items. I am not using a UITableViewController.

However I am unable to register the identifier "masterlist", or should I say I am not sure where I can register it. I am not using storyboard, and if I am still to register it in my UIViewController's viewDidLoad, I am unable to figure out the proper syntax.

Is this something I can do?


class BeginNewCustomer: UIViewController {
    let myList = createTblView()

    override func viewDidLoad() {
        super.viewDidLoad()

        myList.delegate = self
        myList.dataSource = self

}

func createTblView() -> UITableView {
	let myTable = MyTableView(frame: CGRect(x: 0, y: 0, width: 0, height: 0 ))

	myTable.backgroundColor = .white
	return myTable
}

extension BeginNewInv: UITableViewDelegate, UITableViewDataSource  {
	
	func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
		return dbClass.invsList.count
	}


	func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
		let cell = UITableViewCell()
		
//		let cell = tableView.dequeueReusableCell(withIdentifier: "masterlist", for: indexPath)
		
        var config = cell.defaultContentConfiguration() 
		.... fill the configuration
		cell.contentConfiguration = config
			return cell
		}
}


Answered by Claude31 in 720273022

You should register cell in createTblView

Accepted Answer

You should register cell in createTblView

So now, when I create the table I am registering it as such

func createTblView() -> UITableView {
	let myTable = MyTableView(frame: CGRect(x: 0, y: 0, width: 0, height: 0 ))

	myTable.register(UITableViewCell.self, forCellReuseIdentifier: "reuse")
	myTable.backgroundColor = .white
	return myTable
}

And within: func tableView( tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell_

	func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//		let cell = UITableViewCell()
		
		let cell = tableView.dequeueReusableCell(withIdentifier: "reuse")
		var config = cell!.defaultContentConfiguration()
		... additional config code below
}

This is just a quick sample, obviously I have to add code because cell is now optional, "reuse" is just a test, etc.

When I look at cell in the debugger I do see:

_reuseIdentifier NSTaggedPointerString * "reuse" 0x84c7f8963e3e2abe

It all appears correct, but since the return cell is an optional, and it wasn't without using the tableView.dequeueReusableCell, just want to make sure I am implementing this correctly.

Thanks for all your help.

It works ? Good.

But forcing unwrap is always risky:

You'd better test:

guard var config = cell?.defaultContentConfiguration() else { return UITableViewCell() }

Question: why don't you use dequeue with indexPath .

        let cell = tableView.dequeueReusableCell(withIdentifier: "reuse", for: indexPath)
Where can I call register(_:forCellReuseIdentifier:) if I am not using a UITableViewController?
 
 
Q