Using iPhone TableView for Displaying Data

This is the “TableView” example. There are many ways to display the “TableView” in iPhone. I am going to show you the simplest way to execute the TableView.

Step 1: Open the Xcode and create a new Xcode project using View base application template. Give the application name “TableView”. As shown in the figure below:

Step 2: Expand classes and notice Interface Builder created the Table_ViewViewController.h and Table_ViewViewController.m class for you. Expand Resources and notice the template generated a separate nib, Table_ViewViewController.xib.

Step 3: Open the TableViewViewController.h file and make the following changes in the file.

#import <UIKit/UIKit.h>

@interface Table_ViewViewController : UIViewController {
       
        NSArray *listData;

}
@property(nonatomic,retain) NSArray *listData;
@end

Step 4: Double click the Table_ViewViewController.xib file and after that make the following changes.
A) Open the view window, first drag the table view from the library and place it to the view window and select the table.

B) Connect File’s Owner icon to tableview and select “View”.
Once this is done, save the TableViewViewController.xib file, close it and go back to the Xcode.

Step 5: If you want to add some image in table, then you have to go to Resources folder and add image in resource folder.

Step 6: Open the TableViewViewController.m file and make the following changes in the file.

#import "Table_ViewViewController.h"

@implementation Table_ViewViewController
@synthesize listData;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
(void)viewDidLoad {
        NSArray *array = [[NSArray alloc] initWithObjects:@"Vishal",@"Vinod",@"Sachin",@"Nilesh",@"Balu",@"Amrita",
                                          @"susho",@"Akash",@"Nil",@"Lop",@"Koi",@"Absoulate",@"Dwalin",
                                          @"Fili",@"Kili",@"Oin",@"Gloin",@"Bifur",@"Bofur",@"Bombur",nil];
       
        self.listData = array;
        [array release];
        self.listData= array;
       
    [super viewDidLoad];
}

(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)viewDidUnload {
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
}

(void)dealloc {
    [super dealloc];
}
(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
        return [self.listData count];
}

(UITableViewCell *)tableView:(UITableView *)tableView
                cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                                                         SimpleTableIdentifier];
        if(cell == nil){
                cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
                                                                           reuseIdentifier: SimpleTableIdentifier] autorelease];
        }
        NSUInteger row = [indexPath row];
        cell.textLabel.text = [listData objectAtIndex:row];
        //cell.font=[UIFont boldSystemFontOfSize:20];
        UIImage *image = [UIImage imageNamed:@"macosxlogo.png"];
        cell.image = image;
        return cell;

               
}
@end

Step 7: Now build and run the code and view the Output in the Simulator.

You can download source code from here TableView

Leave a Reply

Your email address will not be published. Required fields are marked *