Hello! Did anyone complete the Silver Challenge without using a title for the footer? The exercise seems to be pretty clear in that it wants us to complete it using a cell with the text “No items!”, though. I’m pretty close to solving it and will post my solution once I am done, but I am wondering what’s the approach others are taking. The way I am doing it is by using a new section without a title (that either shows or hides the contentView in the only cell it has) which seems to be the reasonable way to proceed as this was supposed to build on what we learned in the Bronze Challenge? Or is there an easier way that someone else might have thought of? Any comments would be greatly appreciated!
My solution involved creating a “No items!” cell that gets deleted when a first real new item is created, and created when the last real item is deleted as well as initially on viewDidLoad. I added a method in ItemStore to create a No Items! item, and check for the name in the various ItemsViewController methods.
Note that I took this as a separate challenge from the Bronze challenge, meaning that I started with the single section that the chapter ended with, and not the two from Bronze. I don’t think doing it based on Bronze and two sections would be much more difficult.
The only issue I’m having is that while I can prevent it from being deleted, I haven’t figured out how to prevent the swipe to Delete from appearing when swiping. Swiping doesn’t delete, and I disabled the Edit button when it’s the only item, but I’d like to keep it from appearing to be swiped as well.
Found it. I had to implement tableView( _ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool to prevent the No items! item from being editable at all. It was easy once I found it.
Way late on answering this but I like my solution. It works pretty well and uses an actual cell instead of a footer. I only changed ItemsViewController (see the modified parts below):
override func numberOfSections(in tableView: UITableView) -> Int {
return 2 // Section 0 for regular items, Section 1 for "No items!" cell
}
override func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return itemStore.allItems.count
} else {
return 1 // Just the No items! cell
}
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == 0 {
return tableView.rowHeight
} else {
if itemStore.allItems.isEmpty {
return tableView.rowHeight
} else {
return 0 // Hide the "No itmes!" cell when there are other items
}
}
}
override func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Get a new or recycled cell
let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell", for: indexPath)
if indexPath.section == 0 {
// Set the text on the cell with the description of the item
// that is at the nth index of items, where n = row this cell
// will appear in on the table view
let item = itemStore.allItems[indexPath.row]
cell.textLabel?.text = item.name
cell.detailTextLabel?.text = "$\(item.valueInDollars)"
} else {
cell.textLabel?.text = "No items!"
cell.detailTextLabel?.text = ""
}
return cell
}
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return indexPath.section == 0 ? true : false // Prevent "No items!" cell from being deleted/edited
}