UI1_UITableViewHomeWork

时间:2024-12-19 11:34:32
//
// 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