Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-28-2011, 09:33 AM   #1 (permalink)
Registered Member
 
Join Date: Dec 2010
Location: Germany, Frankfurt
Posts: 30
brush51 is on a distinguished road
Default next/previous function in detailView of TableView

Hello @all,

i want to make a next and previous function in my DetailView. The DetailView appears when i click a TableView's cell, didSelectRowAtIndexPath:.

I have created a method for calling the detailView and this method is called in didSelectRowAtIndexPath:

Code:
//DashboardViewController.h
- (void) detailViewAufruf:(id) sender;

//DashboardViewController.m
- (void) detailViewAufruf:(id) sender {
	
	NSLog(@"selectedDetail: %@", selectedDetail);
	
	//present and release controller
	if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { 
		detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController_iPad" bundle:[NSBundle mainBundle]];
		detailViewController.selectedDetail = [[NSArray alloc] init];
		detailViewController.selectedCardTitle2 = [[NSArray alloc] init];
		
		detailViewController.selectedDetail = [selectedDetail valueForKey:@"cardText"];
		detailViewController.selectedCardTitle2 = [selectedCardTitle valueForKey:@"cardTitle"];
		detailViewController.selectedRow2 = [self.tableViews indexPathForSelectedRow];
		
		detailViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
		[dashboardViewController_iPad  presentModalViewController:detailViewController animated:YES];
		
		[detailViewController release];
	} else { 
		detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController_iPhone" bundle:[NSBundle mainBundle]];
		detailViewController.selectedDetail = [[NSArray alloc] init];
		detailViewController.selectedCardTitle2 = [[NSArray alloc] init];

		detailViewController.selectedDetail = [selectedDetail valueForKey:@"cardText"];
		detailViewController.selectedCardTitle2 = [selectedCardTitle valueForKey:@"cardTitle"];
		detailViewController.selectedRow2 = [self.tableViews indexPathForSelectedRow];
		
		detailViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
		[dashboardViewController_iPad  presentModalViewController:detailViewController animated:YES];
		
		[detailViewController release];		
	}
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	selectedDetail = [[NSArray alloc] initWithObjects:[[self fetchedResultsController] objectAtIndexPath:indexPath], nil];
	selectedCardTitle = [[NSArray alloc] initWithObjects:[[self fetchedResultsController] objectAtIndexPath:indexPath], nil];
	
	[self detailViewAufruf:indexPath];
}
At next, i want to jump to the next cell, when i am in detailView and press the nextCell button:

Code:

//DetailViewController.m

- (IBAction) previousCard:(id) sender {
	NSLog(@"previousBtn");
	
	DashboardViewController *parent = (DashboardViewController *)self.parentViewController;
	NSLog(@"parent: %@", parent); //Log == parent: <UIViewController: 0x4e99ee0>

	
	NSIndexPath *actualIndexPath = selectedRow2;
	NSIndexPath * newIndexPath = [NSIndexPath indexPathForRow:actualIndexPath.row-1 inSection:actualIndexPath.section];
	//[parent.tableViews selectRowAtIndexPath:newIndexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];

	
	if([parent.view.subviews objectAtIndex:0]) {
		UIView * subview = (UIView *) [parent.view.subviews objectAtIndex:0];
		UITableView *tView = (UITableView *) subview;

		//NSLog(@"tView.delegate: %@", tView.delegate);

		/*
		[tView selectRowAtIndexPath:newIndexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
		[tView.delegate tableView:tView didSelectRowAtIndexPath:newIndexPath];
		//[tView.delegate tableView:tView didSelectRowAtIndexPath:newIndexPath];
		*/
		
		[self.detailViewController detailViewAufruf:sender];
	}

}
And if i click to the next cell iam getting an error:
[UIViewController detailViewAufruf:]: unrecognized selector sent to instance 0x5958460

Whats wrong here, can someone help me out?
Thanks,
brush51
brush51 is offline   Reply With Quote
Old 10-28-2011, 10:58 AM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Ok, several issues here. First let's talk about the error:

Code:
[UIViewController detailViewAufruf:]: unrecognized selector sent to instance 0x5958460
This says that you are sending the detailViewAufruf: message to a plain view controller. Plain view controllers don't have that method, which is a problem. I'm not quite seeing where that would be the case, but somewhere you are not properly creating your own subclass instance.

