locked
MT.D section enable or disable RRS feed

  • Question

  • User1603 posted

    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

    Tuesday, July 9, 2013 8:06 PM

All replies

  • User37696 posted

    @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.

    Wednesday, July 10, 2013 1:00 PM
  • User1603 posted

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

    Wednesday, July 10, 2013 4:32 PM
  • User2418 posted

    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.

    Thursday, July 11, 2013 1:01 PM