(Note: I didn’t want to look at the other thread on this Challenge yet…)
I poured through the UITableViewDataSource documentation and the first I’ve discovered is that an .editingStyle for ‘Update’ doesn’t exist.
Because I didn’t see a way to add a button or caption to ‘Add as Favorites’ in the app’s Edit mode, my approach is to have the user Tap a cell to add it to favorites. The ideal result is a highlighted row with the additional ‘Favorites’ caption tacked on after the Item name.
Here’s what I’ve tried in ItemsViewController, which hasn’t yielded the desired results:
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 indexPath.section == 0 {
cell.textLabel?.text = itemStore.over50Items[indexPath.row].name
cell.detailTextLabel?.text = "$\(itemStore.over50Items[indexPath.row].valueInDollars)"
if cell.isHighlighted {
cell.textLabel?.text = "\(cell.textLabel?.text) is a Favorite!" // new for Gold Challenge
itemStore.over50Items[indexPath.row].isFavorite = true
}
return cell
} else {
cell.textLabel?.text = itemStore.under50Items[indexPath.row].name
cell.detailTextLabel?.text = "$\(itemStore.under50Items[indexPath.row].valueInDollars)"
if cell.isHighlighted {
cell.textLabel?.text = "\(cell.textLabel?.text) is a Favorite!" // new for Gold Challenge
itemStore.under50Items[indexPath.row].isFavorite = true
}
return cell
}
…
// Gold Challenge: implement editing within a row?
override func tableView(_ tableView: UITableView,
willBeginEditingRowAt indexPath: IndexPath) {
let addToFavorites = UIButton.init()
addToFavorites.titleLabel?.text = "Add to Favorites"
}
// Gold Challenge: set up another action after a tapping a cell?
@IBAction func addToFavorites (_ sender: UITableViewCell) {
isEditing = true
sender.setHighlighted(true, animated: true)
}
I also discovered that I can’t use an IBAction to connect the Cell to this function within the Storyboard. (addToFavorites doesn’t appear in the selection menu after I control-drag between Items View Controller and the TableViewCell).
A few questions to start:
- Why doesn’t ‘Update’ exist as an .editingStyle? There’s only none, delete, and insert.
- Can’t you associate/connect an IBAction to a UITableViewCell?
- I once tried adding a button control to the cell in Storyboard, but it will only stay center-aligned. Did anyone else trying this approach notice this?