Saving Order on UITableView data - swift

I have created a reorder-able UITableView. I am loading data from Realm, and showing that data into UITableView. My UITableView has Section and rows.
now User can hold Any cell in any section and can reorder them in 2 ways
User can reorder cell with in the same section.
User can reorder cell across the different section.
What I need:
Once the user has made the order he should see same item in same order
when he come back to App. For any type of reordering I have to do
indexing. But I do not have clue how to reorder them in database.
My Idea to resolve the requirement
My idea is to include the String type value in the Model where I can replace something like (1,10) that will indicate section 1, row 10.
But I do not have idea how to tackle this and how to save the reordering as i need here. Any idea about that?? please share

Related

store selected rows on PFQueryTableViewController

I have a tableview that is of type PFQueryTableViewController and gets its value from Parse cloud. The tableview works fine but now I need to allow the user to:
Select a row
Record in a column (string array) on Parse-Users table what rows have been selected (need to record on parse that - i will use these values for other things later)
When the user comes back and opens the tableview he can see what rows have been selected last time he was in the app
I am not sure if PFQueryTableViewController has any methods ready for that. Could anyone give me some guidance?
I would prefer to use parse cause there are so much stuff out of the box. But if not, that is fine as well.
Also, code samples from similar solutions would be great. Just need to know the best approach.
The table view controller is there for display, it will tell you about selection, but it won't automatically maintain a record of selected items in the back end. You need to decide on the appropriate way to store the selections (array of pointers is better than an array of strings) and update the store and table display appropriately. There is no standard approach to this.

How to access UITableViewCell that is out of view on a data entry screen to validate content?

I have a UITableView that contains data entry fields whose total height is about 2 screens tall. After entering data in the fields the user taps the Save button in the UINavigationBar then the contents of all the fields is validated.
If field #1 (topmost) is out of view because the user has scrolled to the bottom of the table then the UITableViewCell that contains the UITextField w/ the value is not accessible. Trying to access the cell/field using cellForRowAtIndexPath returns nil.
What would be the best approach to access all the fields in order to validate the field contents?
The best approach would be to store all of the data as it is entered in the fields, and then query your model for the data when you need it, rather than depending on the data being stored in the view.
You have a few options
What I do - store the text from the fields somewhere else and then use that
Store all of your cells in memory all the time so that they never get reused
Temporarily make your table big enough to hold all of your cells when you're doing the validation
Note that you can do number 3 without the change ever being visible.

how to pass a criteria specific or filtered datasource to UITableview from another view?

this question might be easy but i'm not sure how to do it.
i will like to show a tableview based on users criteria.
user have a checklist, when checked certain or multiple item, the array will check and inform user there is 3 item matching this criteria.
if user tapped on the button, it will push and show the 3 item in UItableview.
tutorial or sample code are appreciated..
thanks for reading
Load an NSMutableArray with the desired items based on the checklist, but adding items when the user checks something or removing objects when the user unchecks something. Call array.count to get the total number of items. Then pass this array to the UITableView as its data source.
Take an array of selected items,call a method,reload your tableview in this method, match your array data with cell text then increase any variable value. then just make an alert view and show the number of items using that variable.
If further any problem knock me.

Mapping a dataset to textfield inside UItableView

In my app, I am using some .net asmx services (as backend) to get some data and populating it on my views. In one of the modules, i need to edit and save the data which am getting from the services.In that am getting several rowkeyvalues and accordingly am creating those many row cells in my tableview, which consists two UItextfields as well, which displays some unique code and comments.
Now if I have to edit and save some fields, I need to map each one of those rowkeys to each row cell and after that am creating object which is basically the wsdl proxy class which I have generated using Sudzc and pass each dataset and serialize it to XML and POST it through SOAP.
The real problem here, am not getting how to map each unique rowkeys to each row cell and create unique dataset(which mainly contains other fields as well including rowkey) for each row and pass it to proxy object (WSDL stubs).
Not sure I understand your problem, but there are 2 ways I can think of to map a row cell to something.
1. Each UITableViewCell is a subclass of UIView, and so it has a "tag" attribute, which is an int.
You can set this and check it's value.
2. When a user taps a row, the tableView:didSelectRowAtIndexPath: is called. IndexPath.row is an int that gives the absolute row postiion within it's section (and if only one section, then in the whole table).
Usually, in cellForRowAtIndexPath: you fetch your backing data corresponding to the IndexPath.row and populate the cell. You could also set the cell's tag at that point.
This ties together the row, the cell and the data.
Hope that helps.
-Mike

Is it possible to preload all the cells in a uitableview?

Is there a simple way to preload all the cells in a uitableview?
I need to do this in order to change whether or not all the cells are checked. If I just use cellForRowAtIndexPath, and the user say unchecks all the cells, and then checks a visible cell and starts to scroll again, either the selected cell gets deselected or the newly loading cells are selected.
It seems the easiest way to go would be to preload all the cells if possible.
Thanks!
Don't use the cells as your database.
Your cells are just a narrow window onto your data. The cells just show you a few of the objects in the underlying data. If you try to preload all the cells so you could then select them all, the UITableView could die a slow death, or slow crawl. Especially if we're talking hundreds of entries.
If you want to select all the items in your data, you do so with a direct call to your data to select its objects. Then, you reload the data into your TableView with a reloadData and if everything is set up right, your cells will show the selected state.
Read up on UITableView. Look at Apple's samples. You need to see the separation of data from the view and the controller.
Please re-read the answer I wrote here to your previous, similar question, which explains one solution to your problem.
Again, you should consider keeping an array of on/off settings. You can use NSMutableArray or a C array of int or BOOL values, whatever you want. But it definitely sounds like you need a data model.
Your -tableView:cellForRowAtIndexPath: looks up values of this array. Each value in the array corresponds in some way to a row in the table.
If you only have one section, then you can simply use the ith element of the array to set the checked state of the ith row of the table view. If you use NSMutableArray to store NSNumbers, you can handle resizing quite easily.
If you have more than one section, keep an array of arrays. Each top-level array corresponds to a section. Each inner array corresponds to a section's rows.
If you use NSMutableArray to store NSMutableArrays of NSNumbers, you can handle resizing and section addition and deletion quite easily.
The method -tableView:cellForRowAtIndexPath: then sets up cells with or without checkmarks, depending on the array's value.
Having a data model gives you the freedom to programmatically perform "select all" and "deselect all" button operations.
For example, when you click a button to "select all" cells:
Loop through the array and set each value to YES or 1 or whatever "on" label you chose originally.
Call [tableView reloadData], which is a method that goes back to -tableView:cellForRowAtIndexPath: and which will set a cell's checkmark state based on the state of values in the updated array.
No, you can't do this. You seem to be under the impression that a cell represents a particular piece of data, it doesn't. When the top cell scrolls off the screen it is usually recycled and brought in as the bottom cell. So a list that has hundreds of items you can scolled through may only ever have 8 or 9 cells alloc'ed and initialized.
You need to rethink your application's architecture. When you "uncheck all" it shouldn't change the visual state of the cell, it should change some state in the objects the cell represents, then when you load the cell for the object at that index path you should read that state and set the check mark appropriately.
The changes in the visual state of your cell should always be in response to changes in your underlying model.