Nikita Took
Nikita Took

Reputation: 4012

ios UITableView with fetchedresultscontroller - add custom rows

I have a UITableView with data coming from NSFetchedResultsController.

Here is my tablewView:

enter image description here

I need to add a row "All types". It also needs to be:

  1. Sortable with all other items
  2. Selectable (Design is now selected)
  3. Selecting "All types" should deselect other rows
  4. Give something to understand that it's an "All types" row when selected

I've read Add extra row to a UITableView managed by NSFetchedResultsController and NSFetchedResultsController prepend a row or section. Given approaches makes impossible to sort data or will look so hacky and produce so much hard-maintailable code, that it will be impossible to change logic and maintain code.

Are there any other good options?

PS. I understand, that my question may sound "broad" and doesn't containt code, but I think it's very common problem.

Upvotes: 1

Views: 157

Answers (1)

Matic Oblak
Matic Oblak

Reputation: 16774

I do not think this is a very common problem at all. I can see it seems natural to do what you are trying but lets analyse your situation: What you generally have are 2 arrays of objects which you wish to sort as a single array. Now that is quite a common situation and I believe everyone knows how to solve this issue. You need to create a single array of objects and then sort it.

The way I see it you have 3 options:

  • Fetch all the items, merge the 2 arrays, sort and present them. This is not a very good idea since your memory consumption can be a bit too large if there are a lot of items in the database.
  • Put the extra data into the database and use a fetch result controller as you would normally. This should work good but you will probably need to mark these items so they are later removed or keep it in the database but ignore them where you wish not to display them.
  • Create a temporary database combined with what needs to be fetched from the database and your additional data. This approach is great if your data are meant for read-only in this list (which actually seems to be the case in what you posted). Still it is best if you create some kind of link between the objects. For instance some kind of ID would be great, this way when user selects an object from the second database you simply read the ID and fetch the object from the original database.

Upvotes: 1

Related Questions