Reputation: 161
I'm using the DateElement in the MvvmCross implementation of MonoTouch Dialog. The exception happens because the method UpdateDetailDisplay(UITableViewCell cell) in the DateTimeElement expects the cell parameter never to be null.
protected override void UpdateDetailDisplay(UITableViewCell cell)
{
if (cell.DetailTextLabel != null)
{
cell.DetailTextLabel.Text = FormatDate(Value);
}
}
It seems that this method is called three times during the set up of a Dialog view:
as a result of creating an instance of a DateElement
on binding
during the building of the TableView when GetCell is called.
The cell parameter is only present on event 3.
Am I doing something wrong or should the method have a test for the parameter being null like StringElement has?
Here is my code in the ViewDidLoad event in my derivative of MvxTouchDialogViewController:
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
this.Root = new RootElement("Sign-Up")
{
new Section()
{
Bind( new EntryElement("Gender:", "required"), "{'Value':{'Path':'Gender','Mode':'TwoWay'}}"),
Bind( new EntryElement("First name:", "required"), "{'Value':{'Path':'FirstName','Mode':'TwoWay'}}"),
Bind( new EntryElement("Last name:", "required"), "{'Value':{'Path':'LastName','Mode':'TwoWay'}}"),
Bind( new EntryElement("Display name:", "required"), "{'Value':{'Path':'DisplayName','Mode':'TwoWay'}}"),
Bind( new EntryElement("Email:", "required"), "{'Value':{'Path':'Email','Mode':'TwoWay'}}"),
Bind( new EntryElement("Confirm email:", "required"), "{'Value':{'Path':'ConfirmEmail','Mode':'TwoWay'}}"),
Bind( new EntryElement("Password:", "required",null,true), "{'Value':{'Path':'Password','Mode':'TwoWay'}}"),
Bind( new EntryElement("Confirm password:", "required", null,true), "{'Value':{'Path':'ConfirmPassword','Mode':'TwoWay'}}"),
Bind (new DateElement("Date of birth", DateTime.Now), "{'Value':{'Path':'DateOfBirth','Mode':'TwoWay'}}")
},
};
}
I have only been able to 'work around' the problem by deriving my own class from DateElement with my own method:
public class MyDateElement : DateElement { public MyDateElement (string caption, DateTime date) : base (caption, date) { }
protected override void UpdateDetailDisplay(UITableViewCell cell)
{
if(cell == null)return;
if (cell.DetailTextLabel != null)
{
cell.DetailTextLabel.Text = FormatDate(Value);
}
}
}
Upvotes: 1
Views: 489
Reputation: 66882
This looks like it's a bug somewhere in MonoTouch.Dialog and/or MvvmCross.
Am I doing something wrong?
No. Looks to me like you are doing things right.
I guess this bug occurs in your sample because the DateTimeElement is a long way down the list - and so it isn't on screen (doesn't get a cell) when the table is first drawn.
It's not clear to me whether the best solution is the one you've found or whether to change the code in ValueElement which calls UpdateXXXDisplay to check for null first (or whether to be defensive and do both!)
private UITextAlignment _alignment;
public UITextAlignment Alignment
{
get { return _alignment; }
set { _alignment = value; UpdateCaptionDisplay(CurrentAttachedCell);}
}
private TValueType _value;
public TValueType Value
{
get { return _value; }
set { _value = value; UpdateDetailDisplay(CurrentAttachedCell); }
}
I'll log this as an issue in https://github.com/slodge/MvvmCross/issues and then fix it shortly...
Thanks for finding this - and for the very detailed notes
Upvotes: 0