-
-
Save patridge/6c1d35292e776c08d878 to your computer and use it in GitHub Desktop.
Xamarin.iOS 8.6 issue with UIPickerViewModel overrides (and example workaround using UIPickerViewDelegate/DataSource). Reference material for Xamarin bug: https://bugzilla.xamarin.com/show_bug.cgi?id=25981.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UIKit; | |
using System.Collections.Generic; | |
using CoreGraphics; | |
using System.Linq; | |
using Foundation; | |
namespace PickerViewFailExample | |
{ | |
public class SomePickerViewModel : UIPickerViewModel { | |
static readonly List<string> items = Enumerable.Range(1, 10).Select(i => "picker item " + i).ToList(); | |
public override nfloat GetRowHeight (UIPickerView pickerView, nint component) | |
{ | |
Console.WriteLine ("GetRowHeight: {0}", component); | |
return 50f; | |
} | |
public override nint GetComponentCount (UIPickerView pickerView) | |
{ | |
Console.WriteLine ("GetComponentCount"); | |
return 1; | |
} | |
public override nint GetRowsInComponent (UIPickerView pickerView, nint component) | |
{ | |
Console.WriteLine ("GetRowsInComponent"); | |
return items.Count; | |
} | |
public override string GetTitle (UIPickerView pickerView, nint row, nint component) | |
{ | |
Console.WriteLine ("GetTitle: {0}:{1}", row, component); | |
return items [(int)row]; | |
} | |
public override NSAttributedString GetAttributedTitle (UIPickerView pickerView, nint row, nint component) | |
{ | |
return new NSAttributedString (GetTitle (pickerView, row, component)); | |
} | |
public override void Selected (UIPickerView pickerView, nint row, nint component) | |
{ | |
Console.WriteLine ("Selected: {0}:{1}", row, component); | |
} | |
} | |
public class PickerViewFailExampleViewController : UIViewController | |
{ | |
UITextView SomeTextView { get; set; } | |
UIPickerView SomePickerView { get; set; } | |
public PickerViewFailExampleViewController() | |
{ | |
SomePickerView = new UIPickerView () { | |
DataSource = new SomePickerViewModel(), | |
}; | |
SomeTextView = new UITextView () { | |
InputView = SomePickerView, | |
Text = "picker test", | |
}; | |
View.Add (SomeTextView); | |
} | |
public override void ViewDidLoad () | |
{ | |
base.ViewDidLoad (); | |
View.BackgroundColor = UIColor.LightGray; | |
SomeTextView.Frame = new CGRect (new CGPoint (50f, 50f), new CGSize (View.Bounds.Width - 100f, 25f)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UIKit; | |
using System.Collections.Generic; | |
using CoreGraphics; | |
using System.Linq; | |
using Foundation; | |
namespace PickerViewFailExample | |
{ | |
// This is a hacked-together workaround for UIPickerViewModel issues in PickerViewFailExampleViewController. | |
public class SomePickerViewDelegate : UIPickerViewDelegate { | |
public static readonly List<string> Items = Enumerable.Range(1, 10).Select(i => "picker item " + i).ToList(); | |
public override nfloat GetRowHeight (UIPickerView pickerView, nint component) | |
{ | |
Console.WriteLine ("GetRowHeight: {0}", component); | |
return 50f; | |
} | |
public override string GetTitle (UIPickerView pickerView, nint row, nint component) | |
{ | |
Console.WriteLine ("GetTitle: {0}:{1}", row, component); | |
return Items [(int)row]; | |
} | |
public override NSAttributedString GetAttributedTitle (UIPickerView pickerView, nint row, nint component) | |
{ | |
Console.WriteLine ("GetAttributedTitle: {0}:{1}", row, component); | |
return new NSAttributedString (GetTitle (pickerView, row, component)); | |
} | |
public override void Selected (UIPickerView pickerView, nint row, nint component) | |
{ | |
Console.WriteLine ("Selected: {0}:{1}", row, component); | |
} | |
} | |
public class SomePickerViewDataSource : UIPickerViewDataSource { | |
int rowCount; | |
public SomePickerViewDataSource(int rowCount) { | |
this.rowCount = rowCount; | |
} | |
public override nint GetComponentCount (UIPickerView pickerView) | |
{ | |
Console.WriteLine ("GetComponentCount"); | |
return 1; | |
} | |
public override nint GetRowsInComponent (UIPickerView pickerView, nint component) | |
{ | |
Console.WriteLine ("GetRowsInComponent"); | |
return rowCount; | |
} | |
} | |
public class PickerViewFailExampleViewController2 : UIViewController | |
{ | |
UITextView SomeTextView { get; set; } | |
UIPickerView SomePickerView { get; set; } | |
public PickerViewFailExampleViewController2() | |
{ | |
var pickerDelegate = new SomePickerViewDelegate (); | |
var pickerDataSource = new SomePickerViewDataSource (SomePickerViewDelegate.Items.Count); | |
SomePickerView = new UIPickerView () { | |
Delegate = pickerDelegate, | |
DataSource = pickerDataSource, | |
}; | |
SomeTextView = new UITextView () { | |
InputView = SomePickerView, | |
Text = "picker test", | |
}; | |
View.Add (SomeTextView); | |
} | |
public override void ViewDidLoad () | |
{ | |
base.ViewDidLoad (); | |
View.BackgroundColor = UIColor.LightGray; | |
SomeTextView.Frame = new CGRect (new CGPoint (50f, 50f), new CGSize (View.Bounds.Width - 100f, 25f)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment