• iOS:NAV+TABLE结合


    功能:点击列表项,用列表字符串作为参数创建一个新视图,新视图默认可以有一个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");
        }
    }
  • 相关阅读:
    BlockingQueue
    序列化存取数据库(spring+mybatis+oracle) 以及可能会遇到的数据库取出的数据反序列化失败问题
    关于junit不抛出异常
    关于ByteArrayInputStream、ByteArrayOutputStream 和 ObjectInputStream、ObjectOutputStream
    sc delete mysql命令执行失败
    python中的值传递和引用传递
    flask实现模仿知乎
    协程和装饰器完成简易计算器
    微信JSAPI支付接口,支付完成后关闭当前窗口
    Java关键字transient和volatile小结
  • 原文地址:https://www.cnblogs.com/code-style/p/3952695.html
Copyright © 2020-2023  润新知