I'm having problems while displaying images in a UIPickerView. The images appear and disappear randomly and never show in the "middle" row of the picker. This is the code
#interface ShowImagesPVC () <UIPickerViewDataSource, UIPickerViewDelegate>
#property (strong, nonatomic) NSMutableArray *arrayOfImages;
#end
#implementation ShowImagesPVC
- (void)viewDidLoad
{
[super viewDidLoad];
_arrayOfImages = [NSMutableArray array];
for(int i = 1; i < 36; i++){
NSString *tmpString = [NSString stringWithFormat:#"%d.png",i];
UIImageView *myIcon = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
[myIcon setImage:[UIImage imageNamed:tmpString]];
[_arrayOfImages addObject:myIcon];
}
UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(-5, 0, 315, 200)];
myPickerView.delegate = self;
myPickerView.dataSource = self;
myPickerView.showsSelectionIndicator = YES;
[self.view addSubview:myPickerView];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
return [_arrayOfImages objectAtIndex:row];
}
- (NSInteger)numberOfComponentsInPickerView: (UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return _arrayOfImages.count;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 45;
}
#end
This is a pic of the Picker just loaded
and this is a pic of the Picker a bit rolled
There should be the icons seen in the first pic over the middle row, but there is nothing.
The icon size is 64x64, so i tried to make the first one 40x40 (but doesn't change anything because it should be in the middle row in the first pic, but as you can se it's blank)
Instead of making your _arrayOfImages an array of image views, make it an array of images, and then do this in viewForRow:forComponent:reusingView:,
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
iv.image = _arrayOfImages[row];
return iv;
}
Related
Similar to how amazon does for selecting quantity of an item. I want it to then save the number to display in another tableview. Here is the code I have for my .m file. When I click on the text field the picker is empty.
#interface ApparelViewController ()
#property(nonatomic, readonly) NSInteger numberOfComponents;
#end
#implementation ApparelViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIPickerView *picker = [[UIPickerView alloc] init];
self.quantity.inputView = picker;
//creating the arrays for the values in the picker wheel
self.quantity0 = #[#1, #2, #3, #4, #5];
}
//custom picker in text field
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return self.quantity0.count;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow: (NSInteger)row forComponent:(NSInteger)component {
return self.quantity0[row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
self.quantity.text = self.quantity0[row];
[self.quantity resignFirstResponder];
}
Looks like you forgot to set the UIPicker's delegate to self.
In your viewDidLoad, try this:
picker.delegate = self;
I have a UIPickerView filled with an array of tree types. I also have an empty UIImageView below that I would like to change upon the selection of the picker view. I got a label to change with the selection but cannot get images to change. Here is what I have so far
EDIT
EDIT : Now working! Correct Code below
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
dataArray = [[NSArray alloc]initWithObjects:#"Birch", #"Elm", #"Maple", #"Oak", #"White Pine",#"Willow", nil];
myPicker.delegate = self;
imageArray = [[NSArray alloc]initWithObjects:#"birch_river.png",#"elm_american.png",#"maple_bigtooth.png", #"oak_bur.png", #"white_pine.png", #"oak_willow.png", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [dataArray count];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
myLabel.text = [dataArray objectAtIndex:row];
myImage.image = [UIImage imageNamed:[imageArray objectAtIndex:row]];
}
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent: (NSInteger)component
{
return [dataArray objectAtIndex:row];
}
#end
What you can do is, have an array filled with the image names (Ex: IMAGES_ARRAY with image names).
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
myLabel.text = [dataArray objectAtIndex:row];
YOUR_IMG_VIEW = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[IMAGES_ARRAY ObjectAtIndex.row]]];
}
I am trying to create a PickerView view programatically.
I have followed all of the necessary steps however the picker just simply appears and then disappears straight away. No data can be seen.
I am setting the delegate and data source and I have implemented the necessary methods.
Here is my code:
in .h
#interface NJATimeEntryViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
#property (nonatomic, strong) UIPickerView *customerPicker;
#property (nonatomic, strong) NSArray *pickerTitles;
#end
in .m
- (void)viewDidLoad
{
[super viewDidLoad];
self.pickerTitles = #[#"ONE", #"TWO", #"THREE"];
self.customerPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)];
self.customerPicker.dataSource = self;
self.customerPicker.delegate = self;
[self.view addSubview:self.customerPicker];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return self.pickerTitles.count;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [[self pickerTitles] objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(#"Selected Row %d", row);
}
All help is appreciated thanks.
FIXED:
All i did was set the background color of the view controllers view and now it works. I see this as a bug as no where is it stated that the background color has to be set.
Set Some Background color ( except black )of your view, then try,
self.view.backgroundColor=[UIColor yellowColor];
i am developing the one example of UIPICKERVIEW and i want to disable or off the shadow effect and rolling effect but not able to do it.do you have any idea for that ?
Source code:
pickerViewController.h file
#import <UIKit/UIKit.h>
#interface pickerViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>
{
UIPickerView *languageSelect;
NSMutableArray *pickerData;
}
#property (nonatomic, retain) UIPickerView *languageSelect;
#property (nonatomic, retain) NSArray *pickerData;
#end
pickerViewController.m file
#import "pickerViewController.h"
#interface pickerViewController ()
#end
#implementation pickerViewController
#synthesize languageSelect, pickerData;
- (void)viewDidLoad
{
[super viewDidLoad];
pickerData= [[NSMutableArray alloc] initWithObjects:#"English",#"Spanish",#"French",#"Greek",
#"Japaneese",#"Korean",#"Hindi",#"English",#"Spanish",#"French",#"Greek",
#"Japaneese",#"Korean",#"Hindi", nil];
languageSelect = [[UIPickerView alloc] initWithFrame:CGRectMake(10, 50, 300, 1000)];
languageSelect.showsSelectionIndicator = YES;
languageSelect.hidden = NO;
languageSelect.delegate = self;
[self.view addSubview:languageSelect];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView; {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component;
{
return [pickerData count];
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *pickerLabel = (UILabel *)view;
NSString* labelText = [NSString stringWithFormat:#"%#",[pickerData objectAtIndex:row]];
if (pickerLabel == nil) {
CGSize size = [labelText sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(250, 216) lineBreakMode:NSLineBreakByCharWrapping];
CGRect frame = CGRectMake(0.0, 0.0, 250, size.height + 25);
pickerLabel = [[UILabel alloc] initWithFrame:frame];
[pickerLabel setTextAlignment:NSTextAlignmentLeft];
[pickerLabel setBackgroundColor:[UIColor clearColor]];
[pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];
[pickerLabel setLineBreakMode:NSLineBreakByWordWrapping];
// trying to mess with the UIView of the row itself... to no avail
//CGRect rowFrame = view.frame;
//NSLog(#"%f",rowFrame.size.height);
//rowFrame.size.height = size.height + 25;
//view.frame = rowFrame;
}
[pickerLabel setText:[pickerData objectAtIndex:row]];
//NSLog(#"%f",[pickerView rowSizeForComponent:component].height);
return pickerLabel;
}
-(NSString*) pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [pickerData objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;
{
}
Output:
I want to do something like this...!!
This is a lot of custom work. If you want to achieve that look, you are probably better of trying to use a UITableView. You'll still have to put in quite some effort though.
In pic you can see structure of UIPickerView.
http://i.stack.imgur.com/QFAKV.png
Maybe removing some layers help you
http://i.stack.imgur.com/C2xDO.png
I have a UIPickerView that works perfectly when placed on the main view. However as soon as I place it into a sub UIView (To help with visual placement of elements) the interaction stops completely. The UIPickerView still receives all data correctly, just no interaction.
If I take the UIPickerView back out of the UIView, using storyboard, it works right away.
- (void)viewDidLoad
{
[super viewDidLoad];
// Add provinces into array
[self.provinceView setHidden:YES];
isVisible = NO;
self.provinceArray = [[NSArray alloc] initWithObjects:#"Alberta",#"British Columbia",#"Manitoba",#"New Brunswick",#"Newfoundland and Labrador",#"Northwest Territories",#"Nova Scotia",#"Nunavut",#"Ontario",#"Prince Edward Island",#"Quebec",#"Saskatchewan",#"Yukon",nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [self.provinceArray count];
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 0,(pickerView.frame.size.width-40), 44)];
label.textColor = [UIColor colorWithRed:0.035 green:0.094 blue:0.345 alpha:1.0];
label.font = [UIFont fontWithName:#"HelveticaNeue" size:14];
label.text = [self.provinceArray objectAtIndex:row];
return label;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
self.provinceLabel.text = [self.provinceArray objectAtIndex:row];
}
Check this post
-->
UIPickerView in UITableView doesn't react to touch in lower half of cell
did u bring the subview to foreground?