How To Use TableView In Iphone?

Let’s look at the simplest possible example of a table view to get a feel for how its works. In this example ,we are going to display a list of ┬átext value.

Step 1: Create a View-based application project in Xcode.

Step 2: Double click .xib file to open the file in Interface Builder. Just look in the libarary for aTable View and drag that over to the View window. After dropping the table view onto the view window,single click it to select it,and bring up the connections inspector. You’ll ┬ánotice that the two available connections are there dataSource and delegate. Drag from the circle next to each of those connections over to the File’s Owner icon.After doing that ,save,close and go back to xcode.

Figure 1: The View window after the table view is placed

step 3: All we’re doing here is conforming our class to the two protocols that are needed for it to act as the delegate and datasource for the table view.




Step 4: In the viewDidLoad,we’re simply creating an array of data to pass to the table.




Step 5: We are added here two datasource methods. The first method, tableView:numberOfRowsInSection:,is used by the table to ask how many rows are in a particular section.And this method will be called to get the number of rows in the one section that makes up the list. And the next method, is called by table view when it needs to draw one of its rows.The second argument to this method is an NSIndexPath instance. This is the mechanism that table views use to wrap the section and row into a single object.




Figure 2: Each row of the table is drawn with an indent level higher than the row before it.

Step 6: Now,we’ll just throw up an alert to show that the row was selected. Add the following method in your code.




Figure 3: Alert is display when row is selected.

You can downloaded SourceCode from here TableView