xamarin.ios uipickerview

不说废话,直接上代码 部分解释在注释中

下面代码很简单主要就是 代码生成一个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];
				}

			}
		}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值