0

I have an app whereby a user can add books to a favourites list, by populating a tableview. Details on the book are displayed in a view and by tapping a 'favourites', a segue is performed inputting info on that book into a tableview cell.

At present only one book can appear in the table at a time adding a new book will remove the initial entry (so in effect only the first cell of the tableview is ever used)

Is there a way to save each entry in the tableview so in effect a list of favourites is created

saveButton

 @IBAction func saveButton(_ sender: Any) {

        let bookFormat = formatLabel.text

        if (bookFormat!.isEmpty)
        {
            displayMyAlertMessage(userMessage: "Please Select a Book Format")
            return
        }
        else{

        self.performSegue(withIdentifier: "LibViewSegue", sender: self)
        }


    }

TableView

extension LibrarybookViewController: UITableViewDataSource, UITableViewDelegate{

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 115
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print(#function, dataSource.count)

        return dataSource.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->  UITableViewCell {
        print(#function, "indexPath", indexPath)
        guard let bookCell = tableView.dequeueReusableCell(withIdentifier: "libCell", for: indexPath) as? LibrarybookTableViewCell else {
            return UITableViewCell()
        }
        let libbook = dataSource[indexPath.row]




        bookCell.cellTitleLabel.text = libbook.title
        bookCell.cellReleaseLabel.text = libbook.release
        bookCell.cellFormatLabel.text = bookFormat




        return bookCell
    }

I have been reading about defaults and CoreData but I'm not sure whether this should be implemented within the segue button action or within the tableview functions?

sandpat
  • 1,478
  • 12
  • 30
Davos
  • 5
  • 2

1 Answers1

0

I see you have a dataSource array which contains list of books. At the simplest you could just append data to your dataSource, then reload your UITableView. But if you want persistent storage, you could look into local db solutions like SQLite, CoreData, or Realm. Then it would just be the matter of storing -> get data -> display on UITableView.

The general idea would be on add button tap (or whatever event listener you want to attach the action to), save it to persistent storage, reload your tableView, since the dataSource is from persistent storage, it'll update automatically. Suppose you load the data from persistent storage.

Also, don't store your array in UserDefaults, it's for bite sized data for things like user session etc.

Edit: as @Leo Dabus point out, indeed you can insert row without reloading the tableView by using

let index = IndexPath(row: items.count - 1, section: 0) // replace row with position you want to insert to.
tableView.beginUpdates()
tableView.insertRows(at: [index], with: .automatic)
tableView.endUpdates()
Kevin You
  • 84
  • 10
  • Why reload? UITableView has a method to insert a new row without reloading it all. – Leo Dabus Apr 18 '20 at 03:59
  • @LeoDabus You're right, should've include it in the original answer, thanks for the heads up! – Kevin You Apr 18 '20 at 04:09
  • 1
    no need to call begin updates for a single row insertion – Leo Dabus Apr 18 '20 at 04:12
  • Btw if you need to do multiple insertions, deletions, moves and/or reloads you should use performBatchUpdates https://developer.apple.com/documentation/uikit/uitableview/2887515-performbatchupdates – Leo Dabus Apr 18 '20 at 04:16