//
// 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. 一步步学Mybatis-怎么样实现动态SQL查询(6)

    上一章我们已经讲完了关于Mybatis的分页用法,其实MyBatis 还具有的一个强大的特性之一通常是它的动态 SQL 能力. 如果你有使用 JDBC 或其他 相似框架的经验,你就明白要动态的串联 S ...

  2. Spark1.0.0 开发环境高速搭建

          在本系列博客中.为了解析一些概念.解析一些架构.代码測试.搭建了一个实验平台.例如以下图所看到的:       本实验平台是在一台物理机上搭建的.物理机的配置是16G内存,4核8线程CPU ...

  3. centos 服务器配置(三) 之定时任务

    有些liunx系统已经自带定时任务crontab,但是有的新装系统还未安装定时任务,这个时候就需要我们手动对其进行安装. 安装crontab: yum install crontabs 说明: /sb ...

  4. qobject_cast用法

    函数原型: T qobject_cast ( QObject * object ) 本方法返回object向下的转型T,如果转型不成功则返回0,如果传入的object本身就是0则返回0. 在使用时有两 ...

  5. mysql 历史记录查询

    SHOW SLAVE STATUS insert into jy510_admin set userid='123123' mysqlbinlog d:xampp\mysql\data\mysql-b ...

  6. discuz(dz) SSO(单点,同歩,异步)登录 --转

    原文地址:http://fc-lamp.blog.163.com/blog/static/1745666872012762520123/ discuz(dz) SSO(单点,同歩,异步)登录  一般流 ...

  7. c++ 设计模式7 (Bridge 桥模式)

    4.2 Bridge 桥模式 动机: 由于某些类型的固有的实现逻辑,使得它们具有两个变化的维度,乃至多个变化的维度. 代码示例: 实现一个Messager,含有基本功能PlaySound,Connec ...

  8. c#删除转义字符的方法,删除\0后所有字符串(菜鸟级别)

    string str = "78738\01212"; string str_2= Regex.Unescape(str); int index = str_2.IndexOf(& ...

  9. 1.4.9 DocValues

    DocValues 在solr4.2以后,引入了一个令人兴奋的功能,这个功能在lucene存在已经一段时间了,但是还没有在solr中使用. 在某些方面,DocValue 是一种非常有效的索引方式. 为 ...

  10. batchExportPNG.py不是我的代码

    #coding=gbk#@author lifc,20140806#批量制作输出专题图import arcpy,os,time,math #testpath=raw_input("testp ...