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





Comments
Join the conversation! Your thoughts help the community grow.