Jeff
Jeff

Reputation: 850

Image and text in UIPickerView

Im trying to insert an image to the left of the text (inside) a UIPickerView. My app is crashing, it works without the image array but I'm unsure what I'm doing wrong. Im very new please help. thanks!

.h

#import <UIKit/UIKit.h>
#define kFittingComponent 0
#define kStyleComponent 1
#define kSizeComponent 2

@interface ViewController : UIViewController{
NSArray *PickerData;
NSMutableArray *imageData;
}

@property ( retain, nonatomic) IBOutlet UIPickerView *Picker;
@property ( retain, nonatomic) NSArray *PickerData;
@property ( retain, nonatomic) NSMutableArray *imageData;
@end

.m

#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize  Picker,PickerData,imageData;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {}
return self;}

- (void)viewDidLoad
{[super viewDidLoad];

NSArray *array = [[NSArray alloc] initWithObjects:@"blank",@"one",@"two",nil];
self.PickerData = array;

NSMutableArray *imageArray = [[NSMutableArray alloc]initWithObjects:@"image1.png",@"image2.png", nil];
self.imageData = imageArray;}

-(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
{
UIView *pickerCustomView = (id)view;
UILabel *pickerViewLabel;
UIImageView *pickerImageView;

if (!pickerCustomView) {
pickerCustomView= [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,
[pickerView rowSizeForComponent:component].width - 10.0f, [pickerView       rowSizeForComponent:component].height)];
pickerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 35.0f, 35.0f)];
pickerViewLabel= [[UILabel alloc] initWithFrame:CGRectMake(37.0f, -5.0f,
[pickerView rowSizeForComponent:component].width - 10.0f, [pickerView rowSizeForComponent:component].height)];

[pickerCustomView addSubview:pickerImageView];
[pickerCustomView addSubview:pickerViewLabel];
}

pickerImageView.image = imageData[row];
pickerViewLabel.backgroundColor = [UIColor clearColor];
pickerViewLabel.text = PickerData[row];
pickerViewLabel.font = [UIFont fontWithName:@"ChalkboardSE-Regular" size:20];
return pickerCustomView;}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

int long select = row;
if (select == 0){}
else
if (select == 1) {}
else
if (select == 2) {}
}
@end

Upvotes: 2

Views: 2048

Answers (1)

HAS
HAS

Reputation: 19853

Your imageData array doesn't contain the image data, but NSStrings with the file name of the images.

You now have two ways to change your code:

  1. Load the images into the array (have them in your array all the time).
  2. Store only the paths in the array (like you already do) and load the images when you need it. (And probably rename the array since it doesn't hold image data)

You might think that the second one has performance impacts since you first need to load that image from a file, then it gets decoded and displayed. But if you use UIImage's imageNamed: class method the images get chached. On the other hand if you go with the first solution you might end up wasting memory (I imagine those images are very small, so this shouldn't be the case).

For the first sution you would do the following:

NSArray *imageData = @[[UIImage imageNamed:@"image1.png"], [UIImage imageNamed:@"image2.png"]];

Nothing else needs to be changed (though I would name the array images since imageData sounds like it contained NSData objects.

For the second option you would change

pickerImageView.image = imageArray[row];

to

pickerImageView.image = [UIImage imageNamed:imageArray[row]];

Nothing else to change here either but I would rename imageData to imageFileNames.

Remember that the better you choose your variable names the better not only others but you yourself can understand what's going on and where might be an error.

Upvotes: 1

Related Questions