UITableView is not Getting displayed in iPhone - iphone

I am using Xcode 3.2.3.
Now i am doing simple UITable view.For that i referred this link.
I have done all the steps mentioned there.
But when my application starts it shows only white screen. No any tableview.
please provide me solution.
Thanks in advance.
MyCode is
AppDelegate.h
#import <UIKit/UIKit.h>
#class TableBasicsViewController;
#interface TableBasicsAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
TableBasicsViewController *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet TableBasicsViewController *viewController;
#end
AppDelegate.m
#import "AppDelegate.h"
#import "TableBasicsViewController.h"
#implementation TableBasicsAppDelegate
#synthesize window;
#synthesize viewController;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
}
- (void)applicationWillTerminate:(UIApplication *)application {
}
#pragma mark -
#pragma mark Memory management
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
#end
TableBasicViewController.h
#import <UIKit/UIKit.h>
#interface TableBasicsViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
}
#property (nonatomic, retain) NSArray *games;
#end
TableBasicViewController.m
#import "TableBasicsViewController.h"
#implementation TableBasicsViewController
#synthesize games;
/*
// 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];
self.games = [NSArray arrayWithObjects: #"Super Mario Bros.",
#"The Legend of Zelda", #"Blades of Steel",
#"Teenage Mutant Ninja Turtles", #"Excitebike",
#"Dr. Mario", #"Duck Hunt", #"Tetris", #"Ice Climber",
#"River City Ransom", #"Ninja Gaiden", #"Super Mario Bros. 3",
#"Mega Man 2", #"Kid Icarus", #"Metroid", #"Metal Gear",
#"Super Mario Bros. 2", #"Zelda II: The Adventure of Link",
nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section {
return [self.games count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Default";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
cell.textLabel.text = [self.games objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Selected: '%#'", [self.games objectAtIndex:indexPath.row]);
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (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 {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
self.games = nil;
[super dealloc];
}
#end

Please check this
TableBasicViewController.h
#interface TableBasicsViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
IBOutlet UITableView *tableView;
}
#property (nonatomic, retain) NSArray *games;
#end
and link this in your xib too

Write This Code in Your AppDelegate.m File
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
viewController = [[TableBasicsViewController alloc]initWithNibName:#"TableBasicsViewController"];
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}

Related

Setting a NSString to the sending end of a segue

I made a simple project to explain my problem. Basically what I did was make a project with the templet of a Master Detail Application. Then in the
DetailViewController.h
nothing
DetailViewController.m
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"showDetail"])
{
[segue.destinationViewController setCellName2:#"New String"];
}
}
MasterViewController.h
#property(nonatomic,strong) NSString *cellName2;
MasterViewController.m
#synthesize cellName2;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(#"%#", cellName2);
}
My problem is in the detailViewController where I set the cellName2, I cant set it because the detailViewController is the receivingViewController of the segue. Is their a method to set the sendingViewController end of a segue?
Edit
After Firo's answer my code looks like this now
MasterViewContoller.h
#import <UIKit/UIKit.h>
#import "DetailViewController.h"
#interface MasterViewController : UITableViewController <DetailViewDelegate>
#property(nonatomic,strong) NSString *cellName2;
#end
.m
#import "MasterViewController.h"
#import "DetailViewController.h"
#interface MasterViewController () {
NSMutableArray *_objects;
}
#end
#implementation MasterViewController
#synthesize cellName2;
- (void)awakeFromNib
{
[super awakeFromNib];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
self.cellName2 = cellName;
NSLog(#"%#", self.cellName2);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)insertNewObject:(id)sender
{
if (!_objects) {
_objects = [[NSMutableArray alloc] init];
}
[_objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSDate *object = _objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_objects removeObjectAtIndex:indexPath.row];
[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;
}
*/
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:#"showDetail"]) {
/* assigning self as delegate, telling the detail view that I implement
* setCellName2:, so it (the detailVC) can call it whenever it wants to.
*/
[segue.destinationViewController setDelegate:self];
}
}
// my implementation of the DetailViewDelegate protocol that I abide to
/* note: #pragma mark is not required, just for comment, documentation
* and find-ability purposes
*/
//#pragma mark - DetailViewDelegate
// note: this is just a property setter so this is not actually needed
//- (void)setCellName2:(NSString *)cellName {
// self.cellName2 = cellName;
// NSLog(#"%#", self.cellName2);
//}
#end
DetailViewContoller.h
#import <UIKit/UIKit.h>
/* defining a protocol, whoever is a DetailViewDelegate must implement my
* defined methods
*/
#protocol DetailViewDelegate <NSObject>
- (void)setCellName2:(NSString *)cellName;
#end
#interface DetailViewController : UIViewController
/* storing a delegate property. Whoever sets themselves to my delegate
* must implement my DetailViewDelegate's methods (setCellName2: in this case)
*/
#property (weak, nonatomic) id<DetailViewDelegate> delegate;
#property (strong, nonatomic) id detailItem;
#property (weak, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
#end
.m
#import "DetailViewController.h"
#interface DetailViewController ()
- (void)configureView;
#end
#implementation DetailViewController
#pragma mark - Managing the detail item
- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
}
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
self.detailDescriptionLabel.text = [self.detailItem description];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//[self.delegate setCellName2:#""];
[self configureView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"showDetaila"])
{
[segue.destinationViewController setCellName2:#"New String"];
}
}
#end
The problem now is, i am getting the error "Use of undeclared identifier 'cellName'; did you mean 'cellName2'?"
You are looking for delegates and protocols. Since you are not segueing from your detailVC to your masterVC you cannot put this setter in prepareForSegue. You need to store a reference to the master and have callbacks to it. Here is how you would do it with your basic example:
DetailViewController.h
/* defining a protocol, whoever is a DetailViewDelegate must implement my
* defined methods
*/
#protocol DetailViewDelegate <NSObject>
- (void)setCellName2:(NSString *)cellName;
#end
#interface DetailViewController : UIViewController
/* storing a delegate property. Whoever sets themselves to my delegate
* must implement my DetailViewDelegate's methods (setCellName2: in this case)
*/
#property (weak, nonatomic) id<DetailViewDelegate> delegate;
#end
DetailViewController.m
#implementation DetailViewController
// some action or method
- (IBAction)buttonPress:(id)sender {
// look below at Master's prepareForSegue
/* calling the method that my delegate implements, my delegate can be any
* object that implements my protocol (DetailViewDelegate)
*/
[self.delegate setCellName2:#""];
}
#end
MasterViewController.h
#import "DetailViewController.h"
// saying I implement the DetailViewDelegate protocol (and all necessary methods)
#interface MastViewController : UITableViewController <DetailViewDelegate>
#end
MasterViewController.m
#interface MasterViewController ()
#property(strong, nonatomic) NSString *cellName2;
#end
#implementation MasterViewController
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:#"showDetail"]) {
/* assigning self as delegate, telling the detail view that I implement
* setCellName2:, so it (the detailVC) can call it whenever it wants to.
*/
[segue.destinationViewController setDelegate:self];
}
}
// my implementation of the DetailViewDelegate protocol that I abide to
/* note: #pragma mark is not required, just for comment, documentation
* and find-ability purposes
*/
#pragma mark - DetailViewDelegate
// note: this is just a property setter so this is not actually needed
- (void)setCellName2:(NSString *)cellName {
_cellName = cellName
NSLog("%#", self.cellName);
}
#end
I would give you some more information about delegates and protocols but it is an extremely common pattern when dealing with iOS development. You also should become extremely familiar with delegates and protocols, you will find it useful in many situations and it will help you better understand iOS development and make you a more competent programmer. If something does not work (or make sense) let me know. I just typed this up in SO so there could be some mild mistakes.
Edit
Note: If this becomes to far from the original question you may need to just create a new one.
Your main issue here is that you have self.cellName2 = cellName; in your MasterViewController. According to your original post you want the DetailVC to set this, right? So it will need to go into DetailViewController's viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
[self.delegate setCellName2:#"My custom text!"];
}
Then remove:
self.cellName2 = cellName;
NSLog(#"%#", self.cellName2);
From your MasterViewController's viewDidLoad. Your error is with the first line there (I am assuming). What is cellName? It is not a string and you have not defined it as a variable or property, hence the error.
Are you sure that the code is in the right class? And that you have the segues hooked up correctly in the storyboard?
In your DetailViewController code, you indicate that the segue identifier is "showDetail". It would seem to me that you ought to be handling that in your MasterViewController, as the pattern is typically that MasterViewController is responsible for seguing to the DetailViewController for the purpose of showing details.
Basically, if the destinationViewController property of the segue is NOT an instance of MasterViewController, then either the segue is not correctly configured or your code is in the wrong place.
If I'm mistaken please post more code.

Master Detail Application - Static Table in Detail View

I'm trying to code an app in Xcode 4, with storyboarding. It's a master detail application, and it all worked fine, with the table and the detail view. But in my detail view, I would like to have a static table to display the data. In a grouped table style way, with the "key" on the left and "value" on the right, if that's a way to put it... So, it's all working fine until I put a table into my UIView. Apparently you have to put it in a UITableView for it to work, so I deleted the UIView that Xcode made for me and put in a UITableView in its place. I set it up EXACTLY the same (I think) with the identifier, title etc... and then connect the table cells up with outlets and what not. But now when I enter the view, I just get an empty table (well, not empty, just all the rows say "Detail" in rather than the actual data I want). I don't see why! D: I even changed DetailViewController.h to say "UITableViewController" as well! No avail... :( Could someone please enlighten me as to what I'm doing wrong! I bet it's really simple... :L Here's my code
MasterViewController.h
#import <UIKit/UIKit.h>
#class DetailViewController;
#interface MasterViewController : UITableViewController
#property (strong, nonatomic) DetailViewController *detailViewController;
#property (strong) NSMutableArray *verbs;
#end
MasterViewController.m
#import "MasterViewController.h"
#import "DetailViewController.h"
#import "VerbData.h"
#interface MasterViewController () {
NSMutableArray *_objects;
}
#end
#implementation MasterViewController
#synthesize verbs = _verbs;
- (void)awakeFromNib
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
self.clearsSelectionOnViewWillAppear = NO;
self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
}
[super awakeFromNib];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.detailViewController = (DetailViewController *) [[self.splitViewController.viewControllers lastObject] topViewController];
self.title = #"Verbs";
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)insertNewObject:(id)sender
{
if (!_objects) {
_objects = [[NSMutableArray alloc] init];
}
[_objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _verbs.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"VerbCell"];
VerbData *verb = [self.verbs objectAtIndex:indexPath.row];
cell.textLabel.text = verb.infinitive;
cell.detailTextLabel.text = verb.english;
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_objects removeObjectAtIndex:indexPath.row];
[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;
}
*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
VerbData *object = [self.verbs objectAtIndex:indexPath.row];
self.detailViewController.detailItem = object;
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
VerbData *object = [self.verbs objectAtIndex:indexPath.row];
[[segue destinationViewController] setDetailItem:object];
}
}
#end
DetailViewController.h
#import <UIKit/UIKit.h>
#import "VerbData.h"
#interface DetailViewController : UITableViewController <UISplitViewControllerDelegate>
#property (strong, nonatomic) VerbData *detailItem;
#property (weak, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
#property (weak, nonatomic) IBOutlet UILabel *jeOutlet;
#property (weak, nonatomic) IBOutlet UILabel *tuOutlet;
#property (weak, nonatomic) IBOutlet UILabel *ilOutlet;
#property (weak, nonatomic) IBOutlet UILabel *nousOutlet;
#property (weak, nonatomic) IBOutlet UILabel *vousOutlet;
#property (weak, nonatomic) IBOutlet UILabel *ilsOutlet;
#end
DetailViewController.m
#import "DetailViewController.h"
#interface DetailViewController ()
#property (strong, nonatomic) UIPopoverController *masterPopoverController;
- (void)configureView;
#end
#implementation DetailViewController
#pragma mark - Managing the detail item
#synthesize detailItem = _detailItem;
#synthesize jeOutlet = _jeOutlet;
#synthesize tuOutlet = _tuOutlet;
#synthesize ilOutlet = _ilOutlet;
#synthesize nousOutlet = _nousOutlet;
#synthesize vousOutlet = _vousOutlet;
#synthesize ilsOutlet = _ilsOutlet;
- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
if (self.masterPopoverController != nil) {
[self.masterPopoverController dismissPopoverAnimated:YES];
}
}
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
self.detailDescriptionLabel.text = self.detailItem.english;
self.jeOutlet.text = self.detailItem.je;
self.tuOutlet.text = self.detailItem.tu;
self.ilOutlet.text = self.detailItem.il;
self.nousOutlet.text = self.detailItem.nous;
self.vousOutlet.text = self.detailItem.vous;
self.ilsOutlet.text = self.detailItem.ils;
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title = self.detailItem.infinitive;
[self configureView];
}
- (void)viewDidUnload
{
[self setJeOutlet:nil];
[self setTuOutlet:nil];
[self setIlOutlet:nil];
[self setNousOutlet:nil];
[self setVousOutlet:nil];
[self setIlsOutlet:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
#pragma mark - Split view
- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
{
barButtonItem.title = NSLocalizedString(#"Master", #"Master");
[self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
self.masterPopoverController = popoverController;
}
- (void)splitViewController:(UISplitViewController *)splitController willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
// Called when the view is shown again in the split view, invalidating the button and popover controller.
[self.navigationItem setLeftBarButtonItem:nil animated:YES];
self.masterPopoverController = nil;
}
#end
First thing I will try to debug is -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method. Is it return values? Try to put NSLog in this method and check it.
Or maybe this will help you:
Note: If you want to change the background color of a cell (by setting the background color of a cell via the backgroundColor property declared by UIView) you must do it in the tableView:willDisplayCell:forRowAtIndexPath: method of the delegate and not in tableView:cellForRowAtIndexPath: of the data source. Changes to the background colors of cells in a group-style table view has an effect in iOS 3.0 that is different than previous versions of the operating system. It now affects the area inside the rounded rectangle instead of the area outside of it.
The code must handle the case where dequeueReusableCellWithIdentifier answers nil...
static NSString *CellIdentifier = #"VerbCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

Why is my tableView not displaying objects?

First of I would like to start with I am 100% new to iPhone development. I believe this is a quite simple question for someone experienced. Anyhow, what I am trying to do is:
The user should via the SearchUI be able to search for objects from my database. If the object exist, display it in the tableView where the search-objects will be displayed. I manage to get the objects from the database but not instance them into the tableview and display them.
Honestly I don't know what I am doing wrong. All help will be really appreciated and also some explaining if possible. Under method - (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects - I try to move the objects into the tableView without any success. You find the method in FirstViewController.m at the end of pragma mark TableView Data Scource methods. Here is my code:
FirstViewController.h class
#import <UIKit/UIKit.h>
#import <RestKit/RestKit.h>
#interface FirstViewController : UIViewController <UITableViewDataSource, RKObjectLoaderDelegate>
{
UISearchDisplayController *searchDisplayController;
UISearchDisplayController *searchBar;
UITableView *table;
NSArray *allItems;
NSArray *searchResults;
}
#property (nonatomic, retain) IBOutlet UISearchDisplayController *searchDisplayController;
#property (nonatomic, retain) IBOutlet UISearchDisplayController *searchBar;
#property (nonatomic, retain) IBOutlet UITableView *table;
#property (nonatomic, copy) NSArray *allItems;
#property (nonatomic, copy) NSArray *searchResults;
#end
FirstViewController.m class
#import "FirstViewController.h"
#import "Task.h"
interface FirstViewController ()
end
implementation FirstViewController
#synthesize searchDisplayController;
#synthesize searchBar;
#synthesize allItems;
#synthesize searchResults;
#synthesize table;
#pragma mark - View lifecycle
- (void)viewDidLoad
{
//self.listContent = [[NSArray alloc] initWithObjects:#"John", #"Paul", nil];
//self.filteredListContent = [NSMutableArray arrayWithCapacity:10];
[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
else
{
return YES;
}
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
return NO;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
return NO;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)pSearchBar
{
[[RKObjectManager sharedManager].mappingProvider setMapping:[Task getMapping] forKeyPath:#"tasks"];
NSString *path = [NSString stringWithFormat:#"%#/%#/%#", #"/book/1/tasks/", pSearchBar.text, #".json"];
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:path delegate:self];
NSLog(#"Search: %#", pSearchBar.text);
}
#pragma mark - TableView Data Scource methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.searchResults count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"MyCell"];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MyCell"];
}
cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];
return cell;
}
- (void) deselect {
//[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}
// Respond to user selection tap by coloring the navigation bar
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath
{
}
- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects
{
self.searchResults = objects;
[self.table reloadData];
for(Task *task in objects)
{
if ([task isKindOfClass:[Task class]])
{
NSLog(#"Loaded Book ID: %# ; Name: %# ; Book: %#", task.id, task.name, task.book.name);
}
}
}
- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error
{
NSLog(#"Encountered an error: %#", error);
}
#end
Step1. Since your TableView is an IBOutlet, check you tableView datasource and delegate mappings in the .xib file for this view controller. My doubt is that the hooking is not correct.
Step2. If the hook ups in .xib file are correct, then you should do
- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects
{
self.searchResults = objects;
NSLog(#"%#", searchResults)
[self.table reloadData];
....
}
and check if the NSLog is logging the searchResults array. If that's empty for some reason, your numberOfRowsInSection delegate method will return 0 and hence your tableView will be empty.
Hope this helps.
in the first line
#interface FirstViewController : UIViewController <UITableViewDataSource, RKObjectLoaderDelegate>
{
UISearchDisplayController *searchDisplayController;
UISearchDisplayController *searchBar;
UITableView *table;
NSArray *allItems;
NSArray *searchResults;
}
replace this line with below code
#interface FirstViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, RKObjectLoaderDelegate>
{
UISearchDisplayController *searchDisplayController;
UISearchDisplayController *searchBar;
IBOutlet UITableView *table;
NSArray *allItems;
NSArray *searchResults;
}
and connect the tableview delegate and tableview datasource in interface builder.
Hope it helps.

Passing object using story board iOS5

I am trying to pass an object to a static grouped table view that I have created in story board.
Here is the code that I am using in my first view to push the second view:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Row Selected");
CustomerDetailTableViewController *detailView = [[self storyboard] instantiateViewControllerWithIdentifier:#"DetailsView"];
detailView.customer = [self.fetchedResultsController objectAtIndexPath:[self.tableView indexPathForSelectedRow]];
NSLog(#"%#",detailView.customer.firstName);
[self.navigationController pushViewController:detailView animated:YES];
}
The NSlog for the firstName is correct but when the detail view is pushed the cells in the detailView are null. I'm probably just missing something dumb but a fresh set of eyes would be much appreciated.
Here is the code for the detailView controller:
CustomerDetailTableViewController.h
#class Customer;
#import <UIKit/UIKit.h>
#interface CustomerDetailTableViewController : UITableViewController{
Customer *customer;
UILabel *fullName;
UILabel *address;
UILabel *homePhone;
UILabel *cellPhone;
UILabel *email;
}
#property (nonatomic, strong) IBOutlet UILabel *fullName;
#property (nonatomic, strong) IBOutlet UILabel *address;
#property (nonatomic, strong) IBOutlet UILabel *homePhone;
#property (nonatomic, strong) IBOutlet UILabel *cellPhone;
#property (nonatomic, strong) IBOutlet UILabel *email;
#property (nonatomic, strong) Customer *customer;
#end
CustomerDetailTableViewController.m
#import "CustomerDetailTableViewController.h"
#import "Customer.h"
#implementation CustomerDetailTableViewController
#synthesize fullName, address, homePhone, cellPhone, email, customer;
- (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
{
fullName = [NSString stringWithFormat:#"%# %#", customer.firstName, customer.lastName];
address = [NSString stringWithFormat: #"%#/n%#, %# %#", customer.address, customer.city, customer.state, customer.zipCode];
[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;
}
- (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);
}
#end
So, you have created the two views (the first TableView and the CustomerDetailTableViewController) with storyboards?
In that case, you have to click on the connection line between the two views into the storyboard and set the "Identifier" field, into the section "Storyboard Segue", to something like "setCustomer". Here's a screenshot:
Here a little screenshot:
After that, you can comment the method tableView:didSelectRowAtIndexPath: on the first TableView, and replace with this method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"setCustomer"]) {
CustomerDetailTableViewController *customerDetailVC = (CustomerDetailTableViewController *)[segue destinationViewController];
customerDetailVC.customer = [self.fetchedResultsController objectAtIndexPath:[self.tableView indexPathForSelectedRow]];
}
}
Remember to include
#include "CustomerDetailTableViewController.h"
at the top of implementation file.
I hope this help!

Blank screen on device; simulator working fine

I am writing an iOS app with a navigation controller. When I open it up on the Simulator, it runs fine. When I run it on the device, a blank screen is displayed below the status bar. Plus I can't figure out where my RootViewController is made to be the default view (which I suspect is the root of my problem).
#class RootViewController;
#interface MyAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
RootViewController *viewController;
UINavigationController *navigationController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#property (nonatomic, retain) IBOutlet RootViewController *viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Set the navigation controller as the window's root view controller and display.
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
// ...
return YES;
}
RootViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Main Menu";
}
No viewWillAppear, viewDidAppear, etc.
Displays a table of 0 elements.
- (UITableViewCell *)tableView:(UITableView *)tv
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
tv.backgroundColor = [UIColor whiteColor];
UITableViewCell *cell;
if (indexPath.row == 0)
cell = newsCell;
else if (indexPath.row == 1)
cell = configureCell;
else if (indexPath.row == 2)
cell = aboutCell;
return cell;
}
- (NSInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSInteger)section
{
return 0;
}
#pragma mark UITableViewDelegate Methods
- (CGFloat)tableView:(UITableView *)tv
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 84;
}
- (void) tableView:(UITableView *)tv
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (0 == indexPath.row)
{
}
else if (1 == indexPath.row)
{
}
else if (2 == indexPath.row)
{
}
}
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
[tableView release];
[newsCell release];
[configureCell release];
[aboutCell release];
}
RootViewController.h
#interface RootViewController : UIViewController
<UITableViewDataSource, UITableViewDelegate>
{
UITableView *tableView;
IBOutlet UIView *displaySplashScreen;
IBOutlet UITableViewCell *newsCell, *configureCell, *aboutCell;
}
#property (nonatomic, retain) IBOutlet UITableView *tableView;
There are a few issues here. First, your AppDelegate header should read:
#class RootViewController;
#interface MyAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
RootViewController *rootViewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#end
Then, in the implementation, add the root to the navigation controller and the navController to the window like this:
#import "RootViewController.h"
#implementation MyAppDelegate
#synthesize window;
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
rootViewController = [[RootViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:rootViewController];
[window addSubview:[navController view]];
[self.window makeKeyAndVisible];
return YES;
}
PengOne's answer is correct, except I'd make one small change. Do this:
#import "RootViewController.h"
#implementation MyAppDelegate
#synthesize window;
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
rootViewcontroller = [[RootViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:rootViewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
It's a better way to show a nav controller. As PengOne also said, it has to be a problem with your Interface Builder file. Unhook everything then hook it up again to see if the problem persists. If it does, check to make sure everything is named correctly. Good luck!