Resolved Creating a blank table view

Discussion in 'iOS Programming' started by MickeyT, Mar 3, 2013.

  1. MickeyT, Mar 3, 2013
    Last edited: Mar 3, 2013

    MickeyT macrumors member

    Joined:
    Apr 26, 2010
    Location:
    Newcastle, United Kingdom
    #1
    Hello

    I have just seen that the UIColor groupTableViewBackgroundColor has been deprecated in iOS6. This was used so that a view could have the same background as a group-styled table view.

    On searching the internet I came across a proposed solution on stackoverflow (http://stackoverflow.com/questions/12452810/is-grouptableviewbackgroundcolor-deprecated-on-ios-6), which is to put an empty table view onto a view and place all the other sub views on top of it, and then set the style to Grouped.

    Then, when this view is loaded, because the table view is empty, you get the grey lined background but without any table view cells in it.

    I have done this, and it works, but the table view that you drag out of the object library in Xcode is full of preset example table view cells. It makes viewing the real content of the view pretty awful (see screenshot).

    Is there any way to blank the table view so that it is empty and therefore completely transparent so that I can work in interface builder without all the preset stuff spoiling the canvas?

    Thank you.
     

    Attached Files:

  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    I'm not sure of the answer to the question of how to create a blank tableview in IB. I always do this in code and don't set the delegate or dataSource.

    The reason for the deprecation of the color is that the table view background is a gradient. This was implemented first on iPad when it came out in iOS 3.2. They did the same thing for iPhone (I think) with iOS 6.0.

    You may have to look closely but the grey background on iPad and now the striped background on iPhone is a gradient from top to bottom. This effect can't be captured in a UIColor, I guess.
     
  3. MickeyT thread starter macrumors member

    Joined:
    Apr 26, 2010
    Location:
    Newcastle, United Kingdom
    #3
    Perhaps that's the answer - there are examples of doing it in code via that link as well, and given that it isn't meant to offer any functionality it would be pretty easy to do. I also see there is a "send to back" method as well so it will be easy to put the table view behind the text fields and labels.

    There were suggestions at the link to capture the background as an image, but that seems to be a bit complex.

    For info, this is just so when a cell in a table view is clicked and a "detail" view is loaded, the detail view has a consistent background color (like in the settings app - although I think the detail view in there is perhaps a table view as well).
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    It has? Because according the the UIColor Class Reference, it hasn't been.
     
  5. MickeyT thread starter macrumors member

    Joined:
    Apr 26, 2010
    Location:
    Newcastle, United Kingdom
    #5
    I noticed that - I have reference libraries for iOS 5.1, iOS 6.0 and iOS 6.1 and it doesn't have the red writing against this in any of them.

    But I get a yellow warning in Xcode telling me it has been deprecated, and when you load the view the background is just white rather than the grey lines.
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Huh, that's interesting. I guess (once again) Apple's Docs are behind the times. Thanks for clarifying that!
     
  7. MickeyT thread starter macrumors member

    Joined:
    Apr 26, 2010
    Location:
    Newcastle, United Kingdom
    #7
    No problem.

    So I suppose the upshot is that programmatically creating the table view is the best approach to achieve the grey background whilst not having the example cell clutter in interface builder.

    Thanks everyone.
     
  8. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
  9. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #9
    Apple's documentation department is getting further and further behind.

    I did a search in the frameworks, and found this in the header files:


    Code:
    // Group style table view backgrounds can no longer be represented by a simple color.
    // If you want to have a background in your own view that looks like the table view background,
    // then you should create an empty table view and place it behind your content.
    + (UIColor *)groupTableViewBackgroundColor; // This method will be deprecated during the 6.0 seed program
    
    I've seen a few other things that are flagged as deprecated in code in iOS 6.0, but not in the docs. That's pretty poor. Warning us about deprecated methods is a very important job for the documentation.
     
  10. MickeyT, Mar 3, 2013
    Last edited: Mar 3, 2013

    MickeyT thread starter macrumors member

    Joined:
    Apr 26, 2010
    Location:
    Newcastle, United Kingdom
    #10
    In the spirit of answer completeness for anyone reading through this in the future and doesn't know, I added this code to the viewWillAppear method of the view controller, and it works a treat:

    Code:
    UITableView *backgroundTableView = [[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStyleGrouped];
    [[self view] addSubview:backgroundTableView];
    [[self view] sendSubviewToBack:backgroundTableView];
    Edit: probably should be in viewDidLoad actually
     
  11. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #11
    You can use insertSubview:atIndex: to put the tablewview where you want in one call. Also, I set the resize mask so it resizes when the device rotates.
     
  12. MickeyT, Mar 4, 2013
    Last edited by a moderator: Mar 4, 2013

    MickeyT thread starter macrumors member

    Joined:
    Apr 26, 2010
    Location:
    Newcastle, United Kingdom
    #12
    Ah yes, sorry - thank you.
     

Share This Page