Table view with multiple view and sections


 

Table view with multiple view and sections

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.

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];

0

}

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

1

{

    NSDictionary *dictionary = [Items objectAtIndex:section];

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

2

    return [array count];

}

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

3

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

{

    if(section == 0)

4

      return @" Countries to visit ";

    else

      return @" Countries visited ";

5

}

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

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

6

{

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

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

7

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

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

    dvController.selectedCountry = selectedCountry;

8

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

    [dvController release];

    dvController = nil;

9

}

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

{

0

      return UITableViewCellAccessoryDisclosureIndicator;

}

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

1

{

    [self tableView:tableView didSelectRowAtIndexPath:indexPath];

}

2

RootViewController.m file will look like this:

#import "RootViewController.h"

#import "TableViewAppDelegate.h"

3

#import "DetailViewController.h"

@implementation RootViewController

- (void)viewDidLoad

4

{

    [super viewDidLoad];

    Items = [[NSMutableArray alloc] init];

5

         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];

6

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

     [Items addObject:countriesToLiveInDict];

    [Items addObject:countriesLivedInDict];   

7

        self.navigationItem.title =@" Countries ";

}

- (void)didReceiveMemoryWarning

8

{

    [super didReceiveMemoryWarning];

}

9

- (void)viewDidUnload

{

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

0

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

1

    return [Items count];

}

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

2

{

     NSDictionary *dictionary = [Items objectAtIndex:section];

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

3

    return [array count];

}

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

4

{

    if(section == 0)

      return @" Countries to visit ";

5

    else

      return @" Countries visited ";

}

6

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

    static NSString *CellIdentifier = @"Cell";

   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

7

    if (cell == nil)

    {

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

8

    }

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

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

9

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

    cell.textLabel.text = cellValue;

    return cell;

0

  }

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

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

1

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

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

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

2

    dvController.selectedCountry = selectedCountry;

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

    [dvController release];

3

    dvController = nil;

}

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

4

{

      return UITableViewCellAccessoryDisclosureIndicator;

}

5

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

{

    [self tableView:tableView didSelectRowAtIndexPath:indexPath];

6

}

- (void)dealloc

{

7

    [Items release];

    [super dealloc];

}

8

@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:

9

#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController

{

0

    IBOutlet UILabel *label;

    NSString *selectedCountry;

}

1

@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.

2

DetailViewController.m file will look like this:

#import "DetailViewController.h"

@implementation DetailViewController

3

@synthesize selectedCountry;

- (void)viewDidLoad

{

4

    [super viewDidLoad];

    label.text = selectedCountry;

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

5

}

- (void)didReceiveMemoryWarning {

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

6

    [super didReceiveMemoryWarning];

}

- (void)dealloc

7

{

    [label release];

    [selectedCountry release];

8

      [super dealloc];

}

@end

9

After declaring make appropriate Connection in Interface Builder as below

 

0

Finally Press Build And Go Button

1

Download code from here.

 

Ads