I have a learning project I am working with using SQLite. I am able to create a database and insert rows, however in retrieving rows and wanting them to show up on the screen nothing appears. The section of code to view data on the screen is below. Any suggestions on how to fix?
}
return cellMultiline
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ContactCell")!
var label: UILabel?
label = cell.viewWithTag(1) as? UILabel // Name label
label?.text = contacts[indexPath.row].name
label = cell.viewWithTag(2) as? UILabel // Phone label
label?.text = contacts[indexPath.row].phone
}
Set breakpoints in your numberOfSections, numberOfRowsInSection and cellForRowAtIndexPath. Make sure those data source methods are all being called and that you’re returning the right values. For the code you’ve shown, step through it and make sure you’re finding the labels you expect using viewWithTag.
Incidentally digging into the view hierarchy like that is not a very safe way to do it. In my opinion it’s better to use a proper UITableViewCell subclass with public properties you can set. Then the view controller doesn’t have to know the implementation details of how the cell is showing the data. It just says “Here’s the data. Go do your thing.” And the cell is free to use a UILabel, text field, SwiftUI Text, custom image or whatever.
Incidentally digging into the view hierarchy like that is not a very safe way to do it. In my opinion it’s better to use a proper UITableViewCell subclass with public properties you can set. Then the view controller doesn’t have to know the implementation details of how the cell is showing the data. It just says “Here’s the data. Go do your thing.” And the cell is free to use a UILabel, text field, SwiftUI Text, custom image or whatever.