Master Detail Application not loading UIViewControllers - ios

I am creating an iPad version of Master Detail Application using XCode 4.5 with ARC. I have my iPadMaster.h/.m(as my master) and iPadDetailViewController.h/m(as my detail) set up.
I am trying to load different view controllers from iPadDetailViewController when users click/select the rows on iPadMaster.
On iPadDetailController.h, i set this:
#property int itemNumber;
On iPadMaster.h, i called it as:
#class iPadDetailViewController;
and proceeded with this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController * DVC = [[DetailViewController alloc]init];
DVC.itemNumber = indexPath.row;
}
On iPadDetailViewController, i set this:
- (void)configureView
{
switch (_itemNumber) {
case 1:
{
iPadLogin *next = [[iPadLogin alloc] init];
NSMutableArray *mut = [[NSMutableArray alloc]init];
mut = [self.splitViewController.viewControllers mutableCopy];
[mut replaceObjectAtIndex:1 withObject:next];
self.splitViewController.viewControllers = mut;
break;
}
default:{
self.view.backgroundColor = [UIColor whiteColor];
}
break;
}
}
//then i called it on:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
}
When i clicked the second row in the master table, item_number should be 1 and load the 'iPadLogin' but nothing happens... Any pointers is much appreciated...
Thanx in advance...

As I said in my comment, I think you should change the detail controller from the master controller. It is in the master that you're making the decision on what detail controller to go to (by picking a row in the table), so it should be the responsibility of the master controller to make the change. The code below should do that (note, however, if you're using a storyboard for your controllers, then you should be using [self.storyboard instantiateViewControllerWithIdentifier:#"whatever"] to get your next controller rather than alloc init).
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.row) {
case 1:
{
iPadLogin *next = [[iPadLogin alloc] init];
NSMutableArray *mut = [[NSMutableArray alloc]init];
mut = [self.splitViewController.viewControllers mutableCopy];
[mut replaceObjectAtIndex:1 withObject:next];
self.splitViewController.viewControllers = mut;
break;
}
case 2:
{
AnotherVC *another = [[AnotherVC alloc] init];
NSMutableArray *mut = [[NSMutableArray alloc]init];
mut = [self.splitViewController.viewControllers mutableCopy];
[mut replaceObjectAtIndex:1 withObject:another];
self.splitViewController.viewControllers = mut;
break;
}
default:{
UIViewController *detail = self.splitViewController.viewControllers[1];
detail.view.backgroundColor = [UIColor whiteColor];
}
break;
}
}

Related

List not being populated from other view

