I'm new to iOS development and in my current project I have a shopping list table view controller / cell with an array of ShoppingList and a favourites table view controller / cell with an array of Favourites. What I'd like to do is, when the user hits the favouriteButton I'd like for that item to appear in the favourites table view controller array, Favourites. I've read a bunch of articles and can't seem to get anything to work. .append() doesn't seem to be an option unless I'm doing something totally wrong (which is a possibility). Help!
Here's what I have:
var shoppingItems: [ShoppingList] = [
ShoppingList(item: "Dummy Item", note: "Dummy Note")
]
class shoppingListTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return shoppingItems.count
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tableView.reloadData()
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "shoppingListCell", for: indexPath) as! ShoppingListTableViewCell
let shoppingList = shoppingItems[indexPath.row]
cell.update(with: shoppingList)
cell.showsReorderControl = true
return cell
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
shoppingItems.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .left)
}
}
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
let movedItem = shoppingItems.remove(at: fromIndexPath.row)
shoppingItems.insert(movedItem, at: to.row)
}
And this is the favouritesTableViewController:
var favouriteItems: [Favourites] = [
Favourites(favItem: "Dummy Item", favNote: "Dummy Note")
]
class FavouritesTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return favouriteItems.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "favouriteCell", for: indexPath) as! FavouritesTableViewCell
cell.showsReorderControl = true
return cell
}
@IBActions are in the shoppingListTableViewCell