Data Hierarchy with Table Views in iOS

430 Views Asked by At

I have possibly many nested list..For exampel

I have a list and contains some folders, when user tapped it, i show content of the folder in the same tableview.. and this content may has some folders also, can when user tapped it, it also shows the same in the tableview.

but each type of list of the tableview can be different. For example, first folder's list is list of X class, but the second one can be Y class..

I did something but it doesnt work properly in some cases. So I do not know how to solve this kind of nested list.

as scheme, Folder 1-> (Folder X,Folder Y, Document 1, Document 2) if user tap folder X

folder X -> (folder a, folder b)

but this list (Folder X,Folder Y, Document 1, Document 2) is class of X, like List this list (folder a, folder b) is class of Y like List

because webservice send me like that..

I know that it is complicated but i hope it is clear.

I am using Xamarin.ios, but doesnt matter for objective-c. I just seek for possible solution. Like a generic list?

1

There are 1 best solutions below

1
On BEST ANSWER

1. Nested Lists:

In your view controller keep a list of the "selected objects" (it would behave as a stack). When you select a cell, add that corresponding folder / document / ... object to the selectedObjects list/stack. If you go back, remove the last element. When showing objects for the table view, take the last entry in the list/stack and you know "where you are".

A sample use case following your example:

  1. selectedObjects is empty
  2. Tap "Folder 1" in table view
  3. Add "Folder 1" to the end of selectedObjects
  4. Show the sub elements of "Folder 1" being Folder X,Folder Y, Document 1, Document 2
  5. Tap "Folder X" in table view
  6. Add "Folder X" to the end of selectedObjects (the list now has two entries "Folder 1" and "Folder X")
  7. Show the sub elements of "Folder X" being folder a, folder b
  8. Tap "Back" in table view
  9. Remove the last entry from selectedObjects, now it contains only "Folder 1"
  10. Get the last entry from selectedObjects which now is "Folder 1"
  11. You have the folder for which to show the sub list

2. Different Classes:

If there is no other way, you could create a base class - say - CellData. Then for each different class which you get from your webservice, create a subclass - say - FolderCellData and DocumentCellData. Each of the subclasses holds a reference to the actual instance which you get from the web service. Then you could use lists of CellData to hold references to class X and Y.

There will be code which "decides" which of the CellData subclasses to use depending on the class from the webservice. These decisions may as well be placed in the rendering (decide which UITableViewCell to use?) or in the user input handling (Which cell has been tapped -> which class is it in the list of objects?). So you may as well use a list of NSObjects anyway and save you from having to maintain a mirror class hierarchy for the webservices classes.