Have a UIPicker which should display two components (columns in the picker view), but is only displaying one.
Can't find the error; it builds correctly. Second component is empty; no data displayed.
InstaEmailViewController.h
#import <UIKit/UIKit.h>
#interface InstaEmailViewController : UIViewController
<UIPickerViewDataSource, UIPickerViewDelegate> {
NSArray* activities_ ;
NSArray* feelings_ ;
}
InstaEmailViewController.m
#import "InstaEmailViewController.h"
#implementation InstaEmailViewController
- (void)dealloc
{
[activities_ release];
[feelings_ release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
activities_ = [[NSArray alloc] initWithObjects:#"sleeping", #"working", #"thinking", #"crying", #"begging", #"leaving", #"shopping", #"hello worlding", nil];
feelings_ = [[NSArray alloc] initWithObjects: #"awesome", #"sad", #"ambivalent", #"nauseous",#"psyched", #"confused", #"hopeful", #"anxious", nil];
[super viewDidLoad];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (component == 0) {
return [activities_ objectAtIndex:row];
} else {
[feelings_ objectAtIndex:row];
}
return nil;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component {
if (component ==0) {
return [activities_ count];
} else {
return [feelings_ count];
}
}
#end
You are not returning the value in the pickerView:titleForRow:inComponent: method for the second component.
if (component == 0) {
return [activities_ objectAtIndex:row];
} else {
[feelings_ objectAtIndex:row];
}
You should do
return [feelings_ objectAtIndex:row];
Related
I'm new to iOS developing, I have a UITableViewController and I want to load NSMutableArray data in the table, but it does load data and shows an empty table. It's the code:
#import "PhotosTableViewController.h"
#implementation PhotosTableViewController
NSMutableArray *photos;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
photos = [[NSMutableArray alloc] init];
Photo *pic = [[Photo alloc] init];
[pic setName:#"over look"];
[pic setFilename:#"overlook.png"];
[pic setNotes:#"this is notw!"];
[photos addObject:pic];
pic = [[Photo alloc] init];
[pic setName:#"olives"];
[pic setFilename:#"olives.png"];
[pic setNotes:#"this is notw!"];
[photos addObject:pic];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [photos count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"PhotoCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
Photo *current=[photos objectAtIndex:indexPath.row];
cell.textLabel.text=[current name];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
}
#end
Add [self.view reloadData]; at the end of your viewDidLoad implementation.
If that does not help already, I suspect that your viewController is actually not of type PhotoTableViewController. Make sure that is correctly setup. If done via InterfaceBuilder, check the type of the viewController and make sure it says PhotoTableViewController.
Le Sigh you'd be surprised how many UITableView questions are solved with 'Make your class the delegate and datasource.'. In IB, drag the outlets to files owner. In code, set tableView.delegate = self; and tableView.datasource = self;
I have got UIPickerview in my application working fine.
I want to change the default selection behavior of the UIPickerview, i mean i have a done button in the UIToolbar. The toolbar is added to the UIPickerview.
Whenever the user taps on the "Done" button, I want to consider the highlighted entry as selected.
That means user can move up and down , when the user taps on the "Done" button, i want to consider the selection now
Please let me know , how can I fetch the currently selected row for the UIPickerview when tapped "Done" button
Note: I have 3 pickers in my view ( i mean i want to have pickerview tag and pickerview selected row)
//Suppose you have created three picker views as firstPickerView, secondPickerView and ThirdPickerView
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if(pickerView == firstPickerView)
{
NSString *strFirstPickerView = [yourarray objectAtIndex:row];
}
else if(pickerView == secondPickerView)
{
NSString *strSecondPickerView = [yourarray objectAtIndex:row];
}
else
{
NSString *strThirdPickerView = [yourarray objectAtIndex:row];
}
}
selectedRowInComponent:
- Returns the index of the selected row in a given component.
- (NSInteger)selectedRowInComponent:(NSInteger)component
Parameters
component
A zero-indexed number identifying a component of the picker view.
Return Value
A zero-indexed number identifying the selected row, or -1 if no row is selected.
For Help
And to make a difference between different pickerViews add tag to each of the pickers:
self.pikcerView.tag = 1;
Hope this helps.
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSString *str = [yourarray objectAtIndex:row];
}
now str have a selected value. so you can use it when done button press.
Following example for the UIPickerview controller
.m files here
#import "SingleComponentPickerViewController.h"
#implementation SingleComponentPickerViewController
#synthesize singlePicker;
#synthesize pickerData;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
-(IBAction)buttonPressed1
{
NSInteger row = [singlePicker selectedRowInComponent:0];
NSString *selected = [pickerData objectAtIndex:row];
NSString *title = [[NSString alloc] initWithFormat:
#"you selected %#!", selected];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message : #"Thank you for choosing."
delegate:nil
cancelButtonTitle :#"Welcome"
otherButtonTitles :nil];
[alert show];
[alert release];
[title release];
}
- (void)viewDidLoad {
NSArray *array = [[NSArray alloc] initWithObjects:#"iPhone",#"iPad",#"iPod",#"iMac",#"Mac",
#"iBook",#"Safari",nil];
self.pickerData = array;
[array release];
[super viewDidLoad];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
[singlePicker release];
[pickerData release];
[super dealloc];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
return [pickerData count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
return[pickerData objectAtIndex:row];
}
#end
and .h file here
#import <UIKit/UIKit.h>
#interface SingleComponentPickerViewController : UIViewController {
IBOutlet UIPickerView *singlePicker;
NSArray *pickerData;
}
#property(nonatomic , retain) UIPickerView *singlePicker;
#property(nonatomic , retain) NSArray *pickerData;
-(IBAction)buttonPressed1;
#end
happy coding
#samuel
have only been learning objective C for a couple of days now and currently baffled by tables/ UITableView etc.. I've created a table and it lists 6 names I've read up how to set the avatar (image to the left of the text) to 1 picture however I have been playing about trying to set each individual image using an array. Heres what I have, no issues just the simulator crashes constantly? Thanks in advance.
#import <UIKit/UIKit.h>
#interface SImple_TableViewController : UIViewController
<UITableViewDelegate, UITableViewDataSource>
{
NSArray * listData;
NSArray* listPics;
}
#property (nonatomic,retain) NSArray *listData;
#property (nonatomic,retain) NSArray* listPics;
#end
#import "SImple_TableViewController.h"
#implementation SImple_TableViewController
#synthesize listData, listPics;
- (void)dealloc
{
[listData release];
[listPics release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
NSArray *array = [[NSArray alloc] initWithObjects:#"Bradley", #"Glen",#"Alan",#"Sean",#"Mark",#"Luke",#"Jack", nil];
NSArray *pics = [[NSArray alloc] initWithObjects:#"ME.jpg",#"Sean.jpg",#"Glenn.jpg",#"Mark.jpg",#"Luke.jpg",#"Jack.jpg",#"Alan.jpg", nil];
self.listData = array;
// self.listPics = pics;
[array release];
[pics release];
[super viewDidLoad];
}
- (void)viewDidUnload
{
self.listData = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark -
#pragma mark Table View Data Source Methods
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.listData count];
return [self.listPics count];
}
-(UITableViewCell*)tableView:(UITableView*) tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{ static NSString *SimpleTableIdentifier = #"SImpleTableIdentifier";
UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier]autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
return cell;
UIImage*image = [UIImage imageNamed: listPics];
cell.imageView.image = [listPics objectAtIndex:row];
return cell;
}
#end
Here's an important thing to remember: no code that occurs after return will ever be reached. FOr example:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.listData count]; // METHOD RETURNS HERE
return [self.listPics count]; // THIS LINE IS NEVER REACHED
}
You can only return once.
This same error occurs in your method -(UITableViewCell*)tableView:(UITableView*) tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
Here's another problem that I see. This line:
NSArray *pics = [[NSArray alloc] initWithObjects:#"ME.jpg",#"Sean.jpg",#"Glenn.jpg",#"Mark.jpg",#"Luke.jpg",#"Jack.jpg",#"Alan.jpg", nil];
creates an array of NSString objects. If you wish to put UIImages into an array, then in place of #"ME.jpg", you should have [UIImage imageNamed:#"ME.jpg"]. Make sure to add the image, with the exact same name (case sensitive) to the project.
I've searched here, but didn't see nothing like this error.
I'm trying to populate my pickerview on ViewDidLoad, but when I run my program, the pickerView is just populated with "?" instead my strings... what's going on here? Thanks in advance.
- (void)viewDidLoad {
[super viewDidLoad];
activities = [[NSArray alloc] initWithObjects:#"sleeping", #"eating", #"working", #"thinking", #"crying", #"begging", #"leaving", #"shopping", #"hello worlding", nil];
feelings = [[NSArray alloc] initWithObjects:#"awsome",#"sad",#"happy",#"ambivalent",#"nauseous",#"psyched",#"confused",#"hopeful",#"anxious",nil];
}
You need to implement the UIPickerViewDelegate and UIPickerViewDataSource protocols:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == 0)
return [activities count];
else
return [feelings count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 0)
return [activities objectAtIndex:row];
else
return [feelings objectAtIndex:row];
}
Here I am adding a pickerview programaticly
- (void)viewDidLoad {
[super viewDidLoad];
CGRect pickerFrame = CGRectMake(0,280,321,200);
UIPickerView *myPickerView = [[UIPickerView alloc] init]; //Not sure if this is the proper allocation/initialization procedure
//Set up the picker view as you need it
//Set up the display frame
myPickerView.frame = pickerFrame; //I recommend using IB just to get the proper width/height dimensions
//Add the picker to the view
[self.view addSubview:myPickerView];
}
But now I need to actually have it display content and somehow find out when it changes and what value it has changed to. How do I do this?
in .h file place this code
#interface RootVC : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
assign the datasource and delegate to the picker
// this view controller is the data source and delegate
myPickerView.delegate = self;
myPickerView.dataSource = self;
use the following delegate and datasouce methods
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
return 200;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 50;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *returnStr = #"";
if (pickerView == myPickerView)
{
returnStr = [[levelPickerViewArray objectAtIndex:row] objectForKey:#"nodeContent"];
}
return returnStr;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (pickerView == myPickerView)
{
return [levelPickerViewArray count];
}
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
You need to create a class that implements the UIPickerViewDataSource protocol and assign an instance of it to myPickerView.dataSource.