Alert icon
We're changing our privacy policy. This stuff matters.  Learn more  Dismiss

iPhone Programming - UITableViews: Pushing New Table Views + EXTRA

Loading...

Sign in or sign up now!
Alert icon
Upgrade to the latest Flash Player for improved playback performance. Upgrade now or more info.
9,548
Loading...
Alert icon
Sign in or sign up now!
Alert icon

Uploaded by on Jun 17, 2011

In this UITableView tutorial I teach you how to push a new UITableView or UIView. The extra teaches you how to load different data into the table view from different arrays depending on what row is clicked.

Main Code Used:

In RootViewController.m:

#import "PetsTableViewController.h"

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ PetsTableViewController *pets = [[PetsTableViewController alloc] initWithNibName:@"PetsTableViewController" bundle:nil]; if ([[petsArray objectAtIndex:indexPath.row] isEqual:@"Dog"]) { pets.petsInt = 0; [pets setTitle:[petsArray objectAtIndex:indexPath.row]]; } if ([[petsArray objectAtIndex:indexPath.row] isEqual:@"Cat"]) { pets.petsInt = 1; [pets setTitle:[petsArray objectAtIndex:indexPath.row]]; } if ([[petsArray objectAtIndex:indexPath.row] isEqual:@"Snake"]) { pets.petsInt = 2; [pets setTitle:[petsArray objectAtIndex:indexPath.row]]; } [self.navigationController pushViewController:pets animated:YES]; [pets release];
}

In PetsTableViewController.h:

NSMutableArray *dogArray; NSMutableArray *catArray; NSMutableArray *snakeArray; int petsInt;
}

@property int petsInt;

In PetsTableViewController.m:
In viewDidLoad:
dogArray = [[NSMutableArray alloc] initWithObjects:@"Dog 1", @"Dog 2", @"Dog 3", @"Dog 4", nil]; catArray = [[NSMutableArray alloc] initWithObjects:@"Cat 1", @"Cat 2", nil]; snakeArray = [[NSMutableArray alloc] initWithObjects:@"Snake 1", @"Snake 2", @"Snake 3", nil];

Don't forget to release them:
- (void)dealloc
{ [dogArray release]; [catArray release]; [snakeArray release]; [super dealloc];
}
In numberOfSectionsInTableView return 1;

In numberOfRowsInSection:
if (petsInt == 0) return [dogArray count]; if (petsInt == 1) return [catArray count]; if (petsInt == 2) return [snakeArray count]; [self.tableView reloadData];

In cellForRowAtIndexPath:
if (petsInt == 0) cell.textLabel.text = [[dogArray objectAtIndex:indexPath.row] retain]; if (petsInt == 1) cell.textLabel.text = [[catArray objectAtIndex:indexPath.row] retain]; if (petsInt == 2) cell.textLabel.text = [[snakeArray objectAtIndex:indexPath.row] retain]; [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

Next Video: http://www.youtube.com/watch?v=99Ssk1-HUq4
UITableView Playlist: http://www.youtube.com/playlist?p=PLDE7D5FFAA97747AE&feature=mh_lolz
Previous Tutorial: http://www.youtube.com/watch?v=eO6AlkCcL3g

Twitter: http://twitter.com/failcakeapps
Apple Developer Center: http://developer.apple.com/devcenter/ios/index.action
Website: http://failcake.webs.com/
Channel: http://youtube.com/milmersxcode

  • likes, 0 dislikes

Link to this comment:

Share to:

Uploader Comments (MilmersXcode)

  • I can help you with all of these comments. After all you helped me when I needed it. Just a quick favor to ask, can you by any chance send me the code snippets for adding search to this tutorial?:) please?

  • @MrAmp76 Yea. Again I am sorry for not posting stuff. I have been a bit busy with starting school again and I have 3 exams next month!

    So just PM me and I will send it. However at the moment it's 00:07am here so I am in bed and unable to give you the code but I will do it tomorrow :D.

    Milmer.

  • Missjedahh76 the error control reaches the end of .... Can be resolved by declaring an else after if.

    {

    else {

    Return 1;

    }

  • @MrAmp76 Thanks :D. The comments have been getting on top of me lately. I appreciate your help :D

  • sorry to tell that it didn't work please i really need this and i can't see where is the error

    i have two errors

    (control reaches end of non-void function )

    two of the same error one at numberOfRowsInSection at both .m files

  • @missjeddah76 Exactly what MrAmp76 said. Solve it by putting an:

    else Return 1;

see all

All Comments (43)

Sign In or Sign Up now to post a comment!
  • Great tutorials, I have learnt heaps from your channel, in my dictionary i have a mp3 file i wish to push to the detail view and place in a IBAction play button, so user taps and the mp3 song plays. An ideas with the code?

  • My PetsViewControll is not pushing different Arrays. Every time i push it only shows the dog Array

  • Hi,(unrelated question) I'm just curious, what area is your accent from? Sounds a lot like my friend's accent.

  • I just want to take this moment to really thank you, Milmer'sXcode. You have helped me achieve my ios programming goals, and have taught almost everything i know. I hope you continue to make these brilliant tutorials, and continue to get more and more subscribers :)

  • Missheddah76 glad I could help. If you need more help please let me know. :)

  • @MrAmp76 thanks a lot .... my application is working very good :)

  • Febassa77 most of tuts are written for xcode up to 4.1 to use everything, specially for beginners I'll always advice them to run two different xcodes one is the latest and the other one can be any other version that they are comfortable with. I use three just to be able to convert my codes properly to higher versions. This practice keeps me sharp. In Xcode 4.2 ARC is activated but to get around it you can use the upgrade as I mentioned in the last post.

  • Febassa77 if you are running Xcode for lion just update your project as the warning appears and it should take care of the warnings. If you have a better set of codes please post them so everyone can benefit. I've ran his codes through latest beta of Xcode running on lion and it works just fine.

  • GUYS IF U HAVE THE LAST XCODE VERSION (4.2) IT IS NOT NECESARY TO USE THE RETAIN OR RELEASE, IF U DO, U WILL HAVE SEVERAL ERRORS

  • Here is my e mail if it is easier. Do it at your convenience brother. I know you're busy and doing a lot. I am grateful for your help.

    Adrian_ phillips@att.net

    Let me know if I can help in any way brother.

Loading...

Alert icon
0 / 00Unsaved Playlist Return to active list
    1. Your queue is empty. Add videos to your queue using this button:
      or sign in to load a different list.
    Loading...Loading...Saving...
    • Clear all videos from this list
    • Learn more