MT.D section enable or disable

nodoidnodoid Paul JohnsonGBMember, Beta ✭✭✭

Hi,

Is there a simple way to enable or disable a single section on a mt.d view? I have a boolean element that I want to use to switch on or off a particular section.

Thanks

Paul

Posts

  • JohnMillerJohnMiller John Miller USForum Administrator, Xamarin Team Xamurai
    edited July 2013

    @nodoid,

    I do this a lot in some of my code, and the way I have done it is to either rebuild the Root element without that section, or modify the section and then call Root.Reload(section, animation);

    Section has a RemoveRange and Remove method where you can get rid of the elements you dont want. Root also has a Remove(section, animation) method where you could remove an entire section. You could insert it back later with the Root.Insert(index, section) method. Its a little tricky because you need to keep track of where you want it to be inserted at if its not simply at the beginning or end.

    I use the BooleanElement.ValueChanged event to RemoveRange on a section on the true value, and I use the Insert method on the false value.

  • nodoidnodoid Paul Johnson GBMember, Beta ✭✭✭

    Yeah. This is also the solution I came up with. A bit messy, but it works :)

  • GavinGrantGavinGrant Gavin Grant GBUniversity ✭✭✭

    Try subclassing Element as below. In theory calling ReloadData() on the rootelement should force the GetCell to be called again and the internal element type to render a different control without the need to remove and insert sections.

    public class ReadOnlyElement : Element where TElement : Element , new() { TElement internalElement;
    StringElement readonlyElement;

    Element internalElement;
    
    Model _model;
    
    private Element GetElement()
    {
       internalElement = _isReadonly ? new StringElement(_model.Property) : new TElement();
    }
    
    public ReadOnlyElement(bool isReadOnly, Model someModel)
    {          
        _model = someModel;
            internalElement = GetElement();
    }
    
    public override UITableViewCell GetCell(UITableView tv)
    {
       return internalElement.GetCell(tv);
    }
    
    
    private bool _isReadOnly;
    public bool IsReadOnly 
    {
        get { return _isReadOnly;}
        set { _isReadOnly = value; }
    }
    

    }

    This is purely theoretical mind you and may not work.

Sign In or Register to comment.