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
- 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
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(
cell.textLabel.text = [NSString stringWithFormat:@"%i", indexPath.row];