Question about Table Views

Discussion in 'iOS Development' started by chauhan4, Aug 3, 2010.

  1. chauhan4

    chauhan4 New Member

    Joined:
    Nov 21, 2009
    Messages:
    5
    Likes Received:
    0
    Device:
    3G iPod touch
    Hi,
    I've got a pretty trivial development question about table views, the answer to which I have searched for but haven't been able to find. I am creating an app that utilizes table views on multiple pages. Everywhere I've looked, I've encountered developers that have used different view controllers for table views displayed on different pages of their app. Is there a way to use the same table view controller to display on different pages of my app but populated with different data on each of those pages (these table views are on the same level of the app, they are going to be displayed one level down based on the selection of the user). I think it would make the app a little more efficient. Has anyone done this? Can anyone point to a good source to see how this can be done?
    Thanks a lot.
  2. Pelaez-1

    Pelaez-1 New Member

    Joined:
    May 5, 2008
    Messages:
    822
    Likes Received:
    0
    Device:
    iPhone
    That's really easy, but messy.

    You could add a tag to your view controller, and depending on that tag, load different data, with switch.

    Like I said, your code will be very messy, though, and personally I wouldn't do it, since making changes will not be an easy task.
  3. chauhan4

    chauhan4 New Member

    Joined:
    Nov 21, 2009
    Messages:
    5
    Likes Received:
    0
    Device:
    3G iPod touch
    So, is the best way to implement this creating new table view controllers for each set of data? Wouldn't that be inefficient from a memory standpoint? Or is there another way to implement this that I haven't yet considered? Please let me know, thanks a lot for your help.
  4. Pelaez-1

    Pelaez-1 New Member

    Joined:
    May 5, 2008
    Messages:
    822
    Likes Received:
    0
    Device:
    iPhone
    You could set the delegate and data source of the table view to different, lighter objects, and use the same TableViewController
  5. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Option A, create one generic table view controller that accepts a data source. Reuse that object whenever possible.

    Option B, feed the table a new .datasource and .delegate object then call [table reloadData]. It might be handy to combine the datasource and delegate, but that's not necessary: two ancillary objects will do just fine. Don't forget to push/pop navigation items whenever encessary to give the illusion of in app navigation.

    Download and compile this source code to see a combination of A and B in action: http://github.com/SkylarEC/ConnectSome

    Option C, combine A and B.

    Option D, do the same thing as Option B, except instead of using dedicated delegate objects, create an if-then-else tree within each of your table's delegate methods to cover each table that is currently displayed. Handle actions accordingly.




    Most newer developers will pick D. Option D is wrong.

Share This Page