博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UI1_UITableViewHomeWork
阅读量:4322 次
发布时间:2019-06-06

本文共 8269 字,大约阅读时间需要 27 分钟。

////  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 
#import "DetailViewController.h"@interface ViewController : UIViewController
@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 
@protocol sendDetailMessage
- (void)sendName:(NSString *)name andPhoneNumber:(NSString *)number andIndexPath:(NSIndexPath *)indexPath;@end@interface DetailViewController : UIViewController
@property (weak, nonatomic) id
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

 

转载于:https://www.cnblogs.com/0515offer/p/4645324.html

你可能感兴趣的文章
<P>标签小细节
查看>>
Linux 命令 - netstat
查看>>
安卓模拟器genymotion安装
查看>>
C 语言中包含的标准头文件(24个)
查看>>
移动硬盘启动盘制作
查看>>
mac 关闭&&显示隐藏文件命令
查看>>
Altium Designer 10 导出文件(PDF,gerber,BOM)
查看>>
&spi1 , spi1 = &spi1; status = "okay"
查看>>
mysql备份与还原 数据库的常用命令。
查看>>
完成登录与注册页面的前端
查看>>
NIO学习之Channel
查看>>
两分布间距离的度量:MMD、KL散度、Wasserstein 对比
查看>>
HDU 1300 Pearls (DP)
查看>>
2014年军训总结
查看>>
扩展 -------jQuery
查看>>
Winform跨线程操作最简单的办法
查看>>
[51nod1532]带可选字符的多字符串匹配
查看>>
socket 基于udp实现远程执行命令
查看>>
读取本地json文件,解析json
查看>>
【学习】循环语句1027
查看>>