我在网上看了看,但在这上面找不到多少!
我想知道你如何将图像放在UIPicker中,以便每行都有不同的图像。
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize pickerContent;
@synthesize p1;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//////////
- (NSMutableArray *)pickerContent
{
if(!pickerContent) {
pickerContent = [[NSMutableArray alloc] initWithObjects:
[UIImage imageNamed:@"one.jpg"],
[UIImage imageNamed:@"two.jpg"],
[UIImage imageNamed:@"three.jpg"],
[UIImage imageNamed:@"four.jpg"],
[UIImage imageNamed:@"five.jpg"], nil];
}
return pickerContent;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
if ([pickerView isEqual:p1])
{
return 4;
}
return 0;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if ([pickerView isEqual:p1])
{
if (component == R1)return [self.pickerContent count];
if (component == R2)return [self.pickerContent count];
if (component == R3)return [self.pickerContent count];
if (component == R4)return [self.pickerContent count];
}
return 0;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if ([pickerView isEqual:p1])
{
if (component== R1)return [pickerContent objectAtIndex:row];
if (component== R2)return [pickerContent objectAtIndex:row];
if (component== R3)return [pickerContent objectAtIndex:row];
if (component== R4)return [pickerContent objectAtIndex:row];
}
return 0;
}
@end
答案 0 :(得分:4)
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"your image number %@.png", (long)row]];
UIImageView *temp = [[UIImageView alloc] initWithImage:img];
temp.frame = CGRectMake(-70, 10, 60, 40);
UILabel *channelLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, -5, 80, 60)];
channelLabel.text = [NSString stringWithFormat:@"%@", [your array objectAtIndex:row]];
channelLabel.textAlignment = UITextAlignmentLeft;
channelLabel.backgroundColor = [UIColor clearColor];
UIView *tmpView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 110, 60)];
[tmpView insertSubview:temp atIndex:0];
[tmpView insertSubview:channelLabel atIndex:1];
return tmpView;
}
答案 1 :(得分:2)
我在这里以我实施的方式分享整个例子。
请检查CustomPickerViewController.h
#import <UIKit/UIKit.h>
@interface CustomPickerViewController : UIViewController
<UIPickerViewDataSource , UIPickerViewDelegate>{
IBOutlet UIPickerView *picker;
IBOutlet UILabel *winLabel;
NSArray *column1;
NSArray *column2;
NSArray *column3;
NSArray *column4;
NSArray *column5;
}
@property (nonatomic,retain) UIPickerView *picker;@property (nonatomic,retain) UILabel *winLabel;@property (nonatomic,retain) NSArray *column1;@property (nonatomic,retain) NSArray *column2;@property (nonatomic,retain) NSArray *column3;@property (nonatomic,retain) NSArray *column4;@property (nonatomic,retain) NSArray *column5;
-(IBAction)spin;
CustomPickerViewController.m
-(void)spin
{
BOOL win = NO;
int numInRow = 1;
int lastVal = -1;
for(int i = 0; i<5 ;i++)
{
int newValue = random() % [self.column1 count];
if(newValue == lastVal)
numInRow++ ;
else
numInRow = 1;
lastVal = newValue;
[picker selectRow:newValue inComponent:i animated:YES];
[picker reloadComponent:i];
if(numInRow >= 3)
win = YES;
}
if(win)
winLabel.text = @"WIN!";
else
winLabel.text = @"";
}
- (void)viewDidLoad {
UIImage *seven = [UIImage imageNamed:@"seven.png" ];
UIImage *bar = [UIImage imageNamed:@"bar.png" ];
UIImage *crown = [UIImage imageNamed:@"crown.png" ];
UIImage *cherry = [UIImage imageNamed:@"cherry.png" ];
UIImage *lemon = [UIImage imageNamed:@"lemon.png" ];
UIImage *apple = [UIImage imageNamed:@"apple.png"];
for(int i =1; i<=5; i++)
{
UIImageView *sevenView = [[UIImageView alloc] initWithImage:seven];
UIImageView *barView = [[UIImageView alloc] initWithImage:bar];
UIImageView *crownView = [[UIImageView alloc] initWithImage:crown];
UIImageView *cherryView = [[UIImageView alloc] initWithImage:cherry];
UIImageView *lemonView = [[UIImageView alloc] initWithImage:lemon];
UIImageView *appleView = [[UIImageView alloc] initWithImage:apple];
NSArray *imageViewArray = [[NSArray alloc] initWithObjects:
sevenView,barView, crownView,cherryView,lemonView,appleView,nil];
NSString *fieldName = [[NSString alloc] initWithFormat:@"column%d",i];
[self setValue:imageViewArray forKey:fieldName];
[fieldName release];
[imageViewArray release];
[sevenView release];
[barView release];
[crownView release];
[cherryView release];
[lemonView release];
[appleView release];
}
srandom(time(NULL));}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 5;}
-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component{
return [self.column1 count];}
-(UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view{
NSString *arrayName = [[NSString alloc]initWithFormat:@"column%d",component+1];
NSArray *array = [self valueForKey:arrayName];
return [array objectAtIndex:row];}
以这种方式实施。这段代码对我来说很好。请检查并告诉我它没问题。
答案 2 :(得分:0)
而不是
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
您可以使用以下数据源方法在行中添加自定义对象 第1步:创建UIImageView对象 第2步:为其分配适当的图标图像 第3步:返回您的UIImageView对象
- (UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component
{
UIImageView *myIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myIconName"]];
[myIcon setFrame:CGRectMake(0, 0, 50, 50)];
return myIcon;
}