Using Comparison Show Data on Tableview in iPhone

Introduction

In this article I will create a Single View application. Here we use two table view controllers and compare them using it's action event. I mean here I use two autor names and a conditional statement to show their corresponding book in table view.

To understand it better we use the following procedure.

Step 1

Open XCode by double-clicking on it.

Step 2

Create a New XCode Project by clicking on it.

Step 3

Now Select Single View Application and click on Next.

Step 4

Now provide your Product Name and Company Identifier.

Step 5

Select the location where you want to save your project and click on Create.

Step 6 

Now here we write code:

TableExampleViewController.h

#import <UIKit/UIKit.h>
@class BookViewController;
@interface TableExampleViewController : UITableViewController
@property (nonatomic, strong) NSArray *authorList;
@property (nonatomic, retain) IBOutlet BookViewController *bookController;
@end

TableExampleViewController.m

#import "TableExampleViewController.h"
#import "BookViewController.h"
@interface TableExampleViewController ()
@end
@implementation TableExampleViewController
@synthesize authorList;
@synthesize bookController;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
}
return self;
}
- (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)viewDidLoad
{
[super viewDidLoad];
[super viewDidLoad];
self.authorList = [[NSArray alloc]
initWithObjects:@"Sachin,V.S. Naipaul",
@"Nisha,Satish Gujral",nil];
self.title = @"Authors";
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.authorList = nil;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.authorList count];
}
- (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];
}
cell.textLabel.text = [self.authorList objectAtIndex: [indexPath row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (0 == indexPath.row)
{
self.bookController.title = @"Sachin";
} else {
self.bookController.title = @"Nisha";
}
[self.navigationController
pushViewController:self.bookController
animated:YES];
}
@end

BookViewController.h

#import <UIKit/UIKit.h>
@interface BookViewController : UITableViewController
{
NSArray *books;
}
@property (nonatomic, strong) NSArray *books;
@end

BookViewController.m

#import "BookViewController.h"
@interface BookViewController ()
@end
@implementation BookViewController
@synthesize books;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidUnload
{
[super viewDidUnload];
self.books = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if ([self.title isEqualToString:@"Sachin"]) {
books = [[NSArray alloc ]
initWithObjects:@"Mystic River",
@"Shutter Island", @"Mera Bharat Mahan",@"A Gender Lens on Social Psychology ",@"A Bend in the rivernil",nil];
} else {
books = [[NSArray alloc ]
initWithObjects:@"The Hunt for Red October",
@"Red Storm Rising",@"A Brush with Life",@"Haunted Night",nil];
}
[super viewWillAppear:animated];
[self.tableView reloadData];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [books count];
}
- (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];
}
cell.textLabel.text = [books objectAtIndex:
[indexPath row]];
return cell;
}
@end

Step 7

Finally we click on the Run button to show the output.

Step 8

Output 1 in iPhone:

Output1-in-iPhone.png

Output 2 in iPhone:

Output2-in-iPhone.png

Output 3 in iPhone:

Output3-in-iPhone.png

Output 4 in iPhone:

Output4-in-iPhone.png

Up Next
    Ebook Download
    View all
    Learn
    View all