2015-12-24-UI12_限时练习
//
// AppDelegate.h
// UI12_限时练习
//
// Created by Marry W. on 15/12/22.
// Copyright © 2015年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//
import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (retain, nonatomic) UIWindow *window;
@end
//
// AppDelegate.m
// UI12_限时练习
//
// Created by Marry W. on 15/12/22.
// Copyright © 2015年 www.lanou3g.com 蓝鸥科技. 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 = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];ViewController *rootVC = [[ViewController alloc] init];
UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:rootVC];
self.window.rootViewController = navi;[rootVC release];
[navi 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
// UI12_限时练习
//
// Created by Marry W. on 15/12/22.
// Copyright © 2015年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//
import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//
// ViewController.m
// UI12_限时练习
//
// Created by Marry W. on 15/12/22.
// Copyright © 2015年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//
import "ViewController.h"
import "MyTableViewCell.h"
import "SecondViewController.h"
import "Model.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.frame style:UITableViewStyleGrouped]; _tableView.delegate = self; _tableView.dataSource = self; [self.view addSubview:_tableView]; [_tableView release]; // 数据 [self dataHandler];
}
pragma mark - 数据处理
(void)dataHandler
{
NSArray *arr = @[@"a", @"c", @"d", @"e", @"f", @"s", @"q"];
self.dataArr = [NSMutableArray array];
for (NSString *str in arr) {Model *m = [[Model alloc] init]; // 方式1(属性赋值)
// m.string = str;
// 方式2(KVC) [m setValue:str forKey:@"string"]; [_dataArr addObject:m]; [m release];
}
}
pragma mark - tableView协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _dataArr.count;
} (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {cell = [[[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"] autorelease];
}
cell.myLabel.text = [_dataArr[indexPath.row] string];
return cell;
}- (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath
{
SecondViewController *secVC = [[SecondViewController alloc] init];
secVC.str = [_dataArr[indexPath.row] string];
self.index = indexPath;
secVC.delegate = self;
[self.navigationController pushViewController:secVC animated:YES];
} - (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath
{
return 60;
}
pragma mark - 传值协议方法
(void)passValue:(NSString *)string
{Model *m = [[Model alloc] init]; m.string = string; [self.dataArr replaceObjectAtIndex:_index.row withObject:m]; [_tableView reloadRowsAtIndexPaths:@[_index] withRowAnimation:UITableViewRowAnimationRight];
}
(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.
}
@end
//
// MyTableViewCell.h
// UI12_限时练习
//
// Created by Marry W. on 15/12/22.
// Copyright © 2015年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//
import <UIKit/UIKit.h>
@interface MyTableViewCell : UITableViewCell
@property (nonatomic, retain) UILabel *myLabel;
@end
//
// MyTableViewCell.m
// UI12_限时练习
//
// Created by Marry W. on 15/12/22.
// Copyright © 2015年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//
import "MyTableViewCell.h"
@implementation MyTableViewCell
- (void)dealloc
{
[_myLabel release];
[super dealloc];
} (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {self.myLabel = [[UILabel alloc] init]; _myLabel.backgroundColor = [UIColor yellowColor]; [self.contentView addSubview:_myLabel]; [_myLabel release];
}
return self;
}- (void)layoutSubviews
{
[super layoutSubviews];
_myLabel.frame = CGRectMake(10, 10, self.contentView.bounds.size.width - 20, self.contentView.bounds.size.height - 20);
} - (void)awakeFromNib {
// Initialization code
} - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];// Configure the view for the selected state
}
@end
//
// SecondViewController.h
// UI12_限时练习
//
// Created by Marry W. on 15/12/22.
// Copyright © 2015年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//
import <UIKit/UIKit.h>
@protocol PassDelegate <NSObject>
- (void)passValue:(NSString *)string;
@end
@interface SecondViewController : UIViewController
@property (nonatomic, copy) NSString *str;
@property (nonatomic, assign) id<PassDelegate> delegate;
@end
//
// SecondViewController.m
// UI12_限时练习
//
// Created by Marry W. on 15/12/22.
// Copyright © 2015年 www.lanou3g.com 蓝鸥科技. 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] initWithFrame:CGRectMake(100, 100, 200, 40)]; _textField.backgroundColor = [UIColor yellowColor]; _textField.text = self.str; [self.view addSubview:_textField]; [_textField release]; UIButton *back = [UIButton buttonWithType:UIButtonTypeSystem]; back.backgroundColor = [UIColor orangeColor]; back.frame = CGRectMake(100, 180, 200, 40); [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
//
// Model.h
// UI12_限时练习
//
// Created by Marry W. on 15/12/22.
// Copyright © 2015年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//
import <Foundation/Foundation.h>
@interface Model : NSObject
@property (nonatomic, copy) NSString *string;
@end
//
// Model.m
// UI12_限时练习
//
// Created by Marry W. on 15/12/22.
// Copyright © 2015年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//
import "Model.h"
@implementation Model
@end
当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »