如何自定义UIPickerView?

时间:2016-10-04 00:41:37

标签: ios objective-c uipickerview

我正在使用UIPickerview。我想把我的观点视为:

View as needed

但我得到了:

View as I am getting

有没有办法让selectionIndicator靠近字符串?并隐藏将在selectionIndicator上方的数字?

1 个答案:

答案 0 :(得分:0)

lakhan bhojwan尝试下面的答案。我创建了一个样本。它完美无缺。

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>

@property (strong, nonatomic) IBOutlet UIPickerView *pickerCustom;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()
{
   NSMutableArray *arrPicker;
}

@end

@implementation ViewController

@synthesize pickerCustom;


- (void)viewDidLoad
{
  [super viewDidLoad];
  // Do any additional setup after loading the view, typically from a nib.
   arrPicker = [[NSMutableArray alloc]initWithObjects:@"Rate this page",@"10",@"9",@"8",@"7",@"6",@"5",@"4",@"3",@"2",@"1",nil];       
   [pickerCustom selectRow:1 inComponent:0 animated:YES];   
}


- (void)didReceiveMemoryWarning
{
   [super didReceiveMemoryWarning];
   // Dispose of any resources that can be recreated.
}


// The number of columns of data
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
   return 1;
}

// The number of rows of data
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
   return [arrPicker count];
}

// The data to return for the row and component (column) that's being passed in
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
   return arrPicker[row];
}

@end

以下输出截图

enter image description here