I have a view controller where the user can populate an item. Upon exiting the view that object should be saved, and loaded by the previous view which contains a list of all these objects. My NSLog shows that the object is being saved, but I can't get it to appear in the list view.
Code for both controllers is below. Thanks
List Controller
#import "ItemsViewController.h"
#import "Calculation.h"
#import "CalculationItemStore.h"
#import "CalculationDetailViewController.h"
#interface ItemsViewController()
#property UISegmentedControl *segment;
#end
#implementation ItemsViewController
- (instancetype)init
{
// Call the superclass's designated initializer
self = [super initWithStyle:UITableViewStylePlain];
if (self) {
UINavigationItem *navItem = self.navigationItem;
navItem.title = #"MACS";
// Create a new bar button item that will send
// addNewItem: to CalculationsViewController
UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(addNewItem:)];
// Set this bar button item as the right item in the navigationItem
navItem.rightBarButtonItem = bbi;
navItem.leftBarButtonItem = self.editButtonItem;
}
return self;
}
- (instancetype)initWithStyle:(UITableViewStyle)style
{
return [self init];
}
- (void)viewDidLoad
{
self.segment = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Publication", #"About", nil]];
self.tableView.tableHeaderView = _segment;
[_segment addTarget:self action:#selector(segmentPressed:) forControlEvents:UIControlEventValueChanged];
[self.tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:#"UITableViewCell"];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[[CalculationItemStore sharedStore] allCalculations] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Get a new or recycled cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"UITableViewCell" forIndexPath:indexPath];
// Set the text on the cell with the description of the item
// that is at the nth index of items, where n = row this cell
// will appear in on the tableview
NSArray *items = [[CalculationItemStore sharedStore] allCalculations];
Calculation *item = items[indexPath.row];
cell.textLabel.text = item.title;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CalculationDetailViewController *detailViewController = [[CalculationDetailViewController alloc] init];
NSArray *items = [[CalculationItemStore sharedStore] allCalculations];
Calculation *selectedItem = items[indexPath.row];
// Give detail view controller a pointer to the item object in row
detailViewController.calculation = selectedItem;
// Push it onto the top of the navigation controller's stack
[self.navigationController pushViewController:detailViewController
animated:YES];
}
- (void) tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
// If the table view is asking to commit a delete command...
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSArray *items = [[CalculationItemStore sharedStore] allCalculations];
Calculation *item = items[indexPath.row];
[[CalculationItemStore sharedStore] removeItem:item];
// Also remove that row from the table view with an animation
[tableView deleteRowsAtIndexPaths:#[indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
}
- (void) tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toIndexPath:(NSIndexPath *)destinationIndexPath
{
[[CalculationItemStore sharedStore] moveItemAtIndex:sourceIndexPath.row
toIndex:destinationIndexPath.row];
}
- (void)segmentPressed:(id)sender {
if (_segment.selectedSegmentIndex ==0) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://ferm.forestry.oregonstate.edu/facstaff/leshchinsky-ben"]];
}else if(_segment.selectedSegmentIndex ==1){
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, 320, 480)];
imageView.backgroundColor = [UIColor redColor];
[imageView setImage: [UIImage imageNamed:#"MACSLoad#2x.png"]];
[self.view addSubview: imageView];
sleep(5);
imageView.hidden = YES;
}
}
- (IBAction)addNewItem:(id)sender
{
// Create a new Calculation and add it to the store
Calculation *newItem = [[CalculationItemStore sharedStore] createCalculation];
CalculationDetailViewController *detailViewController = [[CalculationDetailViewController alloc]initForNewItem:YES];
detailViewController.calculation = newItem;
detailViewController.dismissBlock = ^{
[self.tableView reloadData];
};
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:detailViewController];
navController.modalPresentationStyle = UIModalPresentationFullScreen;
// [self presentViewController:navController animated:YES completion:NULL];
[self.navigationController pushViewController:detailViewController animated:YES];
}
#end
Item controller save method
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
//clear responder
[self.view endEditing:YES];
//save changes
BOOL success = [[CalculationItemStore sharedStore]saveChanges];
if(success){
NSLog(#"Saved all calcs");
}else{
NSLog(#"failure saving");
}
}
I think the issue is that by the time the table reloadData is called on the first controller, the second one is not done loading. You could notify the first VC to reload the data once the second one is done saving It can be notified using
create a delegate on the second one or
using notifications
Let me know if you need more details on how to do this!
Looks like there is also an issue with the object creation.
your calculation elements are not getting created properly. When you are ready to save.. look at the contents of self.privateItems. They values filled in are nil. You will need to instantiate the Strings properly.
engineerName needs to be alloc'd and other strings as well.
and when user hits Done, the values from the text boxes need to be set to the your data model.

How do I get from one table view to the other

I'm a beginner at this and I'm trying to create an app in which you can select from several options. Therefore, I have a table view. Depending on which cell was tapped, another screen should appear with a second table view where I can checkmark things. Right now, I have one view controller in which I'm creating the first table view. How do I get to the second table view and how does it display the other array. Here is the code so far...
#interface ViewController (){
NSArray *genres;
NSArray *images;
NSMutableArray *array1;
NSMutableArray *array2;
NSMutableArray *array3;
}
#end
#implementation ViewController
#synthesize tableView;
(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = #"Genres";
// Arrays must be in same order for image and title correspondance
genres = [[NSArray alloc] initWithObjects:
#"a1",
#"a2",
#"a3",
nil];
images = [[NSArray alloc] initWithObjects:
#"1.jpeg",
#"2.jpeg",
#"3.jpeg",
nil];
array1 = [[NSMutableArray alloc] initWithObjects:
#"1.1",
#"1.2",
#"1.3",
nil];
array2 = [[NSMutableArray alloc] initWithObjects:
#"2.1",
#"2.2",
#"2.3",
nil];
array3 = [[NSMutableArray alloc] initWithObjects:
#"3.1",
#"3.2",
#"3.3",
nil];
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger) numberOfSectionsInTableView:(UITableView *) tableView{
return 1;
}
- (NSInteger) tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger)section{
return genres.count;
}
//Customize the apperance of table view cells
- (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];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}
//Configure Cell
cell.textLabel.text = [genres objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[images objectAtIndex:indexPath.row]];
return cell;
}
#pragma mark -
#pragma mark Table view delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSArray *selectedArray = [[NSArray alloc] init];
switch (indexPath.row) {
case 0:
selectedArray = array1;
break;
case 1:
selectedArray = array2;
break;
case 2:
selectedArray = array3;
break;
default:
break;
}
}
You can 1) create second view controller from code and set array like argument or 2) use segue interface:
Try to follow this complete answer:
https://stackoverflow.com/a/9736559/2429147
Also this link may be helpful regarding to work with segues and storyboards:
http://agilewarrior.wordpress.com/2012/01/25/how-segue-in-ios-and-pass-data-from-one-viewcontroller-to-another/

