您的位置:首页 > 产品设计 > UI/UE

UI - UITableViewEdit,Contact

2015-10-17 14:00 459 查看
<AppDelegate.m>

#import "AppDelegate.h"
#import "ContactListViewController.h"
@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];

ContactListViewController *contactLVC = [[ContactListViewController alloc]init];
UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:contactLVC];
self.window.rootViewController = naVC;

[contactLVC release];
[naVC release];

return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end


<ContactListViewController.h>

#import <UIKit/UIKit.h>

@interface ContactListViewController : UIViewController

@end


<ContactListViewController.m>

#import "ContactListViewController.h"
#import "Contact.h"
#import "DetailViewController.h"
@interface ContactListViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,retain)UITableView *tableView;
@property (nonatomic,retain)NSMutableDictionary *groupDic;
@property (nonatomic,retain)NSMutableArray *allKeys;
@end

@implementation ContactListViewController

-(void)dealloc
{
self.tableView = nil;
self.groupDic = nil;
self.allKeys = nil;
[super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

//获取数据
[self reLoadData];

//布局 Tableview
[self layoutTableView];

self.navigationItem.title = @"联系人";
self.navigationItem.leftBarButtonItem = self.editButtonItem;//editButtonItem 是系统的item
}

//设置编辑状态    (点击self.editButtonItem触发的方法)且 editing 一直只有0和1两个值在点击时相互变换
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
NSLog(@"%d",editing);
//让 tableview 处于编辑状态
[_tableView setEditing:editing animated:animated];

}

//==================================== 获取数据 =========================================
-(void)reLoadData
{
//获取文件路径
NSString *filePath = [[NSBundle mainBundle ]pathForResource:@"CorrectContact" ofType:@"plist"];
//根据文件路径获取数据
self.groupDic = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
//获取所有 key 值
NSArray *key = [_groupDic allKeys];
//转换为可变数组,并用 allkeys 来接收
self.allKeys = [NSMutableArray arrayWithArray:key];
//排序
[_allKeys sortUsingSelector:@selector(compare:)];

//遍历所有 key 并将数据封装成对象
for (NSString *key in _allKeys) {
//通过 key 获取对应的分组
NSMutableArray *group = [_groupDic valueForKey:key];
//创建对象数组
NSMutableArray *newGroup = [NSMutableArray array];
for (NSDictionary *dic in group) {
//创建对象
Contact *contact = [[Contact alloc]init];
//数据封装
[contact setValuesForKeysWithDictionary:dic];
//将对象装入数组
[newGroup addObject:contact];
}
//将数组装入字典
[_groupDic setValue:newGroup forKey:key];
}

}

//==================================== 布局Tableview =========================================
-(void)layoutTableView
{
self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];

//设置代理和数据源
_tableView.dataSource = self;
_tableView.delegate = self;

[self.view addSubview:_tableView];
[_tableView release];

}

//获取对应分组  (自己写的封装的方法)
-(NSMutableArray *)getGroupWithSection:(NSInteger)section
{
//获取对应分组
NSMutableArray *group = [_groupDic valueForKey:[_allKeys objectAtIndex:section]];
return group;
}

//======================= UITableViewDataSource,UITableViewDelegate =======================
//分区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [_allKeys count];
}
//分区行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//获取对应分组
NSMutableArray *group = [self getGroupWithSection:section];
return [group count];
}
//设置 cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//标识符
static NSString *identifier = @"had";
//从重用队列取
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//判断是否能取到重用队列的 cell
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}

//cell 赋值
//获取对应分组
NSMutableArray *group = [self getGroupWithSection:indexPath.section];
//获取对应联系人
Contact *contact = [group objectAtIndex:indexPath.row];

cell.textLabel.text = contact.name;
cell.detailTextLabel.text = contact.phoneNumber;
cell.imageView.image = [UIImage imageNamed:contact.photoName];

return cell;

}
//行标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [_allKeys objectAtIndex:section];
}
//标题行高
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 50;
}
//行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 40;
}
//索引
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return _allKeys;
}
//点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *detailVC = [[DetailViewController alloc]init];
[self.navigationController pushViewController:detailVC animated:YES];

NSMutableArray *group = [self getGroupWithSection:indexPath.section];
detailVC.contact = [group objectAtIndex:indexPath.row];

}

//======================= cell 的增删编辑 =======================
//编辑
//设置哪一行可编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;  //NO时, cell 编辑不可进入
}

