I wrote an expandableDatePickerElement as a MonoTouch.Dialog.Element
(Changed the code from:here)
Here's my code for that:
using System;
using MonoTouch.Dialog;
using MonoTouch.UIKit;
using System.Drawing;
namespace ImageToDo
{
public class ExpandableDatePickerElement : Element, IElementSizing
{
Section _parentSection;
UITableViewCell _stringElementCell;
DateTime _minDate;
DateTime _maxDate;
public UIDatePicker Picker{ get; set; }
UIViewElement _viewElement;
public ExpandableDatePickerElement (Section parentSection, DateTime minDate, DateTime maxDate)
:base("date")
{
this._maxDate = maxDate;
this._minDate = minDate;
_parentSection = parentSection;
InitializePicker ();
}
private void InitializePicker()
{
Picker = new UIDatePicker (new RectangleF (0, 0, 320, 200));
Picker.Mode = UIDatePickerMode.DateAndTime;
Picker.MinimumDate = _minDate;
Picker.MaximumDate = _maxDate;
_viewElement = new UIViewElement (String.Empty, Picker, true);
}
public override void Selected (DialogViewController dvc, MonoTouch.UIKit.UITableView tableView, MonoTouch.Foundation.NSIndexPath path)
{
base.Selected (dvc, tableView, path);
_stringElementCell.DetailTextLabel.Text = DateTimeHelper.NSDateToDateTime (Picker.Date).ToString ();
if (_parentSection.Elements.Contains (_viewElement))
Collapse ();
else
_parentSection.Add (_viewElement);
}
public override UITableViewCell GetCell (UITableView tv)
{
const string cellKey = "ExpandableDatePickerElement";
var cell = tv.DequeueReusableCell (cellKey);
if (cell == null)
{
cell = new UITableViewCell (UITableViewCellStyle.Value1, cellKey);
cell.TextLabel.Font = UIFont.SystemFontOfSize (17f);
cell.TextLabel.TextColor = UIColor.Black;
cell.TextLabel.AdjustsFontSizeToFitWidth = true;
cell.TextLabel.MinimumScaleFactor = .5f;
cell.Accessory = UITableViewCellAccessory.None;
cell.SelectionStyle = UITableViewCellSelectionStyle.None;
cell.TextLabel.Text="Date";
cell.DetailTextLabel.Font = UIFont.SystemFontOfSize (16f);
cell.DetailTextLabel.TextColor = UIColor.LightGray;
cell.DetailTextLabel.AdjustsFontSizeToFitWidth = true;
cell.DetailTextLabel.MinimumScaleFactor = .5f;
cell.DetailTextLabel.Text=DateTime.Now.ToString();
}
_stringElementCell = cell;
return _stringElementCell;
}
public void Collapse()
{
_parentSection.Remove (_viewElement);
}
#region IElementSizing implementation
public float GetHeight (MonoTouch.UIKit.UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
return 44;
}
#endregion
}
}
This Code works pretty fine. Whenever I had this element on my Dialog it expands the right way if a user taps the element.
But if I add it to an existing root element (which is already displayed) it does not work correct. It looks like the table does not expand the cell.
Sample Code for this scenario:
RootElement root = new RootElement ("Root");
Section defaultSection = new Section ("Default");
CheckboxElement checkElem = new CheckboxElement ("Check");
defaultSection.Add (checkElem);
Section section = new Section ("expandable");
var expandableElement = new ExpandableDatePickerElement(section, DateTime.Now, DateTime.Now.AddYears(2));
checkElem.Tapped += delegate()
{
if(section.Elements.Contains(expandableElement))
section.Remove(expandableElement);
else
section.Add (expandableElement);
};
root.Add (defaultSection);
root.Add (section);
DialogViewController dv = new DialogViewController (root);
NavigationController.PushViewController (dv, true);
Here is picture of the expanded element (Scrolling the datepicker works only on the upper side of the picker):
Does anyone know whats happening this issue or what I am doing wrong with this code?