Next problem is this:

Code:
1. detailViewController.selectedDetail = [[NSArray alloc] init];
2. detailViewController.selectedCardTitle2 = [[NSArray alloc] init];
		
3. detailViewController.selectedDetail = [selectedDetail valueForKey:@"cardText"];
4. detailViewController.selectedCardTitle2 = [selectedCardTitle valueForKey:@"cardTitle"];
1,2: First of all, these arrays are leaked. See the properties link in my signature for an explanation. Second of all, they are not necessary, because you turn around and replace them in 3, 4. Think about it like this:

int variable = 3;
variable = 10;

What happened to 3? It doesn't matter anymore, because now the value is 10. Same thing here. 10 is not 3, and your cardText stuff is not the same as the array you first created.

3,4: You seem to have separated your data into different dictionaries. That's just awkward, and will be difficult to maintain. Make a new model object, Card, and give it whatever properties are necessary. title, text, etc. Then you'll only need 1 array containing these Cards.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 10-28-2011, 11:02 AM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by brush51 View Post
Hello @all,

i want to make a next and previous function in my DetailView. The DetailView appears when i click a TableView's cell, didSelectRowAtIndexPath:.

I have created a method for calling the detailView and this method is called in didSelectRowAtIndexPath:

Code:
//DashboardViewController.h
- (void) detailViewAufruf:(id) sender;

//DashboardViewController.m
- (void) detailViewAufruf:(id) sender {
	
	NSLog(@"selectedDetail: %@", selectedDetail);
	
	//present and release controller
	if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { 
		detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController_iPad" bundle:[NSBundle mainBundle]];
		detailViewController.selectedDetail = [[NSArray alloc] init];
		detailViewController.selectedCardTitle2 = [[NSArray alloc] init];
		
		detailViewController.selectedDetail = [selectedDetail valueForKey:@"cardText"];
		detailViewController.selectedCardTitle2 = [selectedCardTitle valueForKey:@"cardTitle"];
		detailViewController.selectedRow2 = [self.tableViews indexPathForSelectedRow];
		
		detailViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
		[dashboardViewController_iPad  presentModalViewController:detailViewController animated:YES];
		
		[detailViewController release];
	} else { 
		detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController_iPhone" bundle:[NSBundle mainBundle]];
		detailViewController.selectedDetail = [[NSArray alloc] init];
		detailViewController.selectedCardTitle2 = [[NSArray alloc] init];

		detailViewController.selectedDetail = [selectedDetail valueForKey:@"cardText"];
		detailViewController.selectedCardTitle2 = [selectedCardTitle valueForKey:@"cardTitle"];
		detailViewController.selectedRow2 = [self.tableViews indexPathForSelectedRow];
		
		detailViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
		[dashboardViewController_iPad  presentModalViewController:detailViewController animated:YES];
		
		[detailViewController release];		
	}
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	selectedDetail = [[NSArray alloc] initWithObjects:[[self fetchedResultsController] objectAtIndexPath:indexPath], nil];
	selectedCardTitle = [[NSArray alloc] initWithObjects:[[self fetchedResultsController] objectAtIndexPath:indexPath], nil];
	
	[self detailViewAufruf:indexPath];
}
At next, i want to jump to the next cell, when i am in detailView and press the nextCell button:

Code:

//DetailViewController.m

- (IBAction) previousCard:(id) sender {
	NSLog(@"previousBtn");
	
	DashboardViewController *parent = (DashboardViewController *)self.parentViewController;
	NSLog(@"parent: %@", parent); //Log == parent: <UIViewController: 0x4e99ee0>

	
	NSIndexPath *actualIndexPath = selectedRow2;
	NSIndexPath * newIndexPath = [NSIndexPath indexPathForRow:actualIndexPath.row-1 inSection:actualIndexPath.section];
	//[parent.tableViews selectRowAtIndexPath:newIndexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];

	
	if([parent.view.subviews objectAtIndex:0]) {
		UIView * subview = (UIView *) [parent.view.subviews objectAtIndex:0];
		UITableView *tView = (UITableView *) subview;

		//NSLog(@"tView.delegate: %@", tView.delegate);

		/*
		[tView selectRowAtIndexPath:newIndexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
		[tView.delegate tableView:tView didSelectRowAtIndexPath:newIndexPath];
		//[tView.delegate tableView:tView didSelectRowAtIndexPath:newIndexPath];
		*/
		
		[self.detailViewController detailViewAufruf:sender];
	}

}
And if i click to the next cell iam getting an error:
[UIViewController detailViewAufruf:]: unrecognized selector sent to instance 0x5958460

