Objective-C: Can't populate UITableView - tried everything - objective-c

I'm trying to populate a simple uitableview in XCode 4.1.
Here is MessagesInbox.h:
#import <UIKit/UIKit.h>
#interface MessagesInbox : UITableViewController <UITableViewDelegate,UITableViewDataSource>{
NSMutableArray *listData;
IBOutlet UITableView *table;
}
#property (nonatomic,retain) UITableView *table;
#property (nonatomic, retain) NSArray *listData;
#end
Here is MessagesInbox.m:
#import "MessagesInbox.h"
#implementation MessagesInbox
#synthesize listData,table;
- (void)loadView{
}
- (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];
self.listData = [[NSArray alloc] initWithObjects:#"Red", #"Green", #"Blue", #"Indigo", #"Violet", nil];
}
- (void)viewDidUnload
{
self.listData = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (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 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
cell.textLabel.text = [listData objectAtIndex:indexPath.row];
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
}
- (void)dealloc {
[listData release];
[super dealloc];
}
#end
No matter what I do, I keep getting a white screen...
I've connected the tableview's delegate and datasource to file's owner.
Any help greatly appreciated,
Edit-------------------
Here are my connections:

You're returning zero sections - you should have at least one.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

You're returning 0 as the number of sections! You should return at least 1 section if you want to display any table data.

You're overriding the -loadView method, which prevents your base class (UITableViewController) from loading the actual UITableView.
Remove that method and you'll see your table view.

I see two strange things here:
[1] sections returned are 0; you must define at least 1 section to work
[2] you're using another table on top of the "tableView" property defined by the UITableViewController class. You must check if this two tables are not conflicting in some way (sincerely I never tried to put another table over the "standard" table of the controller).
Just do this extra check other than the solution at [1].
This is a snapshot from Apple docs as reference:
If a nib file is specified via the initWithNibName:bundle: method (which is declared by the superclass UIViewController), UITableViewController loads the table view archived in the nib file. Otherwise, it creates an unconfigured UITableView object with the correct dimensions and autoresize mask. You can access this view through the tableView property.
If a nib file containing the table view is loaded, the data source and delegate become those objects defined in the nib file (if any). If no nib file is specified or if the nib file defines no data source or delegate, UITableViewController sets the data source and the delegate of the table view to self.
When the table view is about to appear the first time it’s loaded, the table-view controller reloads the table view’s data. It also clears its selection (with or without animation, depending on the request) every time the table view is displayed. The UITableViewController class implements this in the superclass method viewWillAppear:. You can disable this behavior by changing the value in the clearsSelectionOnViewWillAppear property.
When the table view has appeared, the controller flashes the table view’s scroll indicators. The UITableViewController class implements this in the superclass method viewDidAppear:.
It implements the superclass method setEditing:animated: so that if a user taps an Edit|Done button in the navigation bar, the controller toggles the edit mode of the table.
You create a custom subclass of UITableViewController for each table view that you want to manage. When you initialize the controller in initWithStyle:, you must specify the style of the table view (plain or grouped) that the controller is to manage. Because the initially created table view is without table dimensions (that is, number of sections and number of rows per section) or content, the table view’s data source and delegate—that is, the UITableViewController object itself—must provide the table dimensions, the cell content, and any desired configurations (as usual). You may override loadView or any other superclass method, but if you do be sure to invoke the superclass implementation of the method, usually as the first method call.

The other answers are correct.
You shouldn't override loadView w/o calling [super loadView].
You need to return > 0 for the number of sections in numberOfSectionsInTableView. You should need your iVar table. The tableView iVar is defined by UITableViewController.
You also have a memory leak where you alloc listData.
self.listData = [[NSArray alloc] initWithObjects:#"Red", #"Green", #"Blue", #"Indigo", #"Violet", nil];
The code above will retain the array twice. You should change to one of:
self.listData = [NSArray arrayWithObjects:#"Red", #"Green", #"Blue", #"Indigo", #"Violet", nil];
or
listData = [[NSArray alloc] initWithObjects:#"Red", #"Green", #"Blue", #"Indigo", #"Violet", nil];

Related

Push to viewcontroller and parse data

I have a simple UITableView which is suppose to push to a new Viewcontroller. When the Cell is pressed it should push to the new viewcontroller and send what number of row has been pushed. Is there a way to obtain this?
This is what i have at the moment, which dosent do anything.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
YoutubeViewController *youtubeViewController = [[YoutubeViewController alloc] initWithNibName:#"YoutubeViewController" bundle:nil];
[self.navigationController pushViewController:youtubeViewController animated:YES];
}
Do like this
This method is the delegate that will be called when a the user selects a cell in tableview
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//Write code here to push your viewcontroller
YourViewController *controller=[[YourViewController alloc]initWithNibName:#"YourViewController" bundle:[NSBundle mainBundle]];
controller.selectedRowValue = indexPath.row;
[self presentViewController:controller animated:YES completion:NULL];
}
In YourViewController.h
#interface YourViewController : UIViewController
#property int selectedRowValue;
#end
In YourViewController.m
#implementation YourViewController
#synthesize selectedRowValue;
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"Selected Row %d",selectedRowValue);
}
Check protocol of UITableViewController delegate https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/Reference/Reference.html.
Method You are looking for
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
yes do one thing
create property of integer type in your YoutubeViewController
like this
#property int selectedRowValue;
after that syntsize it in YoutubeViewController.m class
// YoutubeViewController.m
#implementation YoutubeViewController
#synthesize selectedRowValue
now you come class where ur table is and make some changes to it as shown bellow
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
YoutubeViewController *youtubeViewController = [[YoutubeViewController alloc] initWithNibName:#"YoutubeViewController" bundle:nil];
youtubeViewController.selectedRowValue=indexPath.row;
[self.navigationController pushViewController:youtubeViewController animated:YES];
}
now every thing done to check index value selected in youtube class NSLogout the value
//YoutubeViewController.m
- (void)viewDidLoad
{
// nslogout the value to check
NSLog(#"yours data selected from table%d",selectedRowValue);
}
now every thing done .
enjoy coding
Even you dont want this you can go by creating Constructor or by storing the value to NSUserDefaults

Master Detail Storyboard: Table View not showing cells (Objective-C)

I'm having troubles with the master viewController not showing any cells. This is the situation:
The app uses storyboard.
When the app launches, it goes to the navigationController
A button is pressed and connected to the table ViewController and it's set to "push" to it.
I've added the object and made a cell/detailView or whatever.
For some reason, the cell won't show up!!!
Here's the files:
MasterViewController.h:
#import <UIKit/UIKit.h>
#import "CraftingDetail.h"
#import "Crafting.h"
#class CraftingList;
#interface CraftingMaster : UITableViewController
#property (strong, nonatomic) CraftingDetail *detailViewController;
#property (strong, nonatomic) CraftingList *CL;
#end
MasterViewController.m:
#import "CraftingMaster.h"
#import "CraftingList.h"
#interface CraftingMaster ()
#end
#implementation CraftingMaster
#synthesize detailViewController = _detailViewController;
#synthesize CL;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
self.CL = [[CraftingList alloc] init];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return self.CL.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return self.CL.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
cell.textLabel.text = [self.CL craftingAtIndex:indexPath.row].Title;
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
#end
DetailViewController.h:
#import <UIKit/UIKit.h>
#interface CraftingDetail : UIViewController
#property (strong, nonatomic) IBOutlet UIImageView *Image;
#property (strong, nonatomic) IBOutlet UITextView *Description;
#end
This is an old question, but having an unexpectedly empty table view is a common issue when starting to develop with table views so hopefully this answer will be of use to someone.
Here are some things to check when your table view is devoid of cells and you expected otherwise:
Are your data source object(s) (self.CL in this case) valid? (ie. Are they != nil and point to the correct object?)
Does numberOfSectionsInTableView: return an integer value greater than zero?
Does tableView:numberOfRowsInSection: return an integer value greater than zero?
Here are a couple of problems in MasterViewController.m above that need attention:
InitWithStyle: will not be executed when the view controller is instantiated in a storyboard. Instead, initWithCoder: should be used. I suspect this was the source of JomanJi's pain as this resulted in self.CL not being instantiated. (As an aside, the data source object/property: CL should be instantiated by assigning the value to the _CL ivar directly, not to the property. See "Initializing a property, dot notation" to learn why).
Due to returning (what is likely) the same value for numberOfSectionsInTableView: as tableView:numberOfRowsInSection: (ie. "return self.CL.count;"), the table view will display the same number of sections as there are cells in each section with each sections' cells containing the same data as the other sections. I doubt this effect was what the developer intended. (This is of course unless the count accessor method in CraftingList does something really strange).
Without seeing the code for CraftingList it is impossible to determine exactly what the problem is. However, given the age of the question, I suspect JomanJi has since figured it out on his/her own.

Add UITableView to an existing project: crash when adding array to populate rows

I'm adding to a project a TableView populated with countries to choose from. Added the New File (UITableView subclass for iPad+XIB), wrote the trigger IBAction code (editing a textfield if default country is not right), made some connections and the empty table view appears. I have read several tutorials and i cannot make out the problem: when the array with words loads in - (void)viewDidLoad, the app crashes with the following warning:
2012-05-04 12:34:36.740 pruebaF1[4017:f803] * Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-1914.84/UITableView.m:6061
2012-05-04 12:34:36.741 pruebaF1[4017:f803] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'...
CountryVieWController connections:
File Owner's connections
Outlets
dataSource -> File's Owner
delegate ->File's Owner
Referencing Outlets
view -> File's Owner
Code:
// CountryTableVieWController.h
#import <UIKit/UIKit.h>
#interface CountryTableVieWController :
UITableViewController<UITableViewDelegate,UITableViewDataSource>
{
NSMutableArray *countriesArray;
NSArray *countryArray;
}
#end
// CountryTableVieWController.m
#import "CountryTableVieWController.h"
#import "pruebaF1SecondViewController.h"
#interface CountryTableVieWController ()
#end
#implementation CountryTableVieWController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
countriesArray = [[NSMutableArray alloc] initWithObjects:#"Austria", #"Italy", #"France",nil];
}
Thanks in advance.
You need to implement delegate methods for the UITableView.
Have a look at this: http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/CreateConfigureTableView/CreateConfigureTableView.html#//apple_ref/doc/uid/TP40007451-CH6-SW10
I find the easiest way to think of it is that your UITableView is asking your code what should go in the cells. You use these methods to configure your table view and the UITableViewCells within it.
You will need something like this:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
[countriesArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}
NSString *country = [countriesArray objectAtIndex:indexPath.row];
cell.textLabel.text = country;
return cell;
}

trying to fix this error: Could not find mapped image UIPickerViewFrameLeft-162-Popover.png

I'm trying to create programmatically a uiPickerView and add it to a view without using interface builder. Don't get me wrong, I like IB but the reason why I want to do it this way is because i'm trying to build an object that I can quickly plug in to produce popup menus using UIPopoverViewController and a variety of different subViews (such as uiPickerView) as the menu in the popup. I've already made this work by building the menu in IB and initializing the popup with its ViewController so I know how this works for the most part.
I've entered the relevant code below and this is the two errors I get when I run it:
- "Could not find mapped image UIPickerViewFrameRight-162-Popover.png"
- "Could not find mapped image UIPickerViewFrameLeft-162-Popover.png"
I don't know what these images are but I'm assuming they are the png's of the picker view.
menu = [[UIPickerView alloc]initWithFrame:CGRectMake(0,100,162,162)];
menu.delegate = self;
menu.dataSource = self;
[menu reloadAllComponents];
[menu selectRow:0 inComponent:0 animated:YES];
//Add the picker to the view
[customViewController.view addSubview:menu];
popView = [[UIPopoverController alloc] initWithContentViewController:customViewController] ;
[popView setDelegate:self];
CGRect pos = [rootView frame];
[popView presentPopoverFromRect:CGRectMake(pos.origin.x,pos.origin.y,0,pos.size.height)
inView:displayView permittedArrowDirections:arrowDir animated:YES];
Now this Code will crash the program unless you remove the line where I try to add the picker to the view, at which point I just get the blank popover. So I know that it is the picker that is causing this issue however I don't know how to fix it. I've been searching all day but every tutorial online on uipickers all include using IB. My guess is that this is a really stupid error like missing an import or something but if anyone can tell me what I'm doing wrong it would be greatly appreciated.
Also note that I followed the tutorials on how to set up the dataSource and delegate methods for the UIPickerView and I'm pretty sure they are fine however if you want to verify here you are: Thanks again.
#import "PopUpMenuViewController.h"
#implementation PopUpMenuViewController
#synthesize menuType;
#synthesize data;
#synthesize popView;
#synthesize menu;
#synthesize customViewController;
#pragma mark -
#pragma mark UIPOPOVERCONTROLLER DELEGATE METHODS
#pragma mark -
- (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController{
//Delegate this too the User of this class
return TRUE;
}
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController{
//Delegate this too the User of this class
}
#pragma mark -
#pragma mark CUSTOM POPOVERVIEWCONTROLLER METHODS
#pragma mark -
-(void) initWithMenuType:(int)type{
menuType = type;
}
-(id) initWithMenuType:(int)type andData:(NSMutableArray *)dataSet fromViewItem:(id)sender
withMainView:(UIView *)mView{
[super init];
menuType = type;
data = dataSet;
rootView = sender;
displayView = mView;
arrowDir = UIPopoverArrowDirectionUp;
customViewController = [[UIViewController alloc] initWithNibName:#"PopUpMenu" bundle:nil];
return self;
}
-(void) setPopUpArrowDirection:(UIPopoverArrowDirection) arrow{
arrowDir = arrow;
}
-(void) showPopUp{
//UIPicker Menu
if (menuType==1) {
//UIPicker Setup
menu = [[UIPickerView alloc]initWithFrame:CGRectMake(0,100,162,162)];
menu.delegate = self;
menu.dataSource = self;
[menu reloadAllComponents];
[menu selectRow:0 inComponent:0 animated:YES];
//Add the picker to the view
[customViewController.view addSubview:menu];
popView = [[UIPopoverController alloc] initWithContentViewController:customViewController] ;
[popView setDelegate:self];
CGRect pos = [rootView frame];
[popView presentPopoverFromRect:CGRectMake(pos.origin.x,pos.origin.y,0,pos.size.height)
inView:displayView permittedArrowDirections:arrowDir animated:YES];
//[popView setPopoverContentSize:CGSizeMake(menu.frame.size.width+5,menu.frame.size.height+5)];
}
}
#pragma mark -
#pragma mark VIEW CONTROLLER DELEGATE METHODS
#pragma mark -
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}*/
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}
- (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.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
[data release];
[popView release];
[menu release];
[rootView release];
[displayView release];
[customViewController release];
}
#pragma mark -
#pragma mark UIPICKERVIEW DELEGATE & DATASOURCE METHODS
#pragma mark -
#pragma mark -
#pragma mark UIPickerViewDataSource Methods
- (NSInteger) pickerView: (UIPickerView *) pickerView numberOfRowsInComponent: (NSInteger) component {
return [data count];
}
- (NSInteger) numberOfComponentsInPickerView: (UIPickerView *) pickerView {
return 1;
}
#pragma mark -
#pragma mark UIPickerViewDelegate Methods
// Row height in pixels
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
return 40.0f;
}
// Column width in pixels
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
return 90.0f;
}
- (NSString *) pickerView: (UIPickerView *) pickerView titleForRow: (NSInteger) row
forComponent: (NSInteger) component {
return [data objectAtIndex:row];
}
- (void) pickerView: (UIPickerView *) pickerView
didSelectRow: (NSInteger) row inComponent: (NSInteger) component {
}
If anyone else encounters this particular warning in the console "Could not find mapped image UIPickerViewFrameRight-162-Popover.png", I think I've figured out why it appears.
The HIG indicates loosely that UIPickerView should only be added to a Popover on the iPad.
"On iPad, present a date and time picker only within a popover."
http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/mobilehig/UIElementGuidelines/UIElementGuidelines.html
In my experiments, the UIPickerView must be the direct and only view of the current UIViewController in a popover. If the UIPickerView appears elsewhere in a view hierarchy, the warning appears and the UIPickerView will look bad (like it's missing the left and right portions of the view).
In the code above, you can see that the UIPickerView was added as a subview of the rootView in the customController:
[customViewController.view addSubview:menu];
This probably would have worked if the UIPickerView was the root view in the customController.
You can programmatically make the UIPickerView the root view of the controller by overriding the loadView method of the controller and assigning the UIPickerView directly to the root view of the controller:
- (void)loadView {
CGRect frame = CGRectMake(0,0, 300, 300);
UIPickerView *v = [[[UIPickerView alloc] initWithFrame:frame] autorelease];
v.delegate = self; // assuming controller adopts UIPickerViewDelegate
v.dataSource = self; // assuming controller adopts UIPickerViewDataSource
self.view = v;
}
Hope this helps someone.
Alright well I'm not quite sure what happened but I deleted the project and re-wrote this code and voila... No more issues.
I just recently ran into the same issue with my app on the iPad and iPhone. It actually was a simple but stupid fix which involved making the height on the iPad to 180 and 162 on the iPhone. God must love apple s/w but I don't.

NSTableView binding problem

I have only just started with XCode (v3.2.2) and Interface Builder and have run into a problem.
Here is what I have done:
I have made a class to be the datasource of a NSTableView:
#interface TimeObjectsDS : NSControl {
IBOutlet NSTableView * idTableView;
NSMutableArray * timeObjects;
}
#property (assign) NSMutableArray * timeObjects;
#property (assign) NSTableView * idTableView;
- (id) init;
- (void) dealloc;
- (void) addTimeObject: (TimeObj *)timeObject;
// NSTableViewDataSource Protocol functions
- (int)numberOfRowsInTableView:(NSTableView *)tableView;
- (id)tableView:(NSTableView *)tableView
objectValueForTableColumn:(NSTableColumn *)tableColumn row:
(int)row;
#implementation TimeObjectsDS
#synthesize timeObjects;
#synthesize idTableView;
-(id) init {
if (self = [super init]) {
self.timeObjects = [[NSMutableArray alloc] init];
TimeObj *timeObject = [[TimeObj alloc] init];
[timeObject setProjectId:11];
[timeObject setDescription:#"Heja"];
[timeObject setRegDate:#"20100331"];
[timeObject setTimeSum:20.0];
[timeObjects addObject:timeObject];
[timeObject release];
[idTableView reloadData];
}
return self;
}
- (void) dealloc {
[idTableView release];
[timeObjects release];
[super dealloc];
}
// Functions
- (void) addTimeObject: (TimeObj *)timeObject {
[self.timeObjects addObject:timeObject];
[idTableView reloadData];
}
// NSTableViewDataSource Protocol functions
- (int) numberOfRowsInTableView:(NSTableView *)tableView {
return [self.timeObjects count];
}
- (id) tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row {
return [[timeObjects objectAtIndex:row] description];
}
#end
I have then bound my NSTableView in the View to this datasource like so:
alt text http://www.og-entertainment.com/tmp/ib_datasource_bindings_big.png
I have also bound the View NSTableView to the Controller idTableView variable in Interface Builder seen above
In the init function I add a element to the mutable array. This is displayed correctly in the NSTableView when I run the application. However when I add another element to the array (of same type as in init) and try to call [idTableView reloadData] on the View nothing happens.
In fact the Controller idTableView is null. When printing the variable with NSLog(#"idTableView: %#", idTableView) I get "idTableView: (null)"
Im runing out of ideas how to fix this. Any ideas to what I could do to fix the binding?
If your tableview outlet in your controller is null, then you haven't connected it in Interface Builder. Your screenshot above shows a connection to TimeObjectsDS, but that doesn't mean a lot - is that the instance that you are calling reloadData from? It is possible that you have more than one instance of this class, for example.
That's just one possibility. Without more code, it's not feasible to list many more.
Incidentally, in MVC it's considered a bad thing to connect a model object directly to a view. You may just be using the terminology incorrectly.