Populate UIPickerView with images - ios

I'm trying to populate a UIPickerView with images.
Here is where i create programmatically the PickerView and i fill a NSMutableArray with 35 images called 1.png, 2.png ..., 35.png
#interface ShowImagesPVC () <UIPickerViewDataSource, UIPickerViewDelegate>
#property (strong, nonatomic) NSMutableArray *arrayOfImages;
#end
- (void)viewDidLoad
{
[super viewDidLoad];
for(int i = 1; i < 36; i++){
NSString *tmpString = [NSString stringWithFormat:#"%d.png",i];
[_arrayOfImages addObject:[[UIImageView alloc]initWithImage:[UIImage imageNamed:tmpString]]];
}
UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(-5, 0, 315, 200)];
myPickerView.delegate = self;
myPickerView.dataSource = self;
myPickerView.showsSelectionIndicator = YES;
[self.view addSubview:myPickerView];
}
now i've implemented these methods
- (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;
}
The problem is that the UIPicker is created but it's empty, so i tried to NSLog the method - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view and it seems it's never called.
Any idea why?

try this
- (void)viewDidLoad
{
[super viewDidLoad];
_arrayOfImages = [NSMutableArray array];
for(int i = 1; i < 36; i++){
NSString *tmpString = [NSString stringWithFormat:#"%d.png",i];
[_arrayOfImages addObject:[[UIImageView alloc]initWithImage:[UIImage imageNamed:tmpString]]];
}
UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(-5, 0, 315, 200)];
myPickerView.delegate = self;
myPickerView.dataSource = self;
myPickerView.showsSelectionIndicator = YES;
[self.view addSubview:myPickerView];
}

Related

Add Static Row To UIPickerView

