Home Tutorial Iphone Examples Table view with multiple view and sections

 
 

Share on Google+Share on Google+
Table view with multiple view and sections
Posted on: August 26, 2009 at 12:00 AM
Advertisement
In this tutorial we are going to add another view to the cell(Rows of Table View) and will have two section in Table View and will have Back button on next view so that we can come back to the previous view.For this you have to creat a Navigation Based Application and in that add view.

Table view with multiple view and sections

Project will look like this :

  

In this tutorial we are going to add another view to the cell(Rows of Table View)  and will have two section in Table View and will have Back button on next view so that we can come back to the previous view.For this you have to creat a Navigation Based Application and in that add view.

My project name is TableView,is based on navigation application so it will have two .h and two .m file i.e. TableViewAppDelegate.h, TableViewAppDelegate.m,RootViewController.h and RootViewController.m files. Add another view to this application and name it as DatailViewController. Now you are going to add Table View to your nib file (RootViewController.xib) it already contain navigation bar because application which we created is Navigation Based Application.

Now to enter the data on table view we have to take an array and using that will add the objects to table view.   

RootViewController.h file will look like this:

@interface RootViewController : UITableViewController

{

    NSMutableArray *Items;

}

@end

In viewDidLoad method will initialize the array and then will add the object by using an array, this item is add by using 

[Items addObject:countriesToLiveInDict];

    [Items addObject:countriesLivedInDict];    and the title is set to the navigation bar using self.navigationItem.title =@" Countries ".

    Items = [[NSMutableArray alloc] init];

     NSArray *countriesToLiveInArray = [NSArray arrayWithObjects:@"Pakistan", @"china", @"Srilanka", @"Swizerland", @"New Zealand", @"Green Land", @"Africa", @"Ireland",@"Canada",@"Japan",@"Afganistan", nil];

    NSDictionary *countriesToLiveInDict = [NSDictionary dictionaryWithObject:countriesToLiveInArray forKey:@"Countries"];

    NSArray *countriesLivedInArray = [NSArray arrayWithObjects:@"India", @"U.S.A",@"Nepal", nil];

    NSDictionary *countriesLivedInDict = [NSDictionary dictionaryWithObject:countriesLivedInArray forKey:@"Countries"];

    [Items addObject:countriesToLiveInDict];

    [Items addObject:countriesLivedInDict];   

    // set the title

    self.navigationItem.title =@" Countries ";

}

Here we are selecting the number of section into the table view, number of section is counted by return [Items count].

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return [Items count];

}

Add this to .m file: In this customizing the number of rows in the table view and also the number of rows it should expect should be based on the section.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    NSDictionary *dictionary = [Items objectAtIndex:section];

    NSArray *array = [dictionary objectForKey:@"Countries"];

    return [array count];

}

 Here we are setting the title for the section into the table view

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

    if(section == 0)

      return @" Countries to visit ";

    else

      return @" Countries visited ";

}

To get the selected country and also to initialize the detail view controller and display it

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSDictionary *dictionary = [Items objectAtIndex:indexPath.section];

    NSArray *array = [dictionary objectForKey:@"Countries"];

    NSString *selectedCountry = [array objectAtIndex:indexPath.row];

    DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]];

    dvController.selectedCountry = selectedCountry;

    [self.navigationController pushViewController:dvController animated:YES];

    [dvController release];

    dvController = nil;

}

- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath

{

      return UITableViewCellAccessoryDisclosureIndicator;

}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

{

    [self tableView:tableView didSelectRowAtIndexPath:indexPath];

}

RootViewController.m file will look like this:

#import "RootViewController.h"

#import "TableViewAppDelegate.h"

#import "DetailViewController.h"

@implementation RootViewController

- (void)viewDidLoad

{

    [super viewDidLoad];

    Items = [[NSMutableArray alloc] init];

         NSArray *countriesToLiveInArray = [NSArray arrayWithObjects:@"Pakistan", @"china", @"Srilanka", @"Swizerland", @"New Zealand", @"Green Land", @"Africa", @"Ireland",@"Canada",@"Japan",@"Afganistan", nil];

    NSDictionary *countriesToLiveInDict = [NSDictionary dictionaryWithObject:countriesToLiveInArray forKey:@"Countries"];

    NSArray *countriesLivedInArray = [NSArray arrayWithObjects:@"India", @"U.S.A",@"Nepal", nil];

    NSDictionary *countriesLivedInDict = [NSDictionary dictionaryWithObject:countriesLivedInArray forKey:@"Countries"];

     [Items addObject:countriesToLiveInDict];

    [Items addObject:countriesLivedInDict];   

        self.navigationItem.title =@" Countries ";

}

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

}

- (void)viewDidUnload

{

    // Release anything that can be recreated in viewDidLoad or on demand.

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return [Items count];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

     NSDictionary *dictionary = [Items objectAtIndex:section];

    NSArray *array = [dictionary objectForKey:@"Countries"];

    return [array count];

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

    if(section == 0)

      return @" Countries to visit ";

    else

      return @" Countries visited ";

}

   -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)

    {

        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

    }

    NSDictionary *dictionary = [Items objectAtIndex:indexPath.section];

    NSArray *array = [dictionary objectForKey:@"Countries"];

    NSString *cellValue = [array objectAtIndex:indexPath.row];

    cell.textLabel.text = cellValue;

    return cell;

  }

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSDictionary *dictionary = [Items objectAtIndex:indexPath.section];

    NSArray *array = [dictionary objectForKey:@"Countries"];

    NSString *selectedCountry = [array objectAtIndex:indexPath.row];

    DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]];

    dvController.selectedCountry = selectedCountry;

    [self.navigationController pushViewController:dvController animated:YES];

    [dvController release];

    dvController = nil;

}

- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath

{

      return UITableViewCellAccessoryDisclosureIndicator;

}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

{

    [self tableView:tableView didSelectRowAtIndexPath:indexPath];

}

- (void)dealloc

{

    [Items release];

    [super dealloc];

}

@end

Now will declare a label into the next view and also the string and will declare the property for that. 

DetailViewController.h file will look like this:

#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController

{

    IBOutlet UILabel *label;

    NSString *selectedCountry;

}

@property(nonatomic, retain) NSString *selectedCountry;

@end

In .m file will synthesize the string this is done because we have declared property to the string so we have to synthesize it into the .m file. Implement viewDidLoad to do additional setup after the view, typically from nib file.

DetailViewController.m file will look like this:

#import "DetailViewController.h"

@implementation DetailViewController

@synthesize selectedCountry;

- (void)viewDidLoad

{

    [super viewDidLoad];

    label.text = selectedCountry;

    self.navigationItem.title = @"Selected Country";

}

- (void)didReceiveMemoryWarning {

    // Releases the view if it doesn't have a superview.

    [super didReceiveMemoryWarning];

}

- (void)dealloc

{

    [label release];

    [selectedCountry release];

      [super dealloc];

}

@end

After declaring make appropriate Connection in Interface Builder as below

 

Finally Press Build And Go Button

Download code from here.

 

Advertisement

Related Tags for Table view with multiple view and sections :


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 26, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com