UITableView iPhone Programming Tutorial

December 12, 2008 by admin · 1 Comment
Filed under: Tutorials, Views and View Controllers 

Hi Dears, Here i am not at going to use interface builder.I am going to implent complete application with coding, i hope it make you to become good programmer.

I am assuming that you know the implementation of UITableview with interface builder ,if not please have a look at the iPhone Programming Tutorial - Hello World with UITableView.

Now Please Observe the steps carefully

  • create window-based application
  • Have look at defaults files
  • Modify the code

step-1 : Create window based application.

Xcode—>File—>Create New Project. Give the project name as ” UITBImplement ” .

Step-2 :Observe the default files generated by the xcode . Those files are.

Classes

UITBImplementAppDelegate.h

UITBImplementAppDelegate.m

Other Sources

UITBImplement_Prefix.pch

main.m

Resources

MainWindow.xib

info.plist

Frameworks

UIKit.framework

Foundation.framework

CoreGraphics.framework

Products

UITBImplement.app

So , these are our default files list.

Think here we are not going to use the interface builder ,so  need  not to touch mainwindow.xib file.

So we have only two files to change. i.e UITBImplementAppDelegate.h and UITBImplementAppDelegate.m

So we need TableView right?

UITBImplementAppDelegate.h  file default code is

//
//  UITBImplementAppDelegate.h
//  UITBImplement
//
//  Created by iappdevs on 13/12/08.
//  Copyright
iappdevs 2008. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UITBImplementAppDelegate : NSObject <UIApplicationDelegate>

{
UIWindow *window;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@end

modified code is

_________________________________________________________________________

//
//  UITBImplementAppDelegate.h
//  UITBImplement
//
//  Created by iappdevs on 13/12/08.
//  Copyright iappdevs 2008. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UITBImplementAppDelegate : NSObject <UIApplicationDelegate,UITableViewDataSource,UITableViewDelegate>

{
UIWindow *window;

UITableView *tb;

}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@property (nonatomic, retain) UITableView *tb;
@end

_________________________________________________________________________

TableView means it contains delegate and datasource right?

So we are delegating the table with our code  so we need to extend them as

@interface UITBImplementAppDelegate : NSObject <UIApplicationDelegate,UITableViewDataSource,UITableViewDelegate>

one is for data souce, one is for delegating.

Ok till now we delcared the table view in appdelagate.m , now we have to implement the code about the table view in the appdelegate.m

We are using <UITableViewDataSource> so we need to implement the required methods in our application

@protocol UITableViewDataSource<NSObject>

@required

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

Default UITBImplementAppDelegate.m is

//
//  UITBImplementAppDelegate.m
//  UITBImplement
//
//  Created by srinivas gourishetty on 13/12/08.
//  Copyright iappdevs 2008. All rights reserved.
//

#import “UITBImplementAppDelegate.h”

@implementation UITBImplementAppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(UIApplication *)application {

// Override point for customization after application launch
[window makeKeyAndVisible];
}

- (void)dealloc {
[window release];
[super dealloc];
}

@end

Modified code is

_________________________________________________________________________

//
//  UITBImplementAppDelegate.m
//  UITBImplement
//
//  Created by srinivas gourishetty on 13/12/08.
//  Copyright iappdevs 2008. All rights reserved.
//

#import “UITBImplementAppDelegate.h”

@implementation UITBImplementAppDelegate

@synthesize window,tb;

- (void)applicationDidFinishLaunching:(UIApplication *)application {

tb = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStyleGrouped];
tb.delegate = self;//Becoz We are delegating the TableView , So it is self
tb.dataSource = self;//We only providing data fot tableview so it also self
[window addSubview:tb];//Finally the table view was completed then add this table to present window
[window makeKeyAndVisible];
}

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section

{

return 8;//you can retun ant number;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{//Row means One cell here.

static NSString *MyIdentifier = @”MyIdentifier”;

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}
cell.text = @”My Start Here….”;
return cell;

}

- (void)dealloc {

[tb release];
[window release];
[super dealloc];
}

@end

_________________________________________________________________________

Your out put is

Please let me know if any thing was wrong. and  i am not a good writer try to understand and steps was very easy  and source code

Comments

One Response to “UITableView iPhone Programming Tutorial”

Trackbacks

Check out what others are saying about this post...


Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!