iOS uiTableViewController use

AppDelegate.h

#import <UIKit/UIKit.h>


@class ViewController;


@interface AppDelegate : UIResponder <UIApplicationDelegate>


@property (strong, nonatomic) UIWindow *window;


@property (strong, nonatomic) ViewController *viewController;


@property (strong,nonatomic) UINavigationController *navigationController;


@end 

 

AppDelegate.m

 @implementation AppDelegate


@synthesize window = _window;

@synthesize viewController = _viewController;

@synthesize navigationController=_navigationController;


- (void)dealloc

{

    [_window release];

    [_viewController release];

    [_navigationController release];

    [super dealloc];

}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    // Override point for customization after application launch.

    ViewController *vc = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];

    

    self.navigationController=[[[UINavigationController alloc] initWithRootViewController:vc]autorelease];

    self.navigationController.title=@"test";

  

    //add shade under navigation bar

    self.navigationController.navigationBar.tintColor=NAVBAR_COLOR;

   

    

    UIImageView *shader=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"top_shader"]];

    shader.frame=CGRectMake(0, 64, self.navigationController.view.frame.size.width, 16);

    shader.autoresizingMask=UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleWidth;

    [self.navigationController.view addSubview:shader];

    [shader release];

    

    self.window.rootViewController = self.navigationController;

    [self.window makeKeyAndVisible];

    

    return YES;

}


3  viewcontroller.h

 

@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>

{

    NSArray *categary;

    UITableView *tableview;

}

@property (nonatomic,retain) NSArray *categary;

@end 

 

4.viewcontroller.m

#import "ViewController.h"


@implementation ViewController

@synthesize categary;


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.

}


#pragma mark - View lifecycle


- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    UIImage *img = [UIImage imageNamed:@"app_bg.png"];

    self.view.backgroundColor=[UIColor colorWithPatternImage:img];

    

    self.navigationItem.title=@"@title";

    self.categary=[NSArray arrayWithObjects:@"aa",@"bb",@"cc", nil];

    tableview=[[UITableView alloc] initWithFrame:self.view.bounds];

    tableview.dataSource=self;

    tableview.delegate=self;

    

    [self.view addSubview:tableview];

}


- (void)viewDidUnload

{

    [super viewDidUnload];

    // Release any retained subviews of the main view.

    // e.g. self.myOutlet = nil;

}


- (void)viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:animated];

}


- (void)viewDidAppear:(BOOL)animated

{

    [super viewDidAppear:animated];

}


- (void)viewWillDisappear:(BOOL)animated

{

[super viewWillDisappear:animated];

}


- (void)viewDidDisappear:(BOOL)animated

{

[super viewDidDisappear:animated];

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    // Return YES for supported orientations

    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

}


//navigate


- (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 [self.categary count];

}


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

{

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    

    // Configure the cell...

    if (cell==nil) {

        cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    }

    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;

    cell.textLabel.text=[self.categary objectAtIndex:indexPath.row];

    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;

    return cell;

}


//点击一个CELL,跳转到下一窗口

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    // Navigation logic may go here. Create and push another view controller.

    /*

     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];

     // ...

     // Pass the selected object to the new view controller.

     [self.navigationController pushViewController:detailViewController animated:YES];

     [detailViewController release];

     */

//    SubViewController *subViewController=[[SubViewController alloc] initWithNibName:@"SubViewController" bundle:nil];

//    

//    [self. 

 

posted @ 2012-08-21 16:02  大树2  阅读(1703)  评论(1编辑  收藏  举报