UITableView grouped style custom section headers

Discussion in 'iOS Programming' started by North Bronson, Mar 6, 2009.

  1. macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #1
    When you use Apple's free header views for your sections (and add text for a title), does anyone know what is going on behind the scenes? Is it just a regular UILabel?

    The reason that I ask is because I had to write some custom section headers with my own labels. I made the labels with a clear background so that the textured table background shows through. It seems like my table isn't scrolling quite as fast as it used to.

    Is there any other way to show that textured background behind my label while keeping the label opaque? Is this what is going on when you use Apple's section views?
     
  2. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    The 'Cells and Table-View Performance' section of the "Table View Programming Guide for iPhone OS" recommends using opaque subviews or you may impact the performance of your table view. This is probably what is happening with your section headers.

    Depending on the characteristics of your textured background, you may be able to include a similar background into your section titles and those avoid the need for transparency.
     
  3. thread starter macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #3
    All that I want is the same blue-and-white lines that you see behind the normal Apple grouped tableview.
     
  4. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    If you are using a Grouped Style table view and are already getting the blue-and-white lines then, wow, I don't think there should be any impact in using the tableView:titleForHeaderInSection: since that just returns an NSString that I'm pretty sure gets converted into a UILabel for the section titles.
     
  5. thread starter macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #5
    Well, I kind of wanted to display the title in two different fonts and with different justifications. I don't think titleForHeaderInSection would give me that much control.
     
  6. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    It won't. But the trade-off for that control is a decrease in performance.
     
  7. macrumors newbie

    Joined:
    Dec 14, 2002
    #7
    So how do I actually implement a custom section header? subclass UITableView and override the drawRect method, using [self rectForSection:<num>] to figure out where to render it?

    Specifically, I'm wondering if I can add a UIButton to one of my section headers - so I want not just to draw a button, but also to respond to clicks on it. Any ideas, is this possible?

    Cheers, Alan
     
  8. macrumors 6502a

    Joined:
    Nov 9, 2007
    #8
    You could always set the background to your custom header labels rather than having them clear:

    Code:
    mylabel.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
     
  9. thread starter macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #9
    Yes, that's what I ended up doing.
     

Share This Page