用UITableView实现通讯录cell简单的数据移动、删除、添加功能
作业要求:
使用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