Whats wrong here, can someone help me out?
Thanks,
brush51

A number of things are wrong.

Your DashboardViewController class is the class with the table view, right? It has a method detailViewAufruf that is supposed to create and push a detail view controller, passing in the data you want the detail view controller to display.

You say that method is called by didSelectRowAtIndexPath.

In your detailViewAufruf method, you're allocating a pair of empty arrays and assigning them to the detail view controller's selectedDetail and selectedCardTitle2 properties. However, you then assign different values to selectedDetail and selectedCardTitle2. As a result, you leak the empty NSArrays that you assign to those properties a few lines before. Get rid of those first lines that create empty arrays and assign them. There is no point, and it causes a leak.

Your detailViewAufruf method looks like it uses a instance variable selectedDetail to get to the data that you are supposed to pass to the detail controller. However, you also ask your table view for it's selected index path.

Further down, in your detail view controller, you try to call the detailViewAufruf method as if it's a method of the detail view controller, but it is actually a method if your DashboardViewController.


How is your data for the entries in the table view stored? Do you have an array of data objects, one for each entry in the table view?

If you don't, you should. Imagine this:

You create an array of dictionaries, one for each table view entry. Each dictionary contains all the data for that entry.
Make the array of data dictionaries a property of your DashboardViewController, let's call that property dataArray.

Add an integer property indexToDisplay to your detail view controller. Also add a property dataDelegate to the detail view controller. That property would be of type DashboardViewController. (or better yet, create a DashboardDataProtocol, and make the dataDelegate a property of class (id) <DashboardDataProtocol>. That might be a little beyond you, however.)

When the user selects a cell from the table view, you write a didSelectRowAtIndexPath: method that looks something like this:


- (void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)indexPath
{
NSInteger row = [indexPath row];
detailViewController =
[[DetailViewController alloc] initWithNibName: @"DetailViewController_iPad"
bundle:[NSBundle mainBundle]];
detailViewController.indexToDisplay = row;
detailViewController.dataDelegate = self;
[dashboardViewController_iPad presentModalViewController:detailViewController animated:YES];
}

The property "indexToDisplay" would tell the detail view controller which item to display. Then it's viewWillAppear method would call a displayNewDataForRow: method to copy that data to the appropriate places in it's view. That method would use the dataDelegate property to ask the DashboardViewController for the dataArray, get the data for the specified index, and display it.

Now, if you write a nextButton method, that method would increment the row count and call the displayNewDataForRow method on the new row number.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 11-21-2011, 06:00 AM   #4 (permalink)
Registered Member
 
Join Date: Dec 2010
Location: Germany, Frankfurt
Posts: 30
brush51 is on a distinguished road
Default

Thanks for your replies.
I have here my sample application. In this application the data is stored in an nsmutablearray.

Link: File-Upload.net - TableRotate.zip

Last edited by brush51; 11-21-2011 at 06:02 AM. Reason: link to the sample app
brush51 is offline   Reply With Quote
Old 11-21-2011, 07:14 AM   #5 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

So? Nobody is going to write code for you.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 11-21-2011, 07:30 AM   #6 (permalink)
Registered Member
 
Join Date: Dec 2010
Location: Germany, Frankfurt
Posts: 30
brush51 is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
So? Nobody is going to write code for you.
Lol

I dont want ready code. I want just to know if i am on the right way.

Duncan C's and your post are really informativ but i have not to much experience to do it with a protocol or an additional delegate.
brush51 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 397
13 members and 384 guests
13dario13, 7twenty7, buggen, EvilElf, glenn_sayers, j.b.rajesh@gmail.com, LunarMoon, morterbaher, QuantumDoja, sacha1996, Sami Gh, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,673
Threads: 94,122
Posts: 402,906
Top Poster: BrianSlick (7,990)
Welcome to our newest member, morterbaher
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:30 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0