I am trying to implement the uikit content unavailable when no items are in a table view. The configuration seems straight forward enough but I am confused where to place it so it shows when a table has no objects have been added.
I tried placing the configuration in my viewWillAppear both in a conditional and without one. When placed in the conditional no objects appear in the list when added. If I remove the conditional the objects are added but the content unavailable info is still there.
I also tried doing the same in the table view but got the same results.
This app is very simple and mostly for practice. I have a two viewcontrollers, the initial one should have the content unavailable. the second view controller has an array of objects presented in the tableview. if a user increments the stepper, those objects are then passed to the initial viewcontroller and the content unavailable should disappear. Any help on this is appreciated.
First view controller
class KitViewController: UIViewController {
let user = User()
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
initializeTable()
title = "Gear"
}
// initialize a new empty array of essential objects assigned to tableItems
var tableItems = [Essential]()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if tableItems.count == 0 {
var config = UIContentUnavailableConfiguration.empty()
config.image = UIImage(systemName: "figure.hiking")
config.text = "You don't have any gear"
config.secondaryText = "Add items and get hiking"
self.contentUnavailableConfiguration = config
} else {
// assign the users table items to table items
tableItems = User.userTableItems(user: user)
tableView.reloadData()
}
}
@IBSegueAction func plusButtonTapped(_ coder: NSCoder) -> ListViewController? {
return ListViewController(coder: coder, user: user)
}
func initializeTable() {
tableView.delegate = self
tableView.dataSource = self
}
}
extension KitViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// if the no table items are in the essential array no table cells will appear
return tableItems.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: K.categoryReuseID, for: indexPath)
// setup the table cell
let kitItem = tableItems[indexPath.row]
var content = cell.defaultContentConfiguration()
content.text = "(kitItem.count) (kitItem.itemName)"
cell.contentConfiguration = content
return cell
}
}
second view controller
import UIKit
class ListViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var addCustomButton: UIBarButtonItem!
// pass the user to the list view
var user: User
init(coder: NSCoder, user: User) {
self.user = user
super.init(coder: coder)!
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
tableView.register(UINib(nibName: K.cellNibName, bundle: nil), forCellReuseIdentifier: K.categoryReuseID)
title = "Select Gear"
}
extension ListViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// returns the users datasource essential objects array
return user.datasource.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: K.categoryReuseID, for: indexPath) as! MessageCell
let item = user.datasource[indexPath.row]
cell.item = item
cell.cellLabel.text = item.itemName
cell.cellStepper.value = Double(item.count)
cell.countLabel.text = String(item.count)
return cell
}
}