In my app, I query Parse.com to get a list of groups to populate a UIPickerView. I want to add one more option to the UIPickerView at the very top that is called "Create New Group", in which it will pull up a UITextField to make a new group and move from there. How do I go about adding this to all the other fields? Here is how I have it created so far.
-(void)addPickerView{
__block NSMutableArray *arr = [[NSMutableArray alloc] init];
PFQuery *rejectedNumber = [PFQuery queryWithClassName:#"Group"];
[rejectedNumber orderByAscending:#"GroupName"];
[rejectedNumber findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!objects) {
// Did not find any UserStats for the current user
NSLog(#"NotFound");
} else {
pickerArray = objects;
[myPickerView reloadAllComponents];
}
}];
//pickerArray = GoGo;
self.theView.signUpView.additionalField.delegate = self;
[self.theView.signUpView.additionalField setPlaceholder:#"Choose Group"];
myPickerView = [[UIPickerView alloc]init];
myPickerView.dataSource = self;
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
initWithTitle:#"Done" style:UIBarButtonItemStyleDone
target:self action:#selector(finishIt)];
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:
CGRectMake(0, self.view.frame.size.height-
self.theView.signUpView.additionalField.frame.size.height-50, 320, 50)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
NSArray *toolbarItems = [NSArray arrayWithObjects:
doneButton, nil];
[toolBar setItems:toolbarItems];
self.theView.signUpView.additionalField.inputView = myPickerView;
self.theView.signUpView.additionalField.inputAccessoryView = toolBar;
}
#pragma mark - Picker View Data source
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component{
return [pickerArray count];
}
#pragma mark- Picker View Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:
(NSInteger)row inComponent:(NSInteger)component{
[self.theView.signUpView.additionalField setText:self.theGroup];
}
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
PFObject *object = pickerArray[row];
//Assuming "Category" is a string here for your title
self.theGroup = object[#"GroupName"];
return object[#"GroupName"];
}
You can add achieve that in two ways:
Add an extra object at the first index of your array for showing "Create New Group"
You can use tweak the datasource and delegate methods of UIPickerView. Like:
-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
return [pickerArray count] + 1; // +1 for the additional row
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:
(NSInteger)row inComponent:(NSInteger)component
{
if (row == 0)
{
// Create New Group clicked
}
else
{
// Some other option selected
}
}
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *groupName = "Create New Group";
if (row != 0)
{
PFObject *object = pickerArray[row - 1]; // -1 to handle the array index
self.theGroup = object[#"GroupName"];
groupName = object[#"GroupName"];
}
return groupName;
}

UITapGestureRecognizer in custom UITableViewCell within UIPicker is not working

I am trying to implement multiselect option in UIPicker using setAccessoryType in UITableViewCell and following #Suda's blog
Now the Problem is : Not getting any error but UITapGestureRecognizer is not getting recognized.
toggleSelection: is not getting called (not able to add / remove object in selectedItems).
I have searched for this problem in SO but didn't get any solution. nothing is working for me.
I have used following code
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UIPickerViewDelegate, UIPickerViewDataSource>
{
NSMutableArray *selectedItems;
NSArray *datasource;
}
#property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
#end
ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
datasource = [[NSArray alloc] initWithObjects:#"A", #"B", #"C", #"D", #"E", nil];
self.pickerView.delegate = self;
self.pickerView.dataSource = self;
selectedItems = [[NSMutableArray alloc] init];
}
-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return datasource.count;
}
-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return datasource[row];
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UITableViewCell *cell = (UITableViewCell *)view;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
[cell setBackgroundColor:[UIColor clearColor]];
[cell setBounds:CGRectMake(0, 0, cell.frame.size.width - 20, 44)];
cell.tag = row;
UITapGestureRecognizer * singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(toggleSelection:)];
singleTapGestureRecognizer.numberOfTapsRequired = 1;
[cell setUserInteractionEnabled:YES];
[cell addGestureRecognizer:singleTapGestureRecognizer];
// Also tried with [cell.contentView addGestureRecognizer:singleTapGestureRecognizer];
}
if ([selectedItems indexOfObject:[NSNumber numberWithInt:row]] != NSNotFound) {
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
} else {
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
cell.textLabel.text = [datasource objectAtIndex:row];
return cell;
}
- (void)toggleSelection:(UITapGestureRecognizer *)recognizer {
NSNumber *row = [NSNumber numberWithInt:recognizer.view.tag];
NSUInteger index = [selectedItems indexOfObject:row];
if (index != NSNotFound) {
[selectedItems removeObjectAtIndex:index];
[(UITableViewCell *)(recognizer.view) setAccessoryType:UITableViewCellAccessoryNone];
} else {
[selectedItems addObject:row];
[(UITableViewCell *)(recognizer.view) setAccessoryType:UITableViewCellAccessoryCheckmark];
}
}
Any help would be appreciable.

Black inside the pickerview

It is all black in the UIPickerView. I do not know what is happening. I do not have warnings and errors and when I try to bring up the picker view it just shows black. Did I do something wrong and is there a possibility that I have forgoten something.
This is my code.
#synthesize stillImageOutput, imagePreview, captureImage, cameraSwitch, pickerViewContainer;
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
if (component == genre)
return [arraygenre count];
return 0;
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (component == genre)
return [arraygenre objectAtIndex:row];
return 0;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (IBAction)saveButton:(id)sender {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
pickerViewContainer.frame = CGRectMake(0, 307, 320, 261);
[UIView commitAnimations];
}
- (IBAction)closeButton:(id)sender {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
pickerViewContainer.frame = CGRectMake(0, 800, 320, 261);
[UIView commitAnimations];
}
Update
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
FrontCamera = NO;
cameraSwitch.selectedSegmentIndex = 1;
captureImage.hidden = YES;
arraygenre = [[NSMutableArray alloc] init];
[arraygenre addObject:#"blue"];
[arraygenre addObject:#"yellow"];
[arraygenre addObject:#"white"];
[arraygenre addObject:#"red"];
[arraygenre addObject:#"purple"];
[arraygenre addObject:#"Others"];
pickerViewContainer.frame = CGRectMake(0, 800, 320, 261);
}
- (void)viewDidAppear:(BOOL)animated {
[self initializeCamera];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//fetch Category Name from the array used to fill the Picker View
NSString *categoryName= [arraygenre objectAtIndex:row];
NSString *fPath = [documentsDirectory stringByAppendingPathComponent:categoryName];
NSFileManager *fileManager=[[NSFileManager alloc]init];
[fileManager createDirectoryAtPath:fPath withIntermediateDirectories:YES attributes:nil error:nil];
UIImage *image = captureImage.image;
NSData *data = UIImagePNGRepresentation(image);
[data writeToFile:fPath atomically:YES];
}
In my .h file i have
#define genre 0
#interface CameraSave : UIViewController <UIPickerViewDataSource,UIPickerViewDelegate,UIImagePickerControllerDelegate> {
BOOL FrontCamera;
BOOL haveImage;
IBOutlet UIPickerView *SaveTopicker;
NSMutableArray *arraygenre;
}
have you allocate & intialize pickerview?? and is delegate is set?
try this in viewDidLoad
SaveTopicker.delegate = self;
SaveTopicker.showsSelectionIndicator = YES;
[self.view addSubview:SaveTopicker];
for more reference check this Tutorial1, Tutorial 2
- (void)viewDidLoad
{
[super viewDidLoad];
array=[[NSMutableArray alloc]initWithObjects:#"Reading",#"Music",#"Cricket",#"Movie",#"Travelling",nil];
[hobbyPicker selectRow:1 inComponent:0 animated:NO];
}
-(void)buttonHobby:(id)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
hobbyPicker.frame = CGRectMake(0, 244, 320, 216);
pickerToolbar.frame = CGRectMake(0,200, 320, 44);
[UIView commitAnimations];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
NSUInteger numRows = 5;
return numRows;
}
// tell the picker how many components it will have
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [array objectAtIndex:row];
}
// tell the picker the width of each row for a given component
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)componen
{
CGFloat componentWidth = 0.0;
componentWidth = 135.0;
return componentWidth;
}
Try this code acording to your need... hope this wprks for u..

Customize UIPicker-Insert Images

As seen image below, I am trying to customize my picker. I have successfully added images on my picker but not affected all other pickers. I have put my code as well for reference.
- (void)loadView
{
picker1 = [[UIPickerView alloc] initWithFrame:CGRectMake(30, 200, 250, 250)];
picker1.delegate = self;
picker1.dataSource = self;
picker1.showsSelectionIndicator = YES;
picker2 = [[UIPickerView alloc] initWithFrame:CGRectMake(390, 200, 250, 250)];
picker2.delegate = self;
picker2.dataSource = self;
picker2.showsSelectionIndicator = YES;
picker3 = [[UIPickerView alloc] initWithFrame:CGRectMake(750, 200, 250, 250)];
picker3.delegate = self;
picker3.dataSource = self;
picker3.showsSelectionIndicator = YES;
self.view = [[UIView alloc] initWithFrame:CGRectZero];
[self.view addSubview:picker1];
[self.view addSubview:picker2];
[self.view addSubview:picker3];
UIImage *seven = [UIImage imageNamed:#"seven.png"];
UIImage *bar = [UIImage imageNamed:#"bar.png"];
for(int i=1; i<=4; i++)
{
UIImageView *sevenView = [[UIImageView alloc] initWithImage:seven];
UIImageView *barView = [[UIImageView alloc] initWithImage:bar];
NSArray *imageViewArray = [[NSArray alloc] initWithObjects:
sevenView,barView,nil];
NSString *fieldName = [[NSString alloc] initWithFormat:#"column%d",i];
[self setValue:imageViewArray forKey:fieldName];
}
}
-(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];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
}
#pragma mark UIPickerViewDelegate methods
- (NSString*)pickerView:(UIPickerView*)pv titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [NSString stringWithFormat:#"%d",row];
}
#pragma mark UIPickerViewDataSource methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pv
{
return 4;
}
- (NSInteger)pickerView:(UIPickerView*)pv numberOfRowsInComponent:(NSInteger)component
{
return 2;
}
#end
-(UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view
{
NSArray *array = nil;
if (pickerView == picker1) {
array = [self valueForKey:#"column1"];
}
else if (pickerView == picker2) {
array = [self valueForKey:#"column2"];
}
else if (pickerView == picker3) {
array = [self valueForKey:#"column3"];
}
return [array objectAtIndex:row];
}

Custom UIPicker

I have been working on the app, that has 3 uipickers. I want to implement images in each uipicker. However as seen in the image attached below, it just affects only one uipicker, not all of them.I would like to know how I could able to custom all uipicker elements shown in the image
- (void)loadView
{
picker1 = [[UIPickerView alloc] initWithFrame:CGRectMake(30, 200, 250, 250)];
picker1.delegate = self;
picker1.dataSource = self;
picker1.showsSelectionIndicator = YES;
picker1.tag=1;
picker2 = [[UIPickerView alloc] initWithFrame:CGRectMake(390, 200, 250, 250)];
picker2.delegate = self;
picker2.dataSource = self;
picker2.showsSelectionIndicator = YES;
picker2.tag=2;
picker3 = [[UIPickerView alloc] initWithFrame:CGRectMake(750, 200, 250, 250)];
picker3.delegate = self;
picker3.dataSource = self;
picker3.showsSelectionIndicator = YES;
picker3.tag=3;
self.view = [[UIView alloc] initWithFrame:CGRectZero];
self.view.backgroundColor=[UIColor whiteColor];
[self.view addSubview:picker1];
[self.view addSubview:picker2];
[self.view addSubview:picker3];
UIImage *seven = [UIImage imageNamed:#"seven.png"];
UIImage *bar = [UIImage imageNamed:#"bar.png"];
for(int i=1; i<=4; i++)
{
UIImageView *sevenView = [[UIImageView alloc] initWithImage:seven];
UIImageView *barView = [[UIImageView alloc] initWithImage:bar];
NSArray *imageViewArray = [[NSArray alloc] initWithObjects:
sevenView,barView,nil];
NSString *fieldName = [[NSString alloc] initWithFormat:#"column%d",i];
[self setValue:imageViewArray forKey:fieldName];
}
}
-(UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view
{
//NSLog(#"%d",component);
NSLog(#"tag: %d",pickerView.tag);
if(pickerView.tag==1)
{ NSString *arrayName = [[NSString alloc] initWithFormat:#"column%d",component+1];
NSArray *array1 = [self valueForKey:arrayName];
return [array1 objectAtIndex:row];
}
else if(pickerView.tag==2)
{ NSString *arrayName = [[NSString alloc] initWithFormat:#"column%d",component+1];
NSArray *array2 = [self valueForKey:arrayName];
return [array2 objectAtIndex:row];
}
else
{ NSString *arrayName = [[NSString alloc] initWithFormat:#"column%d",component+1];
NSArray *array3 = [self valueForKey:arrayName];
return [array3 objectAtIndex:row];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
}
#pragma mark UIPickerViewDelegate methods
- (NSString*)pickerView:(UIPickerView*)pv titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [NSString stringWithFormat:#"%d",row];
}
#pragma mark UIPickerViewDataSource methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pv
{
return 4;
}
- (NSInteger)pickerView:(UIPickerView*)pv numberOfRowsInComponent:(NSInteger)component
{
return 2;
}
#end
Views can only have one parent. But you are trying to use each view for 3 parents (the 3 pickers). Regardless of the picker, you try to use the same set of image views for component X of each picker. That won't work.
You need three sets of arrays, not just the one set. In other words, you can't share views between the pickers.

Resources