How to disable sorting in UITableView - ios

I have a view with UITableView and I populate NSMutableDictionary and bind it to UITableView.
I add items like: c,b,a,d but data in table are displayed like: a,b,c,d.
Is it possible to disable ordering so items will be displayed in table in the same order as I added them in NSMutableDictionary?

If the order objects are displayed in your table view matters, you should be using an NSArray or NSMutableArray. These are the only Objective-C collections for which the order can be guaranteed, and they work a lot easier with table views than any other collections, given that their objects are stored at indexes and these can match exactly to the row portion of the NSIndexPath argument of cellForRowAtIndexPath:.

Related

Any way to group items by property in UITableView in runtime?

I have a UITableView which I populate by a list of objects I'm getting from a Realm database. What I want to do is to create sections and group items in the list by a property value in runtime.
All of the examples of grouping items in UITableView I see online are operating with it a prearranged dictionaries.
Is it possible to do?
You can set up your table view data source any way you want. You could write code that decides on the fly which items belong in which sections, but I would advise against it.
I would suggest setting up a method that takes your list of Realm objects as input, and builds an array of sections containing sub-arrays of the rows. Then your cellForRowAtIndexPath method can simply index into your model data like normal.

UITableView data source partly used array

I have a UITableView and an array for data source of it.
But I don't want to use all objects in the array but some of them.
since I need that array fully later, I don't want neither remove any objects from array nor filter the array with the desired objects.
Any suggestions?
You will have to have two arrays:
1- All items are stored in original array;
2- Second array with indices of the items to show in the table
arrOne[arrSecond[i]]
would be the item number i in the table
You need another array with the list of filtered items.

Order NSMutableDictionary for use with UITableView Where key is used in section header

I have a NSMutableDictionary that I use with a UITableView, in the table each section header is the value of a key and the cells for each section is derived from the key's value (an array).
I would like the table view to have a specific key at the top and am not quite sure how to do that.
I suggest changing your data structure to an array of custom "TableSection" objects. Each object would have a header property and an array of values for the cells.
Storing the "TableSection" objects in a mutable array lets you order them whatever way you need.
A dictionary (NSDictionary or Swift Dictionary) is not suitable for storing the contents of a table view. Dictionaries are inherently unordered, and a table view must have its contents presented in a consistent order.
You can certainly use an array of dictionaries. That's pretty common. For a sectioned table view an array of arrays of dictionaries is quite common (outer array has an entry for each section, and inner array has an entry for each row in a given section.)
If you have special contents to go in the section header you can create a section object that has a header object an an array of row objects (dictionaries or custom "row" data objects), and then have an array of section objects. Then you make your data source methods serve up cells using whatever data structure you come up with.

Core data - How to reload NSSet (child objects) on second tableview controller

Hi I am new to Core data. I am having difficulties in reloading my child objects or the NSSet objects which is located on a second tableview.
So I have two tableview controllers, the first one is Budget where I can add or delete a budget. Within that budget object it has a one to many relationship with Expense object (So the Expense is the NSSet).
After when I tap into one of the budget cells, it takes me to the NSSet of Expenses, I then converted the Expenses NSSet into NSArray by using the allObjects method like below:
_expenses = [[_currentBudget expenses]allObjects];
I then populated the table view controller and it shows me all of the individual expense available within that budget. I then have an add (+) bar button to add an expense into the current budget (or call the predefined method that core data gave me: -(void)addExpensesObject(NSManagedObject*)object
Everything is great and it can be saved. However.... once I saved I am not sure how to reload the NSArray of expenses as it is not like the fetchedResultsController. (I am able to reload data on the first tablview once a budget was added because i used the NSFetchedResultsControllerDelegate and implemented the changes/update/insert etc methods).
So my question is, how do I reload those set of NSArray objects? I tried to add in viewdidappear [[self tableview]reloadData] but that didn't work for some reason.. But when I tap back to the first tableview (budgets) and tap back into the expense... it reloads.
Many thanks!
The simplest solution would probably be to use a fetched results controller in the
second table view controller as well. Instead of passing
[[_currentBudget expenses]allObjects]
to the second view controller, just pass _currentBudget. In the second view
controller, create a fetched results controller with a
fetch request on the "Expense" entity and using a predicate like
[NSPredicate predicateWithFormat:#"budget = %#", self.currentBudget]

UITableView Selection - Checking cells

I intend to have a UITableView filled with entries from an NSMutableDictionary. What I would like to do is when checking a cell, it creates an array which contains the key's objects.
The table view's cell.textLabel.text is equal to the key of each element of the dictionary, while the object associated with the key is an integer.
Let's say I tap one of the cells, and the object associated with it is 5722. I want to add that to the array, and check the cell as well. Also, tapping the cell again should remove the object associated with it and uncheck the cell. How do I go about doing this?
As of now, when scrolling, my checkmarks go everywhere.
I think that you are better of having your datasource as an NSArray instance. The list of keys provided by the allKeys method of the NSDictionary instance is not ordered and will change at any time. If you use an array, you can store the selected cells using an NSMutableIndexSet, and use this instance to get the records that have been selected from the array using the NSArray method objectsAtIndexes

Resources