iPhone Tutorial1 -UITableView(Navigation Based)

December 26th, 2009 by aabhaanjan

Hi All In this tutorial I would like to discuss about how to create IPHONE Application . I will discuss you to creating a simple “Welcome to IPHONE” application using a UITableView. I am going to show you the simplest way to create IPhone Application .

You will learn how to create navigation based application :-

1.Create a New navigation -based Application

2. Learn About the Default Files

Process to Create Navigation based application :-

1. Open Xcode

Start a new iPhone OS Project
Click Xcode > New Project and a window should pop up like this

Make sure Application is selected under iPhone OS and then select Navigation-Based Application. Click Choose…

Learn About the Default Files

When you will enter the name of your project and click the button .There will be few files that get added to your project. I would like to discribe about the file . What is the functionality of file .So Have a look of each file .

CoreGraphics.framework, Foundation.framwork, UIKit.framework – These are a set of library functions provided by Apple that we use in our application. We use these as includes similar to any other language that includes library functions.

  1. Welcome.app – This is your app that gets installed on the iPhone.
  2. WelCome_Prefix.pch – This is another include file that gets compiled separately from your other files so you don’t need to include it on each file. It contains some code to include the data inside the frameworks.
  3. WelcomeAppDelegate.h – This is a header file that contains all of our definitions for variables that we will be using. It’s very similar to a header file in C or C++;
  4. WelcomeAppDelegate.m – All of the magic starts here. Consider this file our starting point for execution. The main.m file invokes this object.
  5. Info.plist – This contains various meta information about your program.Even you can add main image in plist file.
  6. main.m – Like most programming language, this file contains our main function. This is where execution begins. The main function basically instantiates our object and starts the program. You shouldn’t need to edit this file.
  7. MainWindow.xib – This contains the visual information of our main window. If you double click on it, it will open in a program called “Interface Builder”.
  8. RootViewController.h, RootViewController.m – These are files for a view controller that gets added to our main window. Basically, Apple has already created a simple interface when you clicked on Navigation-Based Application. Since most navigation-based applications use a Table View, Apple has provided it for us to use.
  9. RootViewController.xib – This is a view that Apple has provided that emulates a table. It has rows and columns. We will be displaying our “Welcome” text inside one of these rows

Now, all of these files together create a basic program.When you click Go ahead and click on the Build and Go button ,It display the bellow

Update the UITableView Cells to Display “WelCome” Text

Let’s write some code

Start by opening RootViewController.m. This is the view controller that Apple added to our main view. All of the functions you see already created in here are functions that have been overridden from the Table View super class. Since we are editing a table, all of these functions will be related to editing a table. So find the function called numberOfRowsInSection.

This function tells the application how many rows are in our table. Currently, it returns 0. Let’s change that to return 1.

Now go down to the function called cellForRowAtIndexPath. 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 “WelCome to IPhone”.

What this function is doing is creating a new cell object and returning it. The code between the i(cell==null) block checks to see if we have created a cell before, if not build a new cell, otherwise use the one we created before. This helps in performance so we don’t need to build new cells each time we visit this function. So right before the // Set up the cell comment add the following code:

[cell setText:@”Welcome To IPhone”];

We are basically calling the setText method of the cell object and pass in the string “WelCome to IPhone”. Here string begins with @ sign .

Click the Build and Go button again to launch the iPhone simulator. You should see screen look like this and youe content “welcome to iphone” will display on the screen:

If you have any question and doubt fell free to let me know .

Tags: , , , , , , ,