现在的位置: 首页 > 综合 > 正文

iPhone开发入门4 - 创建一个UITable View程序,不使用Interface Builder

2013年03月24日 ⁄ 综合 ⁄ 共 4107字 ⁄ 字号 评论关闭

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 ” TV” .

 

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

 Classes

TVAppDelegate.h

TVAppDelegate.m

 Other Sources

Table_Views_1_Prefix.pch

main.m

 Resources

MainWindow.xib

info.plist

 Frameworks

UIKit.framework

Foundation.framework

CoreGraphics.framework

 Products

TV.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 TVAppDelegate.h and TVAppDelegate.m

So we need TableView right?

TVAppDelegate.h  file default code is

//
//  TVAppDelegate.h
//  TV
//
//  Created by iappdevs on 29/12/08.
//  Copyright Lorvent 2008. All rights reserved.
//
 
#import <UIKit/UIKit.h>
 
@interface TVAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
}
 
@property (nonatomic, retain) IBOutlet UIWindow *window;
 
@end

modified code is

//
// TVAppDelegate.h
// TV
//  Created by iappdevs on 13/12/08.
//  Copyright iappdevs 2008. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface TVAppDelegate : NSObject
{
UIWindow *window;
UITableView *myTable;
NSArray    *theSimpsons;
}
@end

TableView means it contains data source and delegate right?

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

@interface TVAppDelegate : NSObject <UITableViewDataSource>

Ok till now we declared 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
@required
 
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section;
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

Default TVAppDelegate.m code is

//TVAppDelegate.m
// TV
//  Created by srinivas gourishetty on 13/12/08.
//  Copyright iappdevs 2008. All rights reserved.
//
 
#import “TVAppDelegate.h”
@implementation TVAppDelegate
@synthesizewindow;
- (void)applicationDidFinishLaunching:(UIApplication *)application
 {
 
 // Override point for customization after application launch
    [window makeKeyAndVisible];
}
 
- (void)dealloc
{
       [window release];
       [super dealloc];
}
@end

Modified code is

//  TVAppDelegate.m
//  Tasks
//
//  Created by Admin on 30/08/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//
/*
This is the first program in the Table View series.
It demonstrates a minimalist table.
*/
#import &lt;Foundation/Foundation.h&gt;
#import &lt;UIKit/UIKit.h&gt;
#import "TVAppDelegate.h"
@implementation TVAppDelegate
 
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
     window = [[UIWindow alloc] initWithFrame:[[UIScreen  mainScreen] bounds]] ;
  myTable = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame style:UITableViewStylePlain];
theSimpsons = [[NSArray arrayWithObjects:
                          @"Homer Jay Simpson",
                          @"Marjorie "Marge" Simpson",
                          @"Bartholomew "Bart" J. Simpson",
                          @"Lisa Marie Simpson",
                           @"Margaret "Maggie" Simpson",
                          @"Abraham J. Simpson",
                          @"Santa's Little Helper",
                          @"Ned Flanders",
                          @"Apu Nahasapeemapetilon",
                          @"Clancy Wiggum",
                          @"Charles Montgomery Burns",nil] retain];
myTable.dataSource = self;
[window addSubview:myTable];
[window makeKeyAndVisible];
}
 
- (void)dealloc {
[window release];
[myTable release];
[theSimpsons release];
[super dealloc];
}
 
// DataSource methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [theSimpsons count];
}
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"simpsons"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"simpsons"] autorelease];
}
// Set up the cell
cell.text = [theSimpsons objectAtIndex:indexPath.row];
return cell;
}
@end

Your out put ispicture-22

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

抱歉!评论已关闭.