Now, I can display ‘No Items’ within both sections by default.
I can also prevent a user from Deleting those rows. Even though the Delete button appears for these rows once I click Edit, clicking Delete doesn’t do anything.
Now it crashes when I click Add.
I know I need to delete the ‘No Items!’ row before I can add a new item from either of my Item arrays, but I’m either 1) not doing it correctly, and/or 2) not doing it in the right place.
(ItemsViewController.swift)
@IBAction func addNewItem(_ sender: UIButton) {
// Create a new item and add it to the store
let newItem = itemStore.createItem()
/* print("Number of sections: \(tableView.numberOfSections)")
for i in 0..<sections.count {
print("Section \(i): \(sections[i])")
}*/
// Silver challenge - can I use this to designate my 'No Items!' row?
let placeholderIndex = tableView.indexPathsForVisibleRows!
// Figure out where that item is in the array
if let index = itemStore.over50Items.firstIndex(of: newItem) {
// Silver challenge - trying to delete placeholder row before adding the first item from over50Items
tableView.deleteRows(at: placeholderIndex, with: .none)
let indexPath = IndexPath(row: index, section: 0)
// Insert this new row into the table
tableView.insertRows(at: [indexPath], with: .automatic)
print("Item Name: \(newItem.name)")
print("Value: $\(newItem.valueInDollars)")
// print("Section: \(newItem.overUnder50)")
} else if let index = itemStore.under50Items.firstIndex(of: newItem) {
// Silver challenge - trying to delete placeholder row before adding the first item from over50Items
tableView.deleteRows(at: placeholderIndex, with: .none)
let indexPath = IndexPath(row: index, section: 1)
tableView.insertRows(at: [indexPath], with: .automatic)
// console stuff
print("Item Name: \(newItem.name)")
print("Value: $\(newItem.valueInDollars)")
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Create an instance of UITableViewCell with default appearance
// let cell = UITableViewCell(style: .value1, reuseIdentifier: "UITableViewCell")
// better yet, get a new or recycled cell
let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell", for: indexPath)
// 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
// After the last reply, I first tried splitting my first if statement into two, checking whether each itemList is empty. If so, I set the cell text. But the text ONLY displayed after I clicked Add it added an item to the opposite section.
if itemStore.over50Items.isEmpty {
print ("over50Items is empty!") // this does not display
cell.textLabel?.text = "No Items!"
cell.detailTextLabel?.text = ""
print(indexPath) // indexPath = [0, 0]
return cell
} else {
tableView.deleteRows(at: [indexPath], with: .automatic) // not sure if this is doing anything
}
if itemStore.under50Items.isEmpty {
print ("under50Items is empty!") // this does not display
cell.textLabel?.text = "No Items!"
cell.detailTextLabel?.text = ""
print(indexPath) // indexPath = [1, 0]
return cell
} else {
tableView.deleteRows(at: [indexPath], with: .automatic) // not sure if this is doing anything
}
if indexPath.section == 0 {
cell.textLabel?.text = itemStore.over50Items[indexPath.row].name
cell.detailTextLabel?.text = "$\(itemStore.over50Items[indexPath.row].valueInDollars)"
return cell
} else {
cell.textLabel?.text = itemStore.under50Items[indexPath.row].name
cell.detailTextLabel?.text = "$\(itemStore.under50Items[indexPath.row].valueInDollars)"
return cell
}
}
// Listing 9.20 p 208 - Implementing table view row deletion
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
// If the table view is asking to commit a delete command...
if editingStyle == .delete {
// Silver challenge: apply remove/delete only when the itemList has items
if indexPath.section == 0 && !(itemStore.over50Items.isEmpty) {
// remove the item from the store
itemStore.removeItem(itemStore.over50Items[indexPath.row])
// Also remove that row from the table view with an animation
tableView.deleteRows(at: [indexPath], with: .automatic)
} else if indexPath.section == 1 && !(itemStore.under50Items.isEmpty){
itemStore.removeItem(itemStore.under50Items[indexPath.row])
// Also remove that row from the table view with an animation
tableView.deleteRows(at: [indexPath], with: .automatic)
} else {
_ = UITableViewCell.EditingStyle.none
// the Edit controls still appear, but nothing happens after I click Delete on the 'No Items' row.
}
}
}
// Silver Challenge addition to turn off editing
override func tableView(_ tableView: UITableView,
canEditRowAt indexPath: IndexPath) -> Bool {
let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell", for: indexPath)
if cell.textLabel?.text == "No Items!" {
return false
} else {
return true
}
// result: the Edit controls still appear, but nothing happens after I click Delete on the 'No Items' row.
}
Isn’t there a way to simply deleteRow() from the TableView where its label = ‘No Items!’?