Saving the state of my UITable View - iphone

i have a checklist that is on a UITable View. The user can add/delete cells and check them and uncheck them. I need some assistance with saving the state of the checklist when the page is left, becuase when the user adds a new cell, leaves the page, and returns, the table view is back in the original state i had made it be in! Does someone know my problem? Thanks Guys :D
-Kurt

What you appear to be missing is that adding or deleting a row from your table does not update your source array. Typically when working with a UITableview there are three steps you must perform:
add or remove the tableview cell
update your source array (presumably adding or deleting objects in your context)
save your context to commit the changes
This is assuming you are using Core Data for persistent store. If you are saving in memory than you are likely done at step 2.
I suggest you review the UITableView Class reference, look over the examples and step through a tutorial before trying to stumble through the process.

Related

Entering data directly via table view swift 4

I’m having a play about with table views and saving data. I’ve managed to created an add button where you can add some data to a table view (like a to do list app) and it saves in user defaults. However, I can’t seem to figure out how to add and save data directly just by pressing an empty row in the table view. Basically, I would like to know how to add data and display data on a table view just like the stock To Do and Reminders app from Apple.
I’m working with Swift 4.
Any help or code examples would be much appreciated!

My data source is being changed out from under a UITableView

I have an app based on a tab bar and data retrieved from the Internet. The main tab shows a map and one of the other tabs shows incidents around the center point of the map displayed using a UITableView. If the user moves the map and then moves to the incidents page, I need to refresh the list of incidents displayed in the table. To do this I request the incidents in viewWillAppear:animated: and when that completes (asynchronously) I call the table view's reloadData method.
This works beautifully unless the user taps between the tabs quickly (e.g. display incidents, move to map, move map, move back to incidents, move back to map, etc.). At some point the incidents data source (an NSArray) is modified while the table view is trying to access it.
Here is a question that is similar:
UITableView Crashes if Data Source Is Updated During Scrolling
One of the solutions for that question describes a solution at a high level that is exactly what I want: Freeze the data source while the table is being updated. The thing I can't figure out, however, is when to unfreeze the data source. The problem is I can't find any way to be notified when the table is done being updated.
Any ideas? How do I freeze the data source while the table is being updated and then unfreeze it once the table is done being updated?
Although I'd really like to receive a notification when the table view is done accessing the data source, I found that my problem was due to modifying the data container from the work thread. The answer to this question led me to the solution:
Refreshing XML data and updating a UITableView
What I do now is fill a separate array in the worker thread and then perform a selector on the main thread to swap the updated data into the data container used by the table view.
The problem is I can't find any way to
be notified when the table is done
being updated.
I think you can assume that the data is done being loaded by checking in tableView:cellForRowAtIndexPath: to see if the last row has been loaded.
I haven't tested it, just making an assumption based on the API

iPhone checklist app

I am trying to create a simple app that displays a list of items with check boxes next to each item, then give the user to simply check each box. Nothing (aside from the checkbox image switching) needs to happen when the check box is touched.
Each checklist is a seperate NSDictionary contained in a single master NSDictionary. The checklist dictionary contains arrays for different sections of each checklist.
At the top of the view, the user selects which set (dictionary) of checklists they want to open then I want that checklist to display underneath the picker once a "select checklist" button is pressed.
Any ideas on the best way to get this done?
The easiest thing is to use a UITableView with the accessoryType set to UITableViewCellAccessoryCheckmark for the marked cells. You can find lots of tutorials on working with UITableViews, and it'll be very simple to do what you're describing. For changing the data set, a UISegmentedControl is probably the way to go (if you weren't planning on using that already).

iPhone Table View Data Edit Pane

I have an application I'm working on, and I need the user to be able to add new "Shows", "Movements" and "Dots." These are all represented by classes. At the root of the application, all the shows are shown, the user can click on the show, see the movement in that show, then tap on a movement and see the dots in the movement. It works beautifully.
Now, I need the user to be able to add and edit these instances of these classes. The way I am thinking this will work is when the user clicks on the "Add Show" button (Or the "Add Movement", etc) a new view will be pushed onto the Navigation Controller. This works. When the button is pressed, a new instance of the show class is created, and passed to the new view controller. This also works. If the user wants to edit the show, then they will hit the edit button for the row, and the instance of the class (which already exists) will be passed to new view controller, and the user will be able to edit it (It should use the same view controller for adding and editing)
My question is, in the examples I have seen, it is always really dirty to create the editing view. The edit view is a table view with each row having some sort of control. Usually it is a UITextField, but it may be a slider, and it may be one where another view is popped, and the user needs to check one value. (This is similar to the address book application when adding and editing a contact)
Is there any way that is cleaner than just manually going in and creating a bunch of arrays to hold what custom table view cells need to be at what row? This gets very messy, very fast. I can do it this way, I just was wondering if there is a better, possibly faster way.
To my knowledge there's no structural solution to solve this. I'm afraid managing the cells with child UITextField or other controls yourself is the only method. This indeed gets dirty and painful very fast, I certainly feel your pain.
Although it doesn't exist, it would be very convenient if Apple added out of the box editing cells to the SDK, similar to the different normal cell styles. I haven't come across an open source project that addresses this issue, but it might exist.
If you do find a better/cleaner method to handle these situations, be sure to ping back.
as far as i know, editing mode is the only way to make the changes you describe (if i understood correctly). I agree that it doesn't seem like the most elegant approach.
http://developer.apple.com/iphone/library/documentation/UserExperience/Conceptual/TableView_iPhone/ManageInsertDeleteRow/ManageInsertDeleteRow.html#//apple_ref/doc/uid/TP40007451-CH10-SW19

How to save reordered rows only on pressing the Done button?

if i reorder rows on my table view, is it possible to save the result only if the user pressed the "Done" Button?
(Reorder is working great, but i only want to commit the final result of the positions if the user pressed the Done Button, and not every time a row is moved.)
Is there something like editingStyle == UITableViewCellEditingCellMove in commitEditingStyle?
Or is this only possible in the moveRowAtIndexPath action?
Thanks for help!
You will need to create some "editing" version of your internal data structure that you commit when the user finished editing. UITableView does not keep track of where the cells are; it relies on you to do that. It just tells you when the user asks to move things. If you don't really want to move them at that point, then you'll need to keep track of where they really are versus the current order in the TableView.
Remember, UITableViews are intentionally dumb. They don't keep track of data. They just draw things. You keep track of data (as the data source), and tell them what to draw when they ask.
I generally recommend that developers separate their actual data into a separate model class, and have the UITableView maintain a separate NSMutableArray based on that model class. In that case, during editing, you would update the UITableView's array, and when finished editing, it would send that array to the model to update the "real" data.