Section Headerview with button

TroySmithTroySmith USUniversity

I have a tableviewcontroller, and in one of the section headers, I have a button. When the button is tapped, I insert additional elements into the section. If the button is tapped again, the elements are removed. This has the effect of a collapse/expand tree. The button and logic work fine for the most part.

The tableViewController is part of a navigation. If I navigate backward, then come back to this tableViewController, the button tap works, the code-behind is fired in debug mode, the code to insert and remove section elements fires, but the view never displays the extra elements.

Any ideas?

Posts

  • I believe that any code you have initializing things within your tableViewController's ViewDidLoad() method is not called again after the initial navigation (at least thats my experience so far). Could there be something in there that you need to run every time? My guess is that this occurs because the controller is not destroyed upon navigating away, but I'm still a newbie to Xamarin.

    Could you possibly share any of the code you created for making the collapsible section headers? I've been struggling with how to implement that myself and haven't been able to find any examples/guides.

  • TroySmithTroySmith USUniversity
    edited January 2014

    Hi Matthew,
    here's the method I created for the section header:

    public static UIView SectionCellView(string text, UIButton button = null)
            {
                UIView view = new UIView(RectangleF.FromLTRB(0, 0, 0, 30));
    
                UILabel l = SectionHeaderLabelStyled(
                    new UILabel(RectangleF.FromLTRB(15, 0, 250, view.Bounds.Bottom)), text, ECLSUIUtil.HelveticaNeueBold(16)
                ); 
    
                SizeF size = LabelRequiredSize(l, l.Text);
                l.Frame = RectangleF.FromLTRB(l.Frame.Left, l.Frame.Top, l.Frame.Left + size.Width, view.Bounds.Bottom);
                view.AddSubview(l);
    
                if (button != null) {
    
                    button.Frame = RectangleF.FromLTRB(
                        l.Frame.Right + 10f, (l.Frame.Height / 2) - (button.Frame.Height / 2), 
                        l.Frame.Right + 10f + button.Frame.Width, (l.Frame.Height / 2) - (button.Frame.Height / 2) + button.Frame.Height
                    );
                    view.AddSubview(button);
                }
    
                return view;
            }
    
    
    
    // Call to SectionCellView:
    SectionCellView _billingSection.HeaderView = ECLSUIUtil.SectionCellView (
                        "Billing Items", 
                        UIUtil.SectionHeaderViewButton (
                        _bindingContext, "<< Hide Items", GlobalButtonTags.ShowHideBillingItems, 
                        () => HandleShowHideBillingItems (), () => true
                        ) 
                        );
    
Sign In or Register to comment.