UIPickerViewの選択肢をアイコン+ラベルにする
前回の続き。
UIPickerViewの選択肢をアイコン+ラベルにする。
やりかたとしては本家SDK同様にUITableViewCellを使うようにする。
今回はUIPickerViewModel*1を継承してclassを作る。
public class TestData : UIPickerViewModel {
string names = new string {"aaaa", "bbbbb", "ccccc"};
public override int GetComponentCount (UIPickerView picker)
{
return 1;
}
public override int GetRowsInComponent (UIPickerView picker, int component)
{
return names.Length;
}
public override string GetTitle (UIPickerView picker, int row, int component)
{
return names[row];
}
public override UIView GetView (UIPickerView picker, int row, int component, UIView view)
{
UITableViewCell cell = view as UITableViewCell;
if (cell == null){
cell = new UITableViewCell(UITableViewCellStyle.Default, null);
cell.BackgroundColor = UIColor.Clear;
SizeF s = picker.RowSizeForComponent(component);
cell.Bounds = new System.Drawing.RectangleF(0,0,s.Width, s.Height);
cell.ImageView.Image = UIImage.FromFile("画像.png");
}
else {
cell.PrepareForReuse();
}
cell.TextLabel.Text = this.GetTitle(picker, row, component);
return cell;
}
}
インスタンス化したUITableViewCellは再利用される模様。よって
cell.PrepareForReuse();
をするのがミソらしい。
そしてこれをインスタンス化してUIPickerViewのModelプロパティに割り当てる。
picker.Model = new TestData ();
*1:UIPickerViewDelegateでもいいはず