Master Detail Application for iPad

I am creating an iPad version of Master Detail Application using XCode 4.5 with ARC.
I have my iPadMaster.h/.m(as my master) and iPadDetailViewController.h/m(as my detail) set up.
I am trying to load different view controllers from iPadDetailViewController when users click/select the rows on iPadMaster.
I set this on iPadMaster.m at:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
iPadDetailViewController * DVC = [[iPadDetailViewController alloc]initWithNibName:nil bundle:nil];
DVC.itemNumber = indexPath.row;
}
and tried this stupid stunt on iPadDetailViewController.m on [viewDidLoad]:
switch(_itemNumber)
{
case 0:
{
//Detail row
vc1 *viewController = [[vc1 alloc] init];
[self presentViewController:viewController animated:YES completion:nil];
break;
}
case 1:
{
//Report row
vc2 *viewController = [[vc2 alloc] init];
//viewController.somePassedInData = theDataToPass;
[self presentViewController:viewController animated:YES completion:nil];
break;
}
case 2:
{
//Report row
vc3 *viewController = [[vc3 alloc] init];
//viewController.somePassedInData = theDataToPass;
[self presentViewController:viewController animated:YES completion:nil];
break;
}
...
case 9:
{
//Report row
vc9 *viewController = [[vc9 alloc] init];
//viewController.somePassedInData = theDataToPass;
[self presentViewController:viewController animated:YES completion:nil];
break;
}
default:
{
break;
}
On iPhone i would just plaster the 'switch cases' in the - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath but i'm at lost with iPad environment...
Thanx in advance...
The split view controller has a property, viewControllers. The object at index 1 is the detail controller. You should just create a mutable copy of viewControllers, replace the object at index 1 with your new controller, and set that array to be the split view's arrayControllers.
NextController *next = [[NextController alloc] init..... // or however you get your new controller
NSMutableArray *mut = [self.splitViewController.viewControllers mutableCopy];
[mut replaceObjectAtIndex:1 withObject:next];
self.splitViewController.viewControllers = mut;

NSInvalidArgumentException tableView:numberOfRowsInSection error...but I am not using a tabbed view and I haven't used the Interface Builder at all

I'm trying to create an app for the iPhone where I pull data from a database and then display it on a table (a completely new view from the main screen in which I ask the user to enter in data). I've used this framework to help switch views in my app:
http://www.pushplay.net/2009/05/framework-for-having-multiple-views-in-an-iphone-app/
And basically, I modified it a little bit. I have it implemented fine, but when I populate the table I have to pass the array of information from a class where I pull the data from into the view where I display the table. I found the problem with my code (displayed below).
-(void) displayView:(int)intNewView{
NSLog(#"%i", intNewView);
[currentView.view removeFromSuperview];
[currentView release];
ServiceProvider *g = [[ServiceProvider alloc] init];
ServiceProvider *l = [[ServiceProvider alloc] init];
[g setSPNAME:#"george"];
[l setSPNAME:#"luuuuuusaaaa"];
passInTableToTOI = [[NSMutableArray alloc] initWithObjects:g, l, nil];
ResultsPage *rP = [[ResultsPage alloc] initWithNibName:#"TableOfItems" bundle:[NSBundle mainBundle]];
TableOfItems *tOI = [[TableOfItems alloc] init];
switch (intNewView) {
case 1:
currentView = [[SearchPage alloc] init];
break;
case 2:
[tOI setPassedThroughTable:passInTableToTOI];
[rP setResultsTable:tOI];
currentView = rP;
break;
case 3:
currentView = [[ShowAllPage alloc] init];
break;
/*default:
break;*/
}
//[rP release];
//[tOI release];
[self.view addSubview:currentView.view];
}
The table gets passed fine, but when I try to display the view I get this error "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ResultsPage tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x4e396c0". I feel like I'm not giving enough information, so please if you'd like more information don't hesitate. I'm not sure what else I'm being vague on so it'd help to let me know how I can be more specific. Thanks a lot everyone.
[[EDIT 1]]
//===========ResultsPage.h
#import <UIKit/UIKit.h>
#class TopBottomRectangles;
#class TableOfItems;
#class SearchTextBox;
#interface ResultsPage : UIViewController {
TopBottomRectangles *tbRects;
TableOfItems *resultsTable;
SearchTextBox *sTB;
}
#property (nonatomic, retain) IBOutlet TableOfItems *resultsTable;
#end
//===================ResultsPage.m file
#import "ResultsPage.h"
#import "TopBottomRectangles.h"
#import "TableOfItems.h"
#import "SearchTextBox.h"
#import "MultiviewAppDelegate.h"
#implementation ResultsPage
#synthesize resultsTable;
-(void)goToShowAllPage
{
MultiviewAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate displayView:3];
}
// 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 viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
NSLog(#"load the results page");
tbRects = [[TopBottomRectangles alloc] init];
sTB = [[SearchTextBox alloc] init];
[self.view addSubview:[resultsTable view]];
[self.view addSubview:tbRects.bottomBG_View];
[self.view addSubview:tbRects.topBG_View];
[self.view addSubview:sTB.textBox_BG_border];
[self.view addSubview:sTB.textBox_BG];
[self.view addSubview:sTB.textBox];
//[self.view addSubview:btnTwo];
[super viewDidLoad];
}
/*
// 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 {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
NSLog(#"dealloc results page");
[sTB release];
[resultsTable release];
[tbRects release];
[super dealloc];
}
#end
[[EDIT 2]]
//====TableOfItems.h file
#import <UIKit/UIKit.h>
#interface TableOfItems : UITableViewController {
NSMutableArray *listOfItems;
NSMutableArray *passedThroughTable;
}
#property (nonatomic, retain) NSMutableArray* passedThroughTable;
#end
//=====TableOfItems.m file
#import "TableOfItems.h"
#import "MyTableCell.h"
#import "ServiceProvider.h"
#implementation TableOfItems
#synthesize passedThroughTable;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.navigationController.navigationBarHidden = YES;
//set the size of the table
[self fixTableSize];
NSMutableArray *spName = [[NSMutableArray alloc] init];
for (int i = 0; i < [passedThroughTable count]; i++){
ServiceProvider *willBeGone = [[ServiceProvider alloc] init];
willBeGone = [passedThroughTable objectAtIndex:i];
[spName addObject:willBeGone.SPNAME];
NSLog(#"%#", [spName objectAtIndex:i]);
//[willBeGone release];
}
//Initialize the array.
listOfItems = [[NSMutableArray alloc] init];
NSArray *countriesToLiveInArray = [NSArray arrayWithObjects:#"Iceland", #"Greenland", #"Switzerland", #"Norway", #"New Zealand", #"Greece", #"Rome", #"Ireland", nil];
NSDictionary *countriesToLiveInDict = [NSDictionary dictionaryWithObject:countriesToLiveInArray forKey:#"Countries"];
NSArray *countriesLivedInArray = [NSArray arrayWithObjects:#"India", #"U.S.A", nil];
NSDictionary *countriesLivedInDict = [NSDictionary dictionaryWithObject:countriesLivedInArray forKey:#"Countries"];
[listOfItems addObject:countriesToLiveInDict];
[listOfItems addObject:countriesLivedInDict];
}
-(void)fixTableSize{
CGRect screenBounds = [[UIScreen mainScreen] bounds];
CGFloat screenScale = [[UIScreen mainScreen] scale];
NSInteger height = screenBounds.size.height * screenScale;
NSInteger windowHeight = 100;
NSInteger yFromTop = 60;
NSInteger yFromBottom = height + 30 - windowHeight;
self.tableView.frame = CGRectMake(0,yFromTop,320,yFromBottom - yFromTop);
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return [listOfItems count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSArray *sectionTitles = [[NSArray alloc] initWithObjects:#"Countries to visit", #"Countries visited", nil];
return [sectionTitles objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
NSLog(#"====");
//Number of rows it should expect should be based on the section
NSDictionary *dictionary = [listOfItems objectAtIndex:section];
NSArray *array = [dictionary objectForKey:#"Countries"];
return [array count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *MyIdentifier = [NSString stringWithFormat:#"MyIdentifier %i", indexPath.row];
MyTableCell *cell = (MyTableCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:#"Countries"];
if (cell == nil) {
cell = [[[MyTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(1, 1, 30.0,
tableView.rowHeight)] autorelease];
[cell addColumn:60];
//label.tag = FIRCOL_TAG;
label.font = [UIFont systemFontOfSize:12.0];
label.text = [NSString stringWithFormat:#"%d:00", indexPath.row];
label.textAlignment = UITextAlignmentRight;
label.textColor = [UIColor blueColor];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin;
[cell.contentView addSubview:label];
label = [[[UILabel alloc] initWithFrame:CGRectMake(70.0, 0, 110,
tableView.rowHeight)] autorelease];
[cell addColumn:260];
//label.tag = SECCOL_TAG;
label.font = [UIFont systemFontOfSize:12.0];
label.text = [NSString stringWithFormat:#"%#", [array objectAtIndex:indexPath.row]];
label.textAlignment = UITextAlignmentLeft;
label.textColor = [UIColor blackColor];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label];
label = [[[UILabel alloc] initWithFrame:CGRectMake(270.0, 0, 30,
tableView.rowHeight)] autorelease];
//[cell addColumn:180];
//label.tag = THIRCOL_TAG;
label.font = [UIFont systemFontOfSize:12.0];
// add some silly value
label.text = [NSString stringWithFormat:#"$%d", indexPath.row * 4];
label.textAlignment = UITextAlignmentLeft;
label.textColor = [UIColor blueColor];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label];
}
return cell;
}
#end
I think the problem is not in the code that shows you. When you read the Exception, the problem is in ResultsPage class
ResultsPage *rP = [[ResultsPage alloc] initWithNibName:#"TableOfItems" bundle:[NSBundle mainBundle]];
in tableview data delegate method :
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
So maybe the returned value is wrong or some arguments. We need ResultsPage code.
Bye
The error message is telling you that you're calling a UITableViewDataSource method on something that is not a UITableViewDataSource, because it does not implement the method: tableView:numberOfRowsInSection.
This method has to do with UITableView objects, and I don't see any UITableView objects anywhere in your code (nor in the PushPlay examples.)
Without going through it with a fine-toothed comb, I'd say you're trying to use an NSMutableArray in place of a UITableView and it's delegates.
An array makes a fine data source for a UITableView, but is not a direct substitute for a tableView.
Hope that helps.
-Mike
I found my problem. It was this clause:
ResultsPage *rP = [[ResultsPage alloc] initWithNibName:#"TableOfItems" bundle:[NSBundle mainBundle]];
TableOfItems *tOI = [[TableOfItems alloc] init];
It's supposed to be
ResultsPage *rP = [[ResultsPage alloc] init];
TableOfItems *tOI = [[TableOfItems alloc] initWithNibName:#"TableOfItems" bundle:[NSBundle mainBundle]];
I had a .xib file called "TableOfItems" and that should be corresponding to the class that populates the user interface, TableOfItems. This class hass all those delegates which creates the rows and sections and whatnot.
I'm sorry that I made such a foolish error...I don't think this would have been easily found by someone who didn't have access to the .xcodeproj

Updating DetailViewController from RootController

I'm trying to create an iPad application with a similar user interface to Apple's Mail application, i.e:
RootView controller (table view) on the left hand side of the split view for navigation with a multiple view hierarchy. When a table cell is selected a new table view is pushed on the left hand side
The new view on the left side can update the detail view.
I can accomplish both tasks, but not together.
I mean I can make a multi-level table view in the RootController.
Or I can make a single-level table view in the RootController which can update the detailViewController.
Can anyone tell me how to make a multi-level table in the RootController which can update a detailViewController?
There is more source code at the link but below is the method in which I presume I have to declare a new detailViewController (which has to be put in the UISplitViewController):
- (void)tableView:(UITableView *)TableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
//Get the children of the present item.
NSArray *Children = [dictionary objectForKey:#"Children"];
//
if([Children count] == 0) {
/*
Create and configure a new detail view controller appropriate for the selection.
*/
NSUInteger row = indexPath.row;
UIViewController <SubstitutableDetailViewController> *detailViewController = nil;
if (row == 0) {
FirstDetailViewController *newDetailViewController = [[FirstDetailViewController alloc]initWithNibName:#"FirstDetailView" bundle:nil];
detailViewController = newDetailViewController;
}
if (row == 1) {
SecondDetailViewController *newDetailViewController = [[SecondDetailViewController alloc]initWithNibName:#"SecondDetailView" bundle:nil];
detailViewController = newDetailViewController;
}
// Update the split view controller's view controllers array.
NSArray *viewControllers = [[NSArray alloc] initWithObjects:self.navigationController, detailViewController, nil];
splitViewController.viewControllers = viewControllers//nothing happens.....
[viewControllers release];//
}
else {
//Prepare to tableview.
RootViewController *rvController = [[RootViewController alloc]initWithNibName:#"RootViewController" bundle:[NSBundle mainBundle]];
//Increment the Current View
rvController.current_level += 1;
//Set the title;
rvController.current_title = [dictionary objectForKey:#"Title"];
//Push the new table view on the stack
[self.navigationController pushViewController:rvController animated:YES];
rvController.tableDataSource = Children;
[rvController.tableView reloadData]; //without this instrucion,items won't be loaded inside the second level of the table
[rvController release];
}
}
Sorry, but I cannot post my source code as it contains sensitive information. When I have more time available I will create a separate project and upload the code somewhere.
Here are extracts of how I have done it so far (I welcome any feedback).
The RootViewController - Note I have 4 sections in my root table.
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// Detail view logic
NSUInteger section = indexPath.section;
UIViewController <SubstitutableDetailViewController> *detailViewController = nil;
if (section == 2) {
ProductSearchDetailView *viewController = [[ProductSearchDetailView alloc] initWithNibName:#"ProductSearchDetailView" bundle:nil];
detailViewController = viewController;
//[viewController release];
}
else {
DetailViewController *defaultDetailViewController = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:nil];
detailViewController = defaultDetailViewController;
//[defaultDetailViewController release];
}
// Navigation logic
switch (section) {
case 0:
{
break;
}
case 1:
{
break;
}
case 2:
{
// new Navigation view
ProductSearchViewController *viewController = [[ProductSearchViewController alloc] initWithNibName:#"ProductSearchViewController" bundle:nil];
viewController.navigationItem.backBarButtonItem.title = #"Back";
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
break;
}
case 3:
{
StoreLocatorNavController *viewController = [[StoreLocatorNavController alloc] initWithNibName:#"StoreLocatorNavController" bundle:nil];
viewController.navigationItem.backBarButtonItem.title = #"Back";
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
break;
}
}
// Update the split view controller's view controllers array.
NSArray *viewControllers = [[NSArray alloc] initWithObjects:self.navigationController, detailViewController, nil];
splitViewController.viewControllers = viewControllers;
[viewControllers release];
// Dismiss the popover if it's present.
if (popoverController != nil) {
[popoverController dismissPopoverAnimated:YES];
}
// Configure the new view controller's popover button (after the view has been displayed and its toolbar/navigation bar has been created).
if (rootPopoverButtonItem != nil) {
[detailViewController showRootPopoverButtonItem:self.rootPopoverButtonItem];
}
[detailViewController release];
}
NSNotificationCenter part
Add this to ProductSearchViewController:
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *itemAtIndex = (NSDictionary *)[self.productResults objectAtIndex:indexPath.row];
[[NSNotificationCenter defaultCenter] postNotificationName:#"updateProduct" object:itemAtIndex];
}
And finally, add this to ProductSearchDetailViewController:
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(updateTheProductDetails:) name:#"updateProduct" object:nil];
}
- (void)updateTheProductDetails:(NSNotification *)notification {
NSDictionary *productDictionary = [NSDictionary dictionaryWithDictionary:[notification object]];
// product name
_productName.text = [productDictionary objectForKey:#"ProductDescription"];
}
Hope it helps!

Resources