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的更多相关文章
随机推荐
- 几种更新(Update语句)查询的方法
正 文: 数据库更新就一种方法Update,其标准格式:Update 表名 set 字段=值 where 条件只是依据数据的来源不同,还是有所差别的: 1.从外部输入这样的比較简单例:update ...
- CloudStack的VO在调用setRemoved方法抛异常的原因
今天在开发中发现一个问题,本来想对一个VO对象的removed值赋值,然后去update一下这条记录,一个最简单的set方法,但是在调用时直接抛异常了. 1: public void setRemov ...
- Nmap 源代码学习四 软件简单使用
软件安装环境是win7.使用Zenmap, nmap6.49BETA2 扫描主机port nmap -T4 -A -v 192.168.0.207 输出结果: 扫描整个子网 nmap 192.168. ...
- Android闹钟【复杂版】
最近做闹钟,所以自己写了个Demo版本,这个程序是用listview单独的类来实现的,和activity类分开来实现的!这个是用数据库进行更新的,当闹钟设置后,闹钟图片变成闹钟的样子,闹钟取消后,图片 ...
- jquery easyui from 表单返回乱码!
如果用easyui的form进行提交,必须在<form>标签中加入属性method="post",即<form method="post"&g ...
- volatile synschonized的区别
在一次面试中,被问到volatile与synschonized的区别,概念模模糊糊,今天做一个总结,加强自己的认识. 本文参考http://www.cnblogs.com/dolphin0520/p/ ...
- Python中的注释(转)
一.单行注释 单行注释以#开头,例如: print 6 #输出6 二.多行注释 (Python的注释只有针对于单行的注释(用#),这是一种变通的方法) 多行注释用三引 ...
- MySQL Spatial Extensions 地理信息
http://dev.mysql.com/doc/refman/5.7/en/gis-data-formats.html http://mysqlserverteam.com/mysql-5-7-an ...
- DNS服务器全面解析--转
引用地址:http://pangge.blog.51cto.com/6013757/1273087 基础认知篇 DNS服务的概述 DNS是Domain Name System 的缩写,即域名系统.DN ...
- shell 学习笔记
<Linux命令行与shell脚本编程大全>笔记 wkss 其他:http://www.cnblogs.com/pengdonglin137/p/3528303.html 一.基本命令 ...