Home Tutorial Iphone Examples iphone Table View

 
 

Share on Google+Share on Google+
iphone Table View
Posted on: September 22, 2009 at 12:00 AM
Advertisement
In this tutorial will learn how to use Table View and also how to add object to table view, will add or insert the object into the Table View.

iphone Table View

In this tutorial will learn how to use Table View and also how to add object to table view, will add or insert the object into the Table View. Then will set the number of rows and the component into the Table View, when you click on the object will take you to the next view.Table views are commonly found in iPhone applications, especially productivity applications. They are versatile user-interface objects that you can adapt for different needs. A table presents a scrollable list of items (or rows) that may be divided into sections. Each row may display strings, images, or other identifiers of the represented data item. Users can select a row in a table and, as a result, be presented with another table view that lists items related to the item selected in the first table view.

Will add two ViewController to the Project and the Project is Based on Window Based Application, My project name is iphoneTableView.

Final view will look like this:

Here will create variable for the ViewController and also the property for that and in .m file will synthesize the variable created for the ViewController and that variable will be used in DidFinishLaunching method with the nib file name so that the view will be loaded on the window when you run the program or a project

delegates.h file add this: 

@class iphoneTableViewViewController, TableViewController;

    TableViewController *mtvController;

@property (nonatomic, retain) TableViewController *mtvController;

.m file add this:

#import "TableViewController.h"

@synthesize  mtvController;

- (void)applicationDidFinishLaunching:(UIApplication *)application {

    self.mtvController = [[TableViewController alloc] initWithNibName:@"TableViewController" bundle:[NSBundle mainBundle]];

    [window addSubview:[mtvController view]];

    [window makeKeyAndVisible];

}

In TableViewController will create a class for the other ViewController which we created or added, after that will also create variable for ViewController and also for the Table View And will declare property for all of that, and in .m file will synthesize each variable and then will write the code for the Table view cell by using cel for row at index path method. Here am displaying only the one thing on each cell i.e. Rose India. After that the rows for the section in Table View. After that initializing the ViewController in didSelectRowAtIndexPath, after initializing pass the current row number to the sub view Controller and then add the view as subview to the Sub View Controller.

TableViewController.h add this:

@class SubViewController;

 

    IBOutlet UITableView *tblView;

    SubViewController *smvController;

@property (nonatomic, retain) UITableView *tblView;

@property (nonatomic, retain) SubViewController *smvController;

TableViewController.m add this:

#import "SubViewController.h"

@synthesize tblView, smvController;

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

    static NSString *identifier = @" India";

    UITableViewCell *cell = [self.tblView dequeueReusableCellWithIdentifier:identifier];

    if(cell == nil)

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

    cell.textLabel.text = [[NSString alloc] initWithFormat:@" Rose India : %i", indexPath.row];

    return cell;

}

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

    return 10;

}

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

    if(smvController == nil)

  self.smvController = [[SubViewController alloc] initWithNibName:@"SubViewController" bundle:[NSBundle mainBundle]];

    smvController.row = indexPath.row;

    [self.view addSubview:[smvController view]];

}

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

    return UITableViewCellAccessoryDetailDisclosureButton;

}

In SubViewController will take label to show the message on the next view when the cell is clicked and will take you to the next view, and variable for the viewController and also the property for that in .h file and after doing all this will synthesize it into the .m file so that it will create getter and setter file for that variable and Action for the button by which we will move to previous view , and in that action will write the method for that button so that it will take us to the previous view i.e. table view, and the label is used to show the message on the view.In action will use if statement so that when table is nil then it will move the view to the table view as button is clicked, because the action is for the button change. After all this make the connection from Interface Builder and run the program.

SubViewController.h add this:

@class TableViewController;

    IBOutlet UILabel *Message;

    NSInteger row;

IBOutlet UIButton *mbutton;

    TableViewController *tablle;

@property (nonatomic, readwrite) NSInteger row;

@property (nonatomic, retain) UILabel *Message;

@property (nonatomic, retain) TableViewController *tablle;

 -(IBAction)Change:(id)sender;

SubViewController.m add this:

#import "TableViewController.h"

@synthesize Message, row, tablle;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {

    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {

    }

    return self;

}

- (void)viewDidLoad {

    Message.text = [NSString stringWithFormat:@"Welcome to iphone: %i", row];

}

-(IBAction)Change:(id)sender {

    if(self.tablle == nil)

      tablle = [[TableViewController alloc] initWithNibName:@"TableViewController" bundle:[NSBundle mainBundle]];

    [self.view addSubview:[tablle view]];

}

Make connection as shown below:

Finally press Build And Go Button

Download Here

Advertisement

Related Tags for iphone Table View:


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: September 22, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com