UIPickerViewModel为UIPickerView monotouch调用无限次GetView

时间:2013-09-23 15:17:26

标签: xamarin.ios

我有一个UIPickerView,它使用自定义UIPickerViewModel来覆盖UIPicker视图中显示的视图。

当我第一次打开UIPickerView时,一切顺利,我能够选择一个项目而不是。

如果选择的项目是UIPicker中的第一个或第二个项目,我不会遇到任何问题。但是,如果我选择说第3项,则关闭选择器,然后尝试再次选择其他选项,UIPickerView永远不会在屏幕上显示。

我发现我的PickerViewModel上的GetView方法会被一遍又一遍地调用所选行以及之后的行。我有没有做过可能导致这种行为的事情?下面是我的PickerViewModel

public class AddressPickerViewModel : UIPickerViewModel
{
    private ShippingView view;
    public AddressPickerViewModel(ShippingView view)
    {
        this.view = view;
    }

    private AddressPickerCell currentSelectedCell;
    public override UIView GetView(UIPickerView picker, int row, int component, UIView view)
    {
        if (view == null)
        {
            view = new AddressPickerCell();
            var views = NSBundle.MainBundle.LoadNib(AddressPickerCell.Key, view, null);
            view = Runtime.GetNSObject(views.ValueAt(0)) as AddressPickerCell;
        }

        var pickerCell = (AddressPickerCell)view;
        pickerCell.Selected = false;
        pickerCell.ShippingAddress = this.view.ViewModel.Addresses.ToArray()[row].Address;
        return view;

    }
    public override int GetComponentCount(UIPickerView v)
    {
        return 1;
    }
    public override int GetRowsInComponent(UIPickerView pickerView, int component)
    {
        return this.view.ViewModel.Addresses.Count;
    }
    public override void Selected(UIPickerView picker, int row, int component)
    {
        if (this.currentSelectedCell != null) 
        {
            this.currentSelectedCell.Selected = false;
        }
        view.ViewModel.SelectedAddress=
            this.view.ViewModel.Addresses.ElementAt(picker.SelectedRowInComponent(0));

        var cell = picker.ViewFor(row, component) as AddressPickerCell;
        if (cell != null)
        {
            this.currentSelectedCell = cell;
            this.currentSelectedCell.Selected = true;
        }


    }
    public override float GetComponentWidth(UIPickerView picker, int component)
    {
        if (component == 0)
            return 300f;
        else
            return 40f;
    }

    public override float GetRowHeight(UIPickerView picker, int component)
    {
        return 140f;
    }
}

0 个答案:

没有答案