//
// AppDelegate.m
// UI1_UITableViewHomeWork
//
// Created by zhangxueming on 15/7/14.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "AppDelegate.h"
#import "ViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
ViewController *root = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:root];
self.window.rootViewController = nav;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible]; return YES;
}
//
// ViewController.h
// UI1_UITableViewHomeWork
//
// Created by zhangxueming on 15/7/14.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import <UIKit/UIKit.h>
#import "DetailViewController.h" @interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate,sendDetailMessage> @end //
// ViewController.m
// UI1_UITableViewHomeWork
//
// Created by zhangxueming on 15/7/14.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "ViewController.h"
#import "DetailViewController.h" @interface ViewController ()
{
UITableView *_tableView;
NSMutableArray *_dataList;
}
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; [self creatDataSource];
[self creatUI];
} - (void)creatDataSource
{ //@"phone" NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
_dataList = [defaults objectForKey:@"phone"];
if (!_dataList) {
_dataList = [NSMutableArray array];//指向空对象(初始化)
for (int i=0; i<10; i++) {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
NSString *name = [NSString stringWithFormat:@"人物%i", i+1];
NSString *number = [NSString stringWithFormat:@"1852106733%i",arc4random()%10];
[dict setObject:name forKey:@"personName"];
[dict setObject:number forKey:@"phoneNumber"];
[_dataList addObject:dict];
}
}
} - (void)creatUI
{
_tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
self.automaticallyAdjustsScrollViewInsets = YES;
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView]; UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addPerson)];
self.navigationItem.rightBarButtonItem = item;
} - (void)addPerson
{
DetailViewController *dvc = [[DetailViewController alloc] init];
dvc.delegate = self;
dvc.indexPath = nil;
[self presentViewController:dvc animated:YES completion:nil];
//[self.navigationController pushViewController:dvc animated:YES];
} #pragma mark ---sendDetailMessage--- - (void)sendName:(NSString *)name andPhoneNumber:(NSString *)number andIndexPath:(NSIndexPath *)indexPath
{
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:name forKey:@"personName"];
[dict setObject:number forKey:@"phoneNumber"];
if (indexPath) {//修改
[_dataList replaceObjectAtIndex:indexPath.row withObject:dict];
}
else
{//添加
[_dataList addObject:dict];
} NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:_dataList forKey:@"phone"];
[defaults synchronize]; [_tableView reloadData];
} #pragma mark ---UITableViewDataSource--- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_dataList count];
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *reuseIdentifier = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
}
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.textLabel.text = [[_dataList objectAtIndex:indexPath.row] objectForKey:@"personName"];
cell.detailTextLabel.text = [[_dataList objectAtIndex:indexPath.row] objectForKey:@"phoneNumber"]; return cell;
} - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *dvc = [[DetailViewController alloc] init];
dvc.person = [_dataList objectAtIndex:indexPath.row];
dvc.indexPath = indexPath;
dvc.delegate = self;
[self.navigationController pushViewController:dvc animated:YES];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
//
// DetailViewController.h
// UI1_UITableViewHomeWork
//
// Created by zhangxueming on 15/7/14.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import <UIKit/UIKit.h> @protocol sendDetailMessage <NSObject> - (void)sendName:(NSString *)name andPhoneNumber:(NSString *)number andIndexPath:(NSIndexPath *)indexPath; @end @interface DetailViewController : UIViewController
<UITextFieldDelegate> @property (weak, nonatomic) id <sendDetailMessage> delegate;
@property (strong, nonatomic)NSMutableDictionary *person;
@property (strong, nonatomic)NSIndexPath *indexPath; @end //
// DetailViewController.m
// UI1_UITableViewHomeWork
//
// Created by zhangxueming on 15/7/14.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "DetailViewController.h" @interface DetailViewController () @end @implementation DetailViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UITextField *nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 100, self.view.frame.size.width-20,50)];
nameTextField.borderStyle = UITextBorderStyleLine;
[nameTextField becomeFirstResponder];
nameTextField.backgroundColor = [UIColor yellowColor];
nameTextField.delegate = self;
nameTextField.tag = 100;
nameTextField.text = [_person objectForKey:@"personName"]; [self.view addSubview:nameTextField]; UITextField *numberTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, self.view.frame.size.width-20,50)];
numberTextField.borderStyle = UITextBorderStyleLine;
numberTextField.backgroundColor = [UIColor yellowColor];
numberTextField.delegate = self;
numberTextField.tag = 101;
numberTextField.text = [_person objectForKey:@"phoneNumber"];
[self.view addSubview:numberTextField]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(50,300, self.view.frame.size.width-100, 50);
[btn setTitle:@"工具按钮" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
self.view.backgroundColor = [UIColor whiteColor];
} - (void)btnClick
{
UITextField *nameField = (UITextField *)[self.view viewWithTag:100];
UITextField *numberField = (UITextField *)[self.view viewWithTag:101];
if (nameField.text && numberField.text) {
if ([_delegate respondsToSelector:@selector(sendName:andPhoneNumber:andIndexPath:)]) {
[_delegate sendName:nameField.text andPhoneNumber:numberField.text andIndexPath:_indexPath];
}
}
if (_person) {
[self.navigationController popViewControllerAnimated:YES];
}
else{
[self dismissViewControllerAnimated:YES completion:nil];
}
} - (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return 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

UI1_UITableViewHomeWork的更多相关文章

随机推荐

  1. The sound of silence引发的关于互联网以及教育的利弊思考

    “茫茫人海里,人群跟著人群,我们无时无刻不感到孤寂.停下来让我们好好沟通吧,否则人类的关系将日形恶化,沦为新世纪科技的牺牲品” ------- Simon 说实话,我第一次看<毕业生>应该 ...

  2. zoj-3626 Treasure Hunt I (树形dp)

    本文出自   http://blog.csdn.net/shuangde800 题目链接: zoj-3626 题意 给一棵n个节点的树, 节点编号1~n, 每个节点有权值val[i],经过这个节点就可 ...

  3. [MEAN Stack] First API -- 3. Select by ID with Mongoose and Express

    Mongoose allows you to easily select resources by ID from your MongoDB. This is an important aspect ...

  4. Quartz中Cron表达式使用方法

    Quartz中CronTrigger支持日历相关的反复时间间隔(比方每月第一个周一运行),而不是简单的周期时间间隔. 它的调度规则基于 Cron 表达式. 以下就来说一下Cron表达式的规则及使用方法 ...

  5. paip.android 手机输入法制造大法

    paip.android 手机输入法制造大法 作者Attilax ,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/at ...

  6. nginx1.4.6+php5.5.11+mysql5.6.17+mecache+opcache

    要用到的软件:libiconv-1.13.tar.gz libmcrypt-2.5.8.tar.gz mcrypt-2.6.8.tar.gz mhash-0.9.9.9.tar.gz memcache ...

  7. LeetCode3 Longest Substring Without Repeating Characters

    题意: Given a string, find the length of the longest substring without repeating characters. Examples: ...

  8. Android进阶笔记17:3种JSON解析工具(org.json、fastjson、gson)

    一. 目前解析json有三种工具:org.json(Java常用的解析),fastjson(阿里巴巴工程师开发的),Gson(Google官网出的),其中解析速度最快的是Gson. 3种json工具下 ...

  9. Java基础知识强化之IO流笔记73:NIO之 Channel

    1. Java NIO的Channel(通道)类似 Stream(流),但又有些不同: 既可以从通道中读取数据,又可以写数据到通道.但流的读写通常是单向的. 通道可以异步地读写. 通道中的数据总是要先 ...

  10. uva 101 POJ 1208 The Blocks Problem 木块问题 vector模拟

    挺水的模拟题,刚开始题目看错了,poj竟然过了...无奈.uva果断wa了 搞清题目意思后改了一下,过了uva. 题目要求模拟木块移动: 有n(0<n<25)快block,有5种操作: m ...