Click here to Skip to main content
Click here to Skip to main content

iPhone Programming Tutorial – UITableView Hello World

, 5 May 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
iPhone Programming Tutorial – UITableView Hello World

Introduction

In this tutorial, I will cover how to create a simple “HelloWorld” application using table views and navigation bar. This tutorial assumes that you are familiar with Objective-C. You can find a good tutorial for Object-C here.

Create a New Navigation Based Application

Open XCode from Toolbar and select XCode -> New Project option to pop up a new project window. Select iPhone OS and click Navigation-based Application and click choose to enter your project name. I named this project as “HelloWorld”.

XCode and iPhone SDK create few default files for you, let's have a look at them in detail.

image1.JPG

  • HelloWorld.app: This is the application file that contains the app information for installation.
  • HelloWorldAppDelegate.h: It’s a header file that consists of variable definitions that we are going to use in our application. It delegates the information to views or controllers.
  • HelloWorldAppDelegate.m: The object of this class is instantiated by main.m file. Your application loading starts here.
  • HelloWorld_prefix.pch: This is a pre-compiled header file that consists of method definitions used in included libraries. There is no need to include this in every file.
  • Main.m: This is like any other program main file. It instantiates all of our objects and triggers the program. There is no need to edit this file.
  • Info.plist: It’s a Meta file that contains application information. There is no need to edit this file also.
    CoreGraphics, Foundation and UIKit frameworks: Apple iPhone SDK provides set of libraries for iPhone basic framework, UI and Graphics.
  • MainWindow.xib: This is an Interface Builder file, designer mode of the interface of main window.
  • RootViewContrller.h and RootViewController.m: iPhone SDK creates a sample navigation interface for you with a Table View, because most navigation based applications use Table View. These files are controllers for the main window and table view.
  • RootViewController.xib: An interface builder file with a table with rows and columns. Designer file of RootViewController. We use this table to display “HelloWorld” text.

Now you can Build the application and see the simple table on the iPhone simulator / iPhone device (refer to my previous article, HelloWorld_iPhone.aspx).

image5.JPG

This is how the sample table looks like. Now its time to add some fun to it. Let’s add “HelloWorld” text to our Table View. We have to add rows and in the rows text will be placed. Like HTML Table, see the below figure.

image6.JPG

Update UITableView Cells to Display "HelloWorld"

  • RootViewController.m: Open RootViewController.m file which is a controller file attached to the application main view. The functions in this file are overridden from table view class. By editing these table view related functions, we can achieve our goal.
  • Function numberOfRowsInSection: This function is used to declare the number of rows in our table. For our current application, one row is fine. The number next to the return keyword defines the number of rows. Edit the following function like below:
-  (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section 
{
       return 1;
}

Function:cellForRowAtIndexPath

When a row is displayed this function will be called, for each and every row this function call will happen. In this function, we will define the content to be displayed “HelloWorld”.
This function gets called once for every row. This is where we define what content to display in a given row. In this case, we want the row to be a string that says “Hello World”.

-(UITableViewCell *)tableView:(UITableView *)
  tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifer = @“MyIdentifier”;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

if(cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero 
		reuseIdentifier:MyIdentifier] autorelease];
}

[cell setText:@"Hello World"];

return cell;
}

This function creates a new cell object and returns. If the function is calling for the first time, it creates a cell if not it uses an already created cell. Condition if(cell == nil) explains this. Now set the cell text to “HelloWorld” by adding the following line:

[cell setText:@"Hello World"];  

Note: Strings in Object C begins with @ symbol.

We are done with coding. Click Build and Go to see the results.

image7.JPG

This is the basics of using a Table View and a navigation based application. In my next tutorial, you can see how to add some events to cells and show some images based on the cell event.
I hope you have enjoyed my article. I will see you in the next tutorial.

References

History

  • 5th May, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

SureshAddaguduru
Architect articledean.com, conveygreetings.com
United States United States
10+Years of experience in Software Development, Project Management, with proficiency in Development and maintenance
of Mobile Computing, Banking, Commercial, Web –Based and Desktop Applications.
 
Expertise in C#, .NET, MFC, eVc++, VC++ 6.0, VB 6.0, VBA,J2EE with struts framework, Mobile VB 4.0 (App Forge), eVB, VC-COM, TCP/IP socket Programming with ATL, Visual Studo 2005 & 2008, IIS and SQL-Server 2005, Perl and Shell scripting.
 
Expertise in RIM BlackBerry JDE, Good understanding of J2EE struts framework, with Oracle J-Developer, IBM Rational Software Architect, and Macro Vision InstallShield 12.
 
In- depth understanding of C++ TRUESPEECH, PCM, Real Audio, Video Codec’s, Audio Conferencing (VoIP), Video Conferencing in VC++, MFC, OOPS, SDLC, N-Tier Architecture with Client Server Technologies, Web Technologies.
 
Good Experience on ASP.NET, AJAX, VB.NET, Flash, Win32 API, Oracle 8i and SQL-Server 2005.

Comments and Discussions

 
GeneralMy vote of 5 PinmemberJacob Dixon1-Jul-10 7:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141216.1 | Last Updated 5 May 2010
Article Copyright 2010 by SureshAddaguduru
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid