UIday1002:UITableView 2 用UITableView实现通讯录cell简单的数据移动、删除、添加功能
2015-09-08 20:25
861 查看
用UITableView实现通讯录cell简单的数据移动、删除、添加功能
作业要求:
使用tableViewController写简单通讯录
dataArray用来存放数据
创建Person类 (name tel)
实现添加 删除 移动
一个分组就行了
左button是 编辑 点编辑可以实现删除和移动
右button是 添加 点添加弹出一个页面 页面上有姓名 电话 弹出的页面左button是取消 右button是完成
以下解决方法中没有创建peison类
AppDelegate.m
RootTableViewController.m
AddViewController.h
AddViewController.m
作业要求:
使用tableViewController写简单通讯录
dataArray用来存放数据
创建Person类 (name tel)
实现添加 删除 移动
一个分组就行了
左button是 编辑 点编辑可以实现删除和移动
右button是 添加 点添加弹出一个页面 页面上有姓名 电话 弹出的页面左button是取消 右button是完成
以下解决方法中没有创建peison类
AppDelegate.m
#import "AppDelegate.h" #import "RootTableViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; RootTableViewController * rtVC = [[RootTableViewController alloc]initWithStyle:(UITableViewStyleGrouped)]; UINavigationController * rootNC = [[UINavigationController alloc]initWithRootViewController:rtVC]; self.window.rootViewController = rootNC; return YES; } @end
RootTableViewController.m
#import "RootTableViewController.h" #include "AddViewController.h" @interface RootTableViewController ()<addViewControllerDelegate> //用来保存编辑样式 { UITableViewCellEditingStyle _editingStyle; } @property (nonatomic,strong) NSMutableArray * dataArray; @property (nonatomic,strong) AddViewController * addVC; @property (nonatomic,strong) NSString * nameAndPhone; @end @implementation RootTableViewController - (void)viewDidLoad { [super viewDidLoad]; // 注册 [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"]; self.dataArray = [NSMutableArray array]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"添加" style:(UIBarButtonItemStyleDone) target:self action:@selector(firstRightAction:)]; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:(UIBarButtonItemStyleDone) target:self action:@selector(firstLeftAction:)]; } //右button响应事件:添加 -(void)firstRightAction:(UIBarButtonItem*)sender{ self.addVC = [[AddViewController alloc] init]; // 设置代理 self.addVC.delegate = self; _editingStyle = UITableViewCellEditingStyleInsert; [self.navigationController pushViewController:_addVC animated:YES]; } //传值响应方法 实现代理方法 -(void)passValue:(NSString *)nameString phone:(NSString *)phoneString{ NSLog(@"name : %@, phone : %@",nameString,phoneString); self.nameAndPhone = [NSString stringWithFormat:@"%@ : %@",nameString,phoneString]; [self.dataArray addObject:_nameAndPhone]; } // 左button响应方法:编辑 -(void)firstLeftAction:(UIBarButtonItem*)sender{ NSLog(@"编辑"); _editingStyle = UITableViewCellEditingStyleDelete; if (self.editing == YES) { self.title = @"完成"; [self setEditing:NO animated:YES]; }else{ self.title = @"编辑"; [self setEditing:YES animated:YES]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Table view data source //分区 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } //每个分区的行数 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; cell.textLabel.text = self.nameAndPhone; return cell; } // Override to support conditional editing of the table view. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { // Return NO if you do not want the specified item to be editable. return YES; } // Override to support editing the table view. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { // Delete the row from the data source // 删除数据源 [self.dataArray removeObjectAtIndex:indexPath.row]; // 删除UI [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; } else if (editingStyle == UITableViewCellEditingStyleInsert) { // 添加数据源 [self.dataArray insertObject:_nameAndPhone atIndex:indexPath.row]; // 添加UI [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle]; // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view } } -(void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{ // 1.保存数据源 id obj = self.dataArray[sourceIndexPath.row]; // 2.删除 [self.dataArray removeObjectAtIndex:sourceIndexPath.row]; // 插入目的位置 [self.dataArray insertObject:obj atIndex:destinationIndexPath.row]; } @end
AddViewController.h
#import <UIKit/UIKit.h> @protocol addViewControllerDelegate <NSObject> -(void)passValue:(NSString * )nameString phone:(NSString * )phoneString; @end @interface AddViewController : UIViewController @property (nonatomic,weak)id<addViewControllerDelegate>delegate; @end
AddViewController.m
#import "AddViewController.h" @interface AddViewController () @property (nonatomic,strong) UITextField * name; @property (nonatomic,strong) UITextField * phone; @end @implementation AddViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor grayColor]; // 两个Label UILabel * l1 = [[UILabel alloc] initWithFrame:CGRectMake(30, 80, 80, 50)]; l1.backgroundColor = [UIColor greenColor]; l1.text = @"姓名"; [self.view addSubview:l1]; UILabel * l2 = [[UILabel alloc] initWithFrame:CGRectMake(30, 140, 80, 50)]; l2.backgroundColor = [UIColor greenColor]; l2.text = @"电话"; [self.view addSubview:l2]; // 两个textField _name = [[UITextField alloc] initWithFrame:CGRectMake(110, 80, 200, 50)]; _name.backgroundColor = [UIColor whiteColor]; [self.view addSubview:_name]; _phone = [[UITextField alloc] initWithFrame:CGRectMake(110, 140, 200, 50)]; _phone.backgroundColor = [UIColor whiteColor]; [self.view addSubview:_phone]; // 左button self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:(UIBarButtonItemStyleDone) target:self action:@selector(secondLeftButton:)]; // 右button self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:(UIBarButtonItemStyleDone) target:self action:@selector(secondRightButton:)]; } //左button响应事件 -(void)secondLeftButton:(UIBarButtonItem*)sender{ [self.navigationController popViewControllerAnimated:YES]; } //右button响应事件 -(void)secondRightButton:(UIBarButtonItem*)sender{ [_delegate passValue:_name.text phone:_phone.text]; [self.navigationController popViewControllerAnimated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end
相关文章推荐
- Uva-12166-Equilibrium Mobile(逆向思维, map)
- [APUE]再读之进程间通信
- tabBarItem setBadgeValue 样式自定义(IOS)
- iOS UIRefreshControl的使用 示例
- LeetCode || N-Queens II
- 把一个数组遍历倒序放到另一个数组中,数组取值是c:out value
- SPOJ 375 QTREE Query on a tree 树链剖分水题
- IOS_UI_Animation
- poj3614 priority queue
- IOS_UI_Block 传值
- EasyUI单击行数据时动态编写editor
- UIImagePickerController 详解
- IOS_UITableViewController 视图控制器的生命周期
- IOS_UITabBarController
- [POJ3061]Subsequence
- UvaLive 4287 Proving Equivalences 强连通缩点
- hibernate.Query查询
- UITextField 使用
- iOS:UITableViewCell自定义单元格
- AbstractQueuedSynchronizer的实现分析(下)