UI1_UITableViewHomeWork
//
// 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的更多相关文章
随机推荐
- 用ASP实现简单的繁简转换
用ASP实现简单的繁简转换 国际化似乎是一个很流行的口号了,一个站点没有英文版至少也要弄个繁体版,毕竟都是汉字,翻译起来不会那么麻烦:P 一般的繁简转换是使用字典,通过GB的内码算出BIG5字符在字典 ...
- Delphi和JAVA用UTF-8编码进行Socket通信例子
最近的项目(Delphi开发),需要经常和java语言开发的系统进行数据交互(Socket通信方式),数据编码约定采用UTF-8编码. 令我无语的是:JAVA系统那边反映说,Delphi发的数据他们收 ...
- IOS之以UIBezierPath绘制饼状图
1.绘制的饼状图是通过多个扇形拼和而成,绘制一个扇形也是比较简单的,核心代码如下: 先画一条圆弧,再画半径,接着再画一条圆弧,最后闭合路径: UIBezierPath* aPath = [[UIBe ...
- CPU 100%
http://www.cnblogs.com/xuehong1985/articles/757060.html
- CentOS 7 ibus 导入第三方词库
CentOS 7 自带的输入法是ibus默认有带拼音输入法“Intelligent Pinyin 1.6.91“,但是在使用过程中发现很多词汇没有.所以就想捣鼓一下,怎么把像搜狗或者其他输入法的数据库 ...
- iOS H5容器的一些探究(二):iOS 下的黑魔法 NSURLProtocol
来源:景铭巴巴 链接:http://www.jianshu.com/p/03ddcfe5ebd7 iOS H5 容器的一些探究(一):UIWebView 和 WKWebView 的比较和选择 一.前言 ...
- ios 把毫秒值转换成日期 NSDate
ios 把毫秒值转换成日期 (比较好用) 1343359790000 这是毫秒值------最佳解决方案-------------------- long long time=134335979000 ...
- ios-UIPickerView基本使用
#import "ViewController.h" @interface ViewController ()<UIPickerViewDataSource,UIPicker ...
- Nodejs新建博客练习(一)安装express并新建项目
安装express npm install -g express-generator 新建工程 express blog //新建项目 cd blog && npm install / ...
- 【python,logging】python中的logging模块
本文章转自kenby的博客,比较全面易懂,转来留作以后使用. http://kenby.iteye.com/blog/1162698 一.从一个使用场景开始 import logging # 创建一个 ...