2015-12-24-TableView 数组

//
// AppDelegate.h
// UI13_练习
//
// Created by YIem on 15/12/23.
// Copyright © 2015年 www.yiem.net YIem博客. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder

@property (retain, nonatomic) UIWindow *window;

@end


//
// AppDelegate.m
// UI13_练习
//
// Created by YIem on 15/12/23.
// Copyright © 2015年 www.yiem.net YIem博客. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate

  • (void)dealloc
    {
    [_window release];
    [super dealloc];
    }

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [self.window makeKeyAndVisible];
    ViewController *rooVC = [[ViewController alloc]init];
    UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:rooVC];
    self.window.rootViewController = navi;
    [navi release];
    [rooVC release];

[_window release];
return YES;

}

  • (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

  • (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

  • (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

  • (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

  • (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }

@end



//
// ViewController.h
// UI13_练习
//
// Created by YIem on 15/12/23.
// Copyright © 2015年 www.yiem.net YIem博客. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end


//
// ViewController.m
// UI13_练习
//
// Created by YIem on 15/12/23.
// Copyright © 2015年 www.yiem.net YIem博客. All rights reserved.
//

#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate, PassDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataArr;
@property (nonatomic, retain) NSIndexPath *index;
@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"练习";

    // tableView
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    [_tableView release];

    [self dataHanler];
    }

  • (void)dataHanler
    {
    self.dataArr = [@[@"a", @"b", @"c", @"d", @"e"] mutableCopy];
    }

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    return _dataArr.count;
    }

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"
    ];
    }
    cell.textLabel.text = _dataArr[indexPath.row];
    return cell;
    }

  • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    SecondViewController *secVC = [[SecondViewController alloc]init];
    secVC.str = _dataArr[indexPath.row];
    self.index = indexPath;
    secVC.delegate = self;
    [self.navigationController pushViewController:secVC animated:YES];
    }

  • (void)passValue:(NSString *)string
    {
    [self.dataArr addObject:string];
    [self.tableView reloadData];

}

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

@end



//
// SecondViewController.h
// UI13_练习
//
// Created by YIem on 15/12/23.
// Copyright © 2015年 www.yiem.net YIem博客. All rights reserved.
//

#import <UIKit/UIKit.h>

@protocol PassDelegate

-(void)passValue:(NSString *)string;

@end
@interface SecondViewController : UIViewController
@property (nonatomic, copy) NSString *str;
@property (nonatomic, assign) iddelegate;
@end


//
// SecondViewController.m
// UI13_练习
//
// Created by YIem on 15/12/23.
// Copyright © 2015年 www.yiem.net YIem博客. All rights reserved.
//

#import "SecondViewController.h"

@interface SecondViewController ()
@property (nonatomic, retain)UITextField *textField;
@end

@implementation SecondViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    self.textField = [[UITextField alloc]init];
    _textField.text = self.str;
    _textField.frame = CGRectMake(10, 100, 100, 100);
    _textField.backgroundColor = [UIColor redColor];
    [self.view addSubview:_textField];
    [_textField release];

    // button
    UIButton *back = [UIButton buttonWithType:UIButtonTypeSystem];
    back.backgroundColor = [UIColor yellowColor];
    back.frame = CGRectMake(10, 200, 220, 200);
    [back addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:back];

}

  • (void)back
    {
    [self.delegate passValue:_textField.text];
    [self.navigationController popViewControllerAnimated:YES];
    }
  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

/* #pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

  • (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    }
    */

@end

标签: none

添加新评论

Title - Artist
0:00