//设置编辑风格
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row % 2 == 0) {
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleInsert;

//    //可以选中任意联系人进行批量操作
//    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
//    //然后可以在设置编辑状态 的方法中 if(!cell){   }来进行编写实现操作

}
//编辑操作
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
//删除操作

//数据的删除
//获取对应的分组
NSMutableArray *group = [self getGroupWithSection:indexPath.section];

//如果分组或者说分区中只有一个联系人时,直接删除整个分区
if ([group count] == 1)
{

//获取对应 key 值
NSString *key  = [_allKeys objectAtIndex:indexPath.section];
//删除分组
[_groupDic removeObjectForKey:key];
//删除key值
[_allKeys removeObjectAtIndex:indexPath.section];

//界面视图的删除
NSIndexSet *set = [NSIndexSet indexSetWithIndex:indexPath.section];
[tableView deleteSections:set withRowAnimation:UITableViewRowAnimationRight];

}else {
//删除对应位置的学生
[group removeObjectAtIndex:indexPath.row];

//界面视图的删除
[tableView deleteRowsAtIndexPaths:@[indexPath]  withRowAnimation:UITableViewRowAnimationRight];

}
}
else if (editingStyle == UITableViewCellEditingStyleInsert)
{
//        //刷新数据
//        [tableView reloadData];

//添加操作
//数据的添加
//获取对应分组
NSMutableArray *group = [self getGroupWithSection:indexPath.section];
//创建对象
Contact *contact = [[Contact alloc] init];
contact.name = @"小明";
contact.sex = @"男";
contact.age = @"16";
contact.phoneNumber = @"1383838385438";
contact.photoName = @"2.png";
[group addObject:contact];
[contact release];

//界面的添加

NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:indexPath.row + [group count] - 1 inSection:indexPath.section];
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationRight];

}
}

//======================= cell 移动的编辑 =======================
//移动
//设置是否可以移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}

//限制跨区域
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
//通过分区判断是否可以跨区域
if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
//如果在同一分区  则返回新的位置
return proposedDestinationIndexPath;
}
//如果不在同一个分区   则返回之前的位置
return sourceIndexPath;
}
//移动操作
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//获取对应分组
NSMutableArray *group = [self getGroupWithSection:sourceIndexPath.section];
//通过下标交换位置
[group exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
//?
}

- (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


<DetailViewController.h>

#import <UIKit/UIKit.h>
#import "Contact.h"
@interface DetailViewController : UIViewController
@property (nonatomic,retain)Contact *contact;//接受上级传入的对象
@end


<DetailViewController.m>

#import "DetailViewController.h"
#import "DetialView.h"
@interface DetailViewController ()

@end

@implementation DetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

//布局 navigationbar
[self customNavigationBar];

// 布局 DetailView
[self layoutDetailView];

}
//======================= 布局navigationbar 和 DetailView =======================

-(void)customNavigationBar
{
self.navigationItem.title = _contact.name;

}
-(void)layoutDetailView
{
DetialView *detailView = [[DetialView alloc]initWithFrame:self.view.bounds];

//赋值
detailView.photoView.image = [UIImage imageNamed:_contact.photoName];
detailView.nameTf.text = _contact.name;
detailView.ageTf.text = _contact.age;
detailView.sexTf.text = _contact.sex;
detailView.phoneTf.text  = _contact.phoneNumber;
detailView.description.text = _contact.description;

[self.view addSubview:detailView];
[detailView release];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end


<Contact.h>

#import <Foundation/Foundation.h>

@interface Contact : NSObject

@property (nonatomic,copy)NSString *name;
@property (nonatomic,copy)NSString *sex;
@property (nonatomic,copy)NSString *age;
@property (nonatomic,copy)NSString *phoneNumber;
@property (nonatomic,copy)NSString *photoName;
@property (nonatomic,copy)NSString *description;

@end


<Contact.m>

#import "Contact.h"

@implementation Contact
-(void)dealloc
{
self.name = nil;
self.age = nil;
self.sex = nil;
self.phoneNumber = nil;
self.photoName = nil;
self.description = nil;
[super dealloc];
}
@end


<DetialView.h>

#import <UIKit/UIKit.h>

@interface DetialView : UIScrollView
@property (nonatomic,retain)UIImageView *photoView;
@property (nonatomic,retain)UITextField *nameTf;
@property (nonatomic,retain)UITextField *sexTf;
@property (nonatomic,retain)UITextField *ageTf;
@property (nonatomic,retain)UITextField *phoneTf;
@property (nonatomic,retain)UITextView *description;
@end


<DetialView.m>

#import "DetialView.h"

//头像
#define kPhotoViewY 10    //从坐标
#define kPhotoViewX 50    //横坐标
#define kPhotoViewWidth 200  //宽度
#define kPhotoViewHight 280  //高度

//名字
#define kNameTfY 300
#define kNameTfX 30
#define kNameTfWidth 160
#define kNameTfHight 30

//性别
#define kSexTfY 350
#define kSexTfX 200
#define kSexTfWidth 80
#define kSexTfHight 30

//年龄
#define kAgeTfY 350
#define kAgeTfX 50
#define kAgeTfWidth 80
#define kAgeTfHight 30

//电话
#define kPhoneTfY 400
#define kPhoneTfX 160
#define kPhoneTfWidth 150
#define kPhoneTfHight 30

@implementation DetialView
-(void)dealloc
{
self.nameTf = nil;
self.sexTf = nil;
self.ageTf = nil;
self.photoView = nil;
self.phoneTf = nil;
self.description = nil;
[super dealloc];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code

//头像
[self layoutPhoto];

//姓名
[self layoutName];

[self layoutAge];

[self layoutSex];

[self layoutPhone];

[self layoutDescription];

}
return self;
}
//======================= 控件布局 =======================

