iOS:NAV+TABLE結合

來源:互聯網
上載者:User

標籤:des   style   blog   color   os   io   ar   for   2014   

功能:點擊清單項目,用列表字串作為參數建立一個新視圖,新視圖預設可以有一個BACK按鈕回到上一個視圖

////  main.m//  Hello////  Created by lishujun on 14-8-28.//  Copyright (c) 2014年 lishujun. All rights reserved.//#import <UIKit/UIKit.h>// ------------- 接受參數的視圖控制器-------------@interface TestViewController : UIViewController@end@implementation TestViewController-(TestViewController *)initWithArg:(NSString *)arg{    NSLog(@"%@", arg);    return self;}-(void) loadView{    //建立視圖對象    UIView *contentView = [[UIView alloc]initWithFrame:[[UIScreen mainScreen] applicationFrame]];    contentView.backgroundColor = [UIColor lightGrayColor];    self.view = contentView;}@end// --------------視圖控制器對象--------------@interface HelloWorldViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>{    UITableView *tableView;    NSArray *array;}@end@implementation HelloWorldViewController-(HelloWorldViewController *) init{    array = [[NSArray alloc]initWithObjects:@"oops", @"hello", @"world", nil];    return self;}-(void) loadView{    //建立視圖對象    UIView *contentView = [[UIView alloc]initWithFrame:[[UIScreen mainScreen] applicationFrame]];    contentView.backgroundColor = [UIColor lightGrayColor];    self.view = contentView;        tableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen] applicationFrame]];    [self.view addSubview:tableView];    tableView.dataSource = self;    tableView.delegate = self;}// --------DataSource介面必須實現的方法,用於填充表格------------(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{    return 1; //設定列表分區個數}-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return [array count]; //設定列表長度}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    // 產生列表單元(儘可能複用已有單元)    static NSString *simpleTableIdentifer = @"SimpleTableIdentifer";        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifer];    if (cell == nil) {        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifer];    }    cell.textLabel.text = array[indexPath.row];    return cell;}// ----------實現Delegate介面的方法,用於操作----------------(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    //行選中事件    NSString *rowValue = array[indexPath.row];    [tableView deselectRowAtIndexPath:indexPath animated:YES]; //取消選中狀態    [self.navigationController pushViewController:[[TestViewController alloc]initWithArg:rowValue] animated:YES];}@end// ----------------委派物件--------------------@interface HelloWorldAppDelegate : NSObject <UIApplicationDelegate>{    IBOutlet UIWindow *window;}@property (nonatomic, retain) UIWindow *window;@property (nonatomic, retain) UINavigationController *nav;@end@implementation HelloWorldAppDelegate@synthesize window;@synthesize nav;-(void) applicationDidFinishLaunching:(UIApplication *)application{    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]bounds]];    HelloWorldViewController *viewController = [[HelloWorldViewController alloc]init];        self.nav = [[UINavigationController alloc]initWithRootViewController: viewController];    self.window.rootViewController = self.nav;    [self.window makeKeyAndVisible];}@end// ---------------程式入口---------------------int main(int argc, char * argv[]){    @autoreleasepool {        return UIApplicationMain(argc, argv, nil, @"HelloWorldAppDelegate");    }}

 

iOS:NAV+TABLE結合

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.