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

    Code:
    Sub ChangeSectionHeader(Table As TableView)
        
    Dim MyView As Label
        MyView.Initialize(
    "Section")
        MyView.Text=
    "Custom Header"
        MyView.Color=
    Colors.Blue
        MyView.Height=
    60dip
        MyView.TextColor=
    Colors.Red
     
        
    Try
            
    Dim no As NativeObject = Table
            no.RunMethod(
    "willDisplayHeaderView:"Array (True,MyView,0))
        
    Catch
            
    Log(LastException)
        
    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

    JanPro,

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

    Code:
    #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:

    Code:
    p_TableView is a Globally defined tableview
    objAnchor 
    is a reference TextField used for positioning

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

        
    Dim Myview As Label
        
    'Can set these as required
        Myview.Initialize("Header")
        Myview.Text=SectionHeader
        Myview.Color=
    Colors.white
        Myview.Height=objAnchor.Height
        Myview.TextColor=
    Colors.Red
      
        p.AddView(Myview,
    10dip,0,p.Width,p.Height)
        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.

    Code:
    #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

    Code:
    #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
    Code:
    Dim NaObj As NativeObject = TableView1
    NaObj.RunMethod(
    "setDelegate:",Array(Me))
    NaObj.RunMethod(
    "setDataSource:",Array(Me))
    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.
     
Loading...
  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