//头像
-(void)layoutPhoto
{
self.photoView = [[UIImageView alloc]initWithFrame:CGRectMake(kPhotoViewX, kPhotoViewY, kPhotoViewWidth, kPhotoViewHight)];
_photoView.layer.cornerRadius = 10;
_photoView.layer.masksToBounds = YES;//如果不设置剪切, UILabel 和 UIimageview 是不可以显示 layer 的圆角的效果的
[self addSubview:_photoView];
[_photoView release];
}
//姓名
-(void)layoutName
{
self.nameTf = [[UITextField alloc]initWithFrame:CGRectMake(kNameTfX, kNameTfY, kNameTfWidth, kNameTfHight)];
_nameTf.enabled = NO;
_nameTf.textAlignment = NSTextAlignmentCenter;
_nameTf.borderStyle = UITextBorderStyleRoundedRect;
_nameTf.placeholder = @"姓名";
_nameTf.backgroundColor = [UIColor yellowColor];
[self addSubview:_nameTf];
[_nameTf release];

}
//年龄
-(void)layoutAge
{
self.ageTf = [[UITextField alloc]initWithFrame:CGRectMake(kAgeTfX, kAgeTfY, kAgeTfWidth, kAgeTfHight)];
_ageTf.enabled = NO;
_ageTf.placeholder = @"年龄";
_ageTf.textAlignment = NSTextAlignmentCenter;
_ageTf.borderStyle = UITextBorderStyleRoundedRect;
_ageTf.backgroundColor = [UIColor orangeColor];
[self addSubview:_ageTf];
[_ageTf release];
}
//性别
-(void)layoutSex
{
self.sexTf = [[UITextField alloc]initWithFrame:CGRectMake(kSexTfX, kSexTfY, kSexTfWidth, kSexTfHight)];
_sexTf.enabled = NO;
_sexTf.placeholder = @"性别";
_sexTf.textAlignment = NSTextAlignmentCenter;
_sexTf.borderStyle = UITextBorderStyleRoundedRect;
_sexTf.backgroundColor = [UIColor lightGrayColor];
[self addSubview:_sexTf];
[_sexTf release];

}
//电话
-(void)layoutPhone
{
self.phoneTf = [[UITextField alloc]initWithFrame:CGRectMake(kPhoneTfX, kPhoneTfY, kPhoneTfWidth, kPhoneTfHight)];
_phoneTf.enabled = NO;
_phoneTf.placeholder = @"电话";
_phoneTf.textAlignment = NSTextAlignmentCenter;
_phoneTf.borderStyle = UITextBorderStyleRoundedRect;
_phoneTf.backgroundColor = [UIColor greenColor];
[self addSubview:_phoneTf];
[_phoneTf release];
}
//描述
-(void)layoutDescription
{
self.description = [[UITextView alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(_phoneTf.frame) + 30, 300, 200)];
_description.editable = NO;   //设置不可编辑
_description.backgroundColor = [UIColor brownColor];
_description.font = [UIFont systemFontOfSize:25];
_description.layer.cornerRadius = 10;

self.contentSize = CGSizeMake(320, CGRectGetMaxY(_description.frame));
[self addSubview:_description];
[_description release];
}

@end


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: