自定义UIPickerView iOS

时间:2017-01-12 18:10:33

标签: c# ios xamarin

我怎样才能选择第一列为“name”的选择器?第二列是“data”? 据我所知,它是一个包含2个组件的选择器:namestuff from the list但是多个name应该只有一个并且在没有移动它的情况下震惊并且仅仅是列表中的数据。 它能够实现一些吗?

这是我的自定义UIPickerView

using System;
using System.Collections.Generic;
using UIKit;

namespace Test
{
    class CustomPickerModel : UIPickerViewModel
    {
        private readonly IList<string> values;
        string name;
        public event EventHandler<PickerChangedEventArgs> PickerChanged;

        public VehiclePickerModel(IList<string> values, string name)
        {
            this.values = values;
            this.name = name;
        }

        public override nint GetComponentCount(UIPickerView picker)
        {
            return 2;
        }

        public override nint GetRowsInComponent(UIPickerView picker, nint component)
        {
            return values.Count;
        }

        public override string GetTitle(UIPickerView picker, nint row, nint component)
        {
            switch (component)
            {
                case 0:
                    return name;
                case 1:
                    return values[(int)row];
                default:
                    throw new NotImplementedException();
            }
        }

        public override nfloat GetRowHeight(UIPickerView picker, nint component)
        {
            return 40f;
        }

        public override void Selected(UIPickerView picker, nint row, nint component)
        {
            if (this.PickerChanged != null)
            {
                this.PickerChanged(this, new PickerChangedEventArgs { SelectedValue = values[(int)row] });
            }
        }
    }
}

这就是我想要获得的可视化:

enter image description here

1 个答案:

答案 0 :(得分:0)

这里的问题是您为两个组件指定了相同的行数。您需要确保将组件0放到1行。

public override nint GetRowsInComponent(UIPickerView picker, nint component)
{
    switch (component)
    {
        case 0:
            return 1;
        case 1:
            return values.Count;
        default:
            // However you want to handle
    }
}