Click here to Skip to main content
15,884,177 members
Articles / Programming Languages / C#

How to Use the iPhone OS UITableView (Video)

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
13 May 2010CPOL1 min read 10.8K  
How to use the iPhone OS UITableView (video)

Post image for How To Use The iPhone OS UITableView (Video)

You see UITableView in all sorts of utility iPhone applications. It is used to display data, lists of items and even to format objects on the touch display. UITableView is one of the things that you can learn that will bring you a ton of leverage in your iPhone app development projects. Today, I am going to show you the basics (scroll down to see the video) of using UITableView.

UITableView in iPhone & iPad Programming

UITableView uses the delegation pattern that is very common through iPhone SDK. Generally, the idea is that you will need an object to act of behalf of your UITableView; we will call this object a delegate. When the system needs to know something, like how many rows are in the table, it will simply “ask” the delegate using a specified callback or delegate method.

Delegation is a really useful skill to master and it is used throughout iPhone development. If you need help with delegation and other iPhone SDK design patterns, check out these resources that will help you. You may also want to check out the tutorial section on this website.

iPhone SDK 3.2 Must Be Installed

You will need to have iPhone SDK 3.2 installed. Also, before we start, make sure to have created a window-based iPhone application.

Step By Step

  • Create Window-based iPhone application
  • Add New UITableViewController Subclass
  • Implement numberOfSectionsInTableView
  • Implement numberOfRowsInSection
  • cellForRowAtIndexPath
  • Import UITableViewController Subclass into your app delegate
  • Create an instance of your UITableViewController Subclass
  • Add your UITableViewController Subclass object to the UIWindow
  • Build and Go

Building UITableView Video

Having trouble seeing the video? Click here to download the mp4 file to your desktop to see the video in your favorite player.

UITableView Source Code

#import "TableViewOne.h"

@implementation TableViewOne

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return 5;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(
    NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = 
            [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text = [NSString stringWithFormat:@"%i", indexPath.row];

    return cell;
}

@end

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --