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.
Related
I want to loop through a TableView and extract the text from all the selected rows. I suppose I "could" create and maintain a special array that is updated every time a row is selected/deselected using the didSelect/didDeselectRowAtIndexPath methods. But creating a separate array seems like an extra step. Is there no way to let the TableView itself serve as the array and then simply loop through it and get the selected rows? What would the code look like? I'm new to Swift, so this might be a silly question.
Part of the problem is that cells are supposed to be reused, and when used this way it is not possible to loop through them all. You could get around this by using a unique reuse identifier for each cell, such as the indexPath itself or some underlying unique id in your model. Then, you could indeed loop through all cells and retrieve whatever state you desired from each.
You would, however, find your application crushed under the weight of too many cells being instantiated and kept in memory. If you don't have many cells you won't be killed, but try it with a big data set and your app will enjoy a very quick death.
It is far more efficient to store one array with a bunch of id's than a large number of memory-intensive UITableViewCells.
As mentioned in comments, you should work with underlying datasource, not the table itself.
For example if your table shows rows from Array, it is way more faster to retrieve strings directly from that array than creating UITableViewCells and get strings from them.
Get indices of selected rows using UITableView's property indexPathsForSelectedRows.
Query datasource for each row.
As has been said the tableview only handles displaying, your datasource is what powers the data shown if you think about it.
Plus as said before the tableview dequeues cells as they scroll on and off the screen.
The best way to achieve what you want is to add a property to your datasource for each element that will allow you to filter out the select properties easily.
How are you storing the state for each selected cell currently? As this is the same functionally you would use to be able to generate your selected text 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.
I have a table view which populates from an array of strings. Using UIGestureRecognizer users can reorder the cells and save the new order.
When pressing Save I need the new order of the cells to save to the array. I have tried many different techniques without success.
How can I capture all the current cell texts in order?
Don't rely on the cell contents as your data source. When the user reorders the cells, update your backing array to match (or a copy of it if you need to be able to revert changes).
That way, when you want to save, everything is already in the correct order.
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:.
I have a UITableViewController that has the ability to save each row to a plist file. Currently when you press a detail disclosure indicator on the row it opens a separate UITableViewController with four static cells in two groups.
At the moment the data in these cells is just being saved in NSUserDefaults, but I'd like to be able to save them in a more robust manner (i.e. plist) and have each set of settings associated with its own row in the first table view controller.
Any help would be greatly appreciated.
Thanks,
Chris
Each of your groups can be saved as an array of dictionaries within your property list. Within each dictionary (or group), you may include various elements, least of which could be an array which would represent your 'related' items. This array could be a collection of simple values or even dictionaries themselves if you are working with a more complex set of data.
You may even take it a step further and use CoreData to create a nice relational object model.
Resources:
Property List Programming Guide
Core Data Programming Guide