iOS Question How to set TableView Section Header size, color etc

Discussion in 'iOS Questions' started by Derek Johnson, Oct 18, 2015.

  1. Derek Johnson

    Derek Johnson Active Member Licensed User

    I'm trying to set the Size, Color etc of the section headers in a Tableview.

    It appears possible to do this using Objective-C but I can't figure out how.

    [Short summary - to save you reading the whole thing, it turns out that you can't do this unless you are prepared to implement all the methods of a TableView in Objective C. :( ]

    I thought it might be possible to replace a Section Header with a Label with this code

    Sub ChangeSectionHeader(Table As TableView)
    Dim MyView As Label
    "Custom Header"
    Dim no As NativeObject = Table
    "willDisplayHeaderView:"Array (True,MyView,0))
    End Try
    End Sub
    but I get this exception when I try it:

    I think that I'm sending the message to the wrong object, but not sure.

    What am I doing wrong?
    Last edited: Oct 21, 2015
  2. JanPRO

    JanPRO Well-Known Member Licensed User

    I think as you can see, willDisplayHeaderView is not a method like you know from B4i, so you have to use obj inline code to catch the sub.
    Last edited: Oct 18, 2015
    Derek Johnson likes this.
  3. Derek Johnson

    Derek Johnson Active Member Licensed User


    Thanks - I missed off the bit of Objective-C that I was trying to call:

    #if objc
    - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
        // Background color
        view.tintColor = [UIColor blackColor];

        // Text Color
        UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
        [header.textLabel setTextColor:[UIColor whiteColor]];

        // Another way to set the background color
        // Note: does not preserve gradient effect of original header
        // header.contentView.backgroundColor = [UIColor blackColor];

    #end if
    but this code is not being reached I think .
  4. JanPRO

    JanPRO Well-Known Member Licensed User

    Derek Johnson likes this.
  5. Derek Johnson

    Derek Johnson Active Member Licensed User

    Thanks for this suggestion, I've looked at this article, and just tried it. I managed to work round most of the issues involving the section numbering by creating a real section header with no header title and no footer title, followed by a table item that is formatted to look like a section header. I also put tags on the table items so that I could distinguish between pseudo-headers and standard table items.

    This is my code for setting up a Custom Section Header if anyone is interested:

    p_TableView is a Globally defined tableview
    is a reference TextField used for positioning

    Sub InsertHeader(SectionHeader As String)
    'This creates an invisible section divider and increments the section number
    Dim tc As TableCell = p_TableView.AddSingleLine("")
        tc.ShowSelection = 
    Dim p As Panel
        p.Width = objAnchor.Width
        p.Height = objAnchor.Height

    Dim Myview As Label
    'Can set these as required
        tc.CustomView = p
    End Sub
    One thing that I couldn't manage to achieve however was the effect whereby the current section header appears at the top of the scroll area as you scroll down to later sections.

    [Edit added later: I've also discovered an issue when the device is rotated. The Panel and Label in the above example needs to be re-sized and moved. I've had to add code to remember the objects and then to update them in the Page_Resize event. Will update the example at some point]

    I still couldn't figure out how to re-format the actual section headers using your Objective-C and my previous guesses.

    #If objc
    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
        return anyView;
    #End If
    In the above code anyView is obviously undefined and I wasn't sure what to set it too.
    Last edited: Oct 20, 2015
    kemanlam likes this.
  6. JanPRO

    JanPRO Well-Known Member Licensed User

    Something like

    #If objc
    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
            UILabel*label =[[UILabel alloc] init];
        label.textColor =[UIColor blackColor];
        label.backgroundColor =[UIColor whiteColor];
        label.text = @"test";
        return label;
    #End If
    kemanlam and Derek Johnson like this.
  7. narek adonts

    narek adonts Well-Known Member Licensed User

    To make it work you shold change the tableview delegate to your module where you are using inline objc.
    JanPRO likes this.
  8. Derek Johnson

    Derek Johnson Active Member Licensed User

    Sorry - I can't figure out how to do that. Could you explain what that would look like - I've having great trouble understanding and working with delegates of objects.
  9. JanPRO

    JanPRO Well-Known Member Licensed User

    Changing the tableview delegate
    Dim NaObj As NativeObject = TableView1
    means also, that the B4i methods for the TableView wouldn't work any more. So you have to do all operations for the tableview with inline objc code ...
    Derek Johnson likes this.
  10. Derek Johnson

    Derek Johnson Active Member Licensed User

    Aargh!! :(

    I don't think I'll be going down that route - sounds decidedly scary, but thanks for your patient help.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice