不说废话,直接上代码 部分解释在注释中
下面代码很简单主要就是 代码生成一个picker ,需要注意的是 model 的设置。
UIPickerView pc = new UIPickerView(new RectangleF(0f,0f,320f,200f));
pc.Model= new AddressPickerSorce();
addPickerView.Add(pc);
this.ui_view.Add(addPickerView);
//继承 UIPickerViewModel
<span style="white-space:pre"> </span>/// <summary>
/// 选择地区 picker
/// </summary>
public class AddressPickerSorce : UIPickerViewModel
{
//数据集合
List<AddressEntity> lsdata;
//标记选中市级城市
public AddressEntity SelectCity;
//标记选中 区县的内容
public AddressEntity SelectCounty;
public AddressPickerSorce()
{
lsdata= AppGlobal.SupportAddress;
if (lsdata!=null&&lsdata.Count>0) {
SelectCity = lsdata[0];
}else
{
lsdata.Add(new AddressEntity(){Name="请重新加载地区数据"});
}
}
/// <Docs>To be added.</Docs>
/// <returns>To be added.</returns>
/// <summary>
/// 主要告诉 picker 有 两列
/// </summary>
/// <param name="pickerView">Picker view.</param>
public override int GetComponentCount (UIPickerView pickerView)
{
return 2;
}
/// <Docs>To be added.</Docs>
/// <summary>
/// 根据每个列返回该列数据集合的count
/// </summary>
/// <remarks>To be added.</remarks>
/// <returns>The rows in component.</returns>
/// <param name="pickerView">Picker view.</param>
/// <param name="component">Component.</param>
public override int GetRowsInComponent (UIPickerView pickerView, int component)
{
if (component==0) {
return lsdata.Count;
}
return SelectCity.SubRegions.Count;
}
/// <Docs>To be added.</Docs>
/// <param name="component">To be added.</param>
/// <returns>To be added.</returns>
/// <summary>
/// 设置在 控件上列,行上显示的内容
/// </summary>
/// <param name="picker">Picker.</param>
/// <param name="row">Row.</param>
public override string GetTitle (UIPickerView picker, int row, int component)
{
if (component==0) {
return lsdata [row].Name;
}
return SelectCity.SubRegions [row].Name;
}
/// <Docs>To be added.</Docs>
/// <param name="component">To be added.</param>
/// <summary>
/// 如果使用默认的样式则不需要 重写该方法。 //构造一个 lable 控制字体大小
/// </summary>
/// <remarks>To be added.</remarks>
/// <returns>The view.</returns>
/// <param name="picker">Picker.</param>
/// <param name="row">Row.</param>
/// <param name="view">View.</param>
public override UIView GetView (UIPickerView picker, int row, int component, UIView view)
{
UILabel lbl = new UILabel(new RectangleF(0, 0, 130f, 40f));
lbl.TextColor = UIColor.Black;
lbl.Font = UIFont.SystemFontOfSize(13f);
lbl.TextAlignment = UITextAlignment.Center;
if (component == 0) {
lbl.Text = lsdata [row].Name;
} else if (SelectCity!=null&&SelectCity.SubRegions!=null&&SelectCity.SubRegions.Count>0) {
lbl.Text = SelectCity.SubRegions [row].Name;
}
return lbl;
}
/// <Docs>To be added.</Docs>
/// <param name="component">To be added.</param>
/// <remarks>To be added.</remarks>
/// <summary>
/// 在多个列的数据中,需要数据联动并在此进行设置 //选中事件 如果是市级,则保存在当前选中市级
/// </summary>
/// <param name="picker">Picker.</param>
/// <param name="row">Row.</param>
public override void Selected (UIPickerView picker, int row, int component)
{
if (component == 0) {
SelectCity = lsdata [row];
SelectCounty = null;
picker.ReloadComponent (1);
} else {
SelectCounty = SelectCity.SubRegions [row];
}
}
}