13933

Saving Tableview data in swift

Question:

I'm currently working on a tableView based project in swift. I have two tableView running in my project. As my first tableView loaded with data which has a editActionsForRowAtIndexPath function to delete cell(as a Favourite) and moving it to my second tableview.I am using the following code.....

firstTableView:

var arrays = ["Alpha","Beta","Gamma","Phill","Below","Above","Clean",] var deleted: [String] = [] //passing data to another tableVC var sendSelectedData = NSString() let defaults = NSUserDefaults.standardUserDefaults() override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? { let favorite = UITableViewRowAction(style: .Normal, title: "Favourite") { action, index in print("favourite button tapped") let editingStyle = UITableViewCellEditingStyle.Delete if editingStyle == UITableViewCellEditingStyle.Delete { self.tableView.beginUpdates() self.deleted.append(self.arrays[indexPath.row]) self.arrays.removeAtIndex(indexPath.row) tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade) self.tableView.endUpdates() } } favorite.backgroundColor = UIColor.orangeColor() return [favorite] } override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if (segue.identifier == "ShowDetails") { // initialize new view controller and cast it as your view controller let viewController = segue.destinationViewController as! favTableViewController // your new view controller should have property that will store passed value viewController.arrayx = deleted } }

Above code works as i planned. Every time, When the swipe action perform. that particular cell been moved to my second view controller.But, I am getting trouble saving data and reload tableview on both my first and second tableView Controller after tableViewCell been moved to my second tableView...

I know NSUserDefaults function will save data.But, I am not familiar using NSUserdefaults.So,Please someone point me the direction...

thanks in Advance...

Answer1:

NSUserdefaults should only be used for storing small amounts of data, ie: user defaults, and is not ideal for a table view, where in the real world you will have a lot of data to load and display.

Other native options are Coredata, Apple's database solution. Unfortunately it is a complex beast and a mere mortal such as I finds it a pain to use.

I highly recommend you investigate using Realm (<a href="https://realm.io/" rel="nofollow">https://realm.io/</a>). It is a powerful database solution, well documented, and easy to use (and free!). If you want to learn how to integrate Realm into your project the following video will start you off with the absolute basics:

<a href="https://www.youtube.com/watch?v=qGXPqDX7dsw&nohtml5=False" rel="nofollow">https://www.youtube.com/watch?v=qGXPqDX7dsw&nohtml5=False</a>

Recommend

  • EXC_BAD_ACCESS when I'm trying to delete a UITableView sections
  • commitEditingStyle: delete data but continue to display row
  • row is not getting deselected in UITableview cell animation
  • How to implement Expandable - Collapsable UITableViewCell in UITableView - IOS
  • Deleting cell from a tableview - Error - Code provided
  • Can't show the core data in tableview Swift
  • public_activity rails undefined method user_path
  • iOS delegate not working with performSegueWithIdentifier?
  • Nested resource with Atom Feed Helper
  • Reload screen when data change - react native app
  • HTML: prevent url-encoding of POST form
  • How to remove panGesture after touch in swift
  • swipe to delete not showing the delete button
  • setScrollsToTop with multiple UIScrollView classes and/or subclasses(UITableView)
  • AFNetworking synchronous calls (like/unlike)
  • Method for moving rows with NSFetchedResultsController and its delegate not working
  • Delete UITable sections dynamically with commitEditingStyle in Swift
  • How can I debug errors in a map function on Spark workers?
  • Jquery UI Sortable, move item automatically
  • Most efficient way to replace lowest list values in dataframe in R
  • List comprehension with if conditional to get list of files of a specific type
  • passing a default argument to a browserify module
  • vectorized indexing/slicing in numpy/scipy?
  • Application level floating views with navigation in Android
  • Angular2 Response for preflight is invalid (redirect) from some GET requests
  • Run multiple queries from 1 SQL file showing result in multiple tables
  • wxPython: displaying multiple widgets in same frame
  • onBackPressed() not being executed
  • Android fill_parent issue
  • Can I check if a recipient has an automatic reply before I send an email?
  • jquery mobile loadPage not working
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • align graphs with different xlab
  • Return words with double consecutive letters
  • How to delete a row from a dynamic generate table using jquery?
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Is there any way to bind data to data.frame by some index?
  • Reading document lines to the user (python)
  • Python/Django TangoWithDjango Models and Databases