我写了一个expandableDatePickerElement作为MonoTouch.Dialog.Element
(更改了代码:here)
这是我的代码:
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
}
}
此代码工作正常。每当我在Dialog上有这个元素时,如果用户点击元素,它就会以正确的方式扩展。
但是,如果我将它添加到现有的根元素(已经显示),它将无法正常工作。看起来表格不会扩展单元格。
此方案的示例代码:
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);
这是展开元素的图片(滚动日期选择器仅适用于选取器的上侧):
有谁知道这个问题发生了什么,或者我对这段代码做错了什么?
答案 0 :(得分:1)
我发现从未调用过expandableElement上的GetHeight。
添加以下行为我解决了问题:
root.UnevenRows = true;