#######通讯录
//
// RootViewController.m
// LessonUITableViewEdit
//
// Created by lanouhn on 15/3/31.
// Copyright (c) 2015年 小代码. All rights reserved.
//
#import "RootViewController.h"
#import "Person.h"
@interface RootViewController () <UITableViewDataSource>
//遇到崩溃的问题
//1.看reason, cmd + f 搜索关键字
//2.有道看翻译, 了解出现了什么问题
//3.把错误的原因(英文), 在百度里搜索
//崩溃没有reason
//1.暴力调试:a.加断点,, 一步一步执行 b.NSlog打印
//2.僵尸调试:
//3.全局异常断点
//一把tableView要显示的数据库写成属性, 保证数据不会过早的释放而影响tableView内容的显示
@property (nonatomic, retain) NSMutableDictionary *personDic;
@property (nonatomic, retain) NSMutableArray *letterArray;
@end
@implementation RootViewController
- (void)dealloc {
[_personDic release];
[_letterArray release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor grayColor];
self.navigationItem.title = @"通讯录";
[self creatView];
[self creatData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//创建视图
- (void)creatView {
UITableView *addressTrableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
addressTrableView.backgroundColor = [UIColor yellowColor];
addressTrableView.rowHeight = 80;
addressTrableView.separatorInset = UIEdgeInsetsMake(10, 50, 10, 50);
addressTrableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
addressTrableView.separatorColor = [UIColor redColor];
addressTrableView.tableFooterView = [[UIView alloc] init];
addressTrableView.dataSource = self;
//由于viewDidLoad内容过多, 可以把具有相同功能的代码, 放到一个方法中
[self.view addSubview:addressTrableView];
[addressTrableView release];
}
//创建数据
- (void)creatData {
self.personDic = [NSMutableDictionary dictionaryWithCapacity:0];
NSArray *nameArray = @[@"辉哥", @"李朋勋", @"张波", @"孙国伟", @"吴朋", @"魏子建", @"杨翔", @"张成行", @"姚成振", @"牛华强", @"王岑", @"江道洪", @"赵鹏飞", @"黄远志", @"刘璐璐", @"段三军", @"王学文", @"张俊飞", @"石圆圆", @"韩文姬", @"汪小芳", @"于良建", @"李珂", @"陈奇", @"徐鹏利", @"刘凯", @"史增良", @"万浩", @"李金帅", @"李蛟龙", @"岳园园", @"杨镇江", @"孔显亮", @"王明稳", @"冯春玉", @"邢进", @"谢勇岳", @"孙锐", @"赵言", @"张建帅", @"张华南", @"王明阳", @"赵伟争", @"陆建荣", @"刘欢", @"李传坤", @"董森森", @"楚高尚", @"申志永", @"段阳"];
for (NSString *tempString in nameArray) {
//创建Person对象
//随机生成电话号码后四位
NSInteger number = arc4random()% 10000;
NSString *phoneNumber = [NSString stringWithFormat:@"1383838%04ld", number];
Person *person = [Person personWithName:tempString phoneNumber:phoneNumber imageName:tempString];
//获取名字首字母
NSString *keyString = [self firstCharactor:tempString];
//去字典中找数组
NSMutableArray *tempArray = [self.personDic objectForKey:keyString];
//判断数组是否存在
if (tempArray == nil) {
NSMutableArray *mArray = [[NSMutableArray alloc] initWithCapacity:0];
[mArray addObject:person];
[self.personDic setObject:mArray forKey:keyString];
} else {
[tempArray addObject:person];
}
}
NSLog(@"%@", self.personDic);
NSArray *array = [self.personDic.allKeys sortedArrayUsingSelector:@selector(compare:)];
//
self.letterArray = [NSMutableArray arrayWithArray:array];
}
//获取拼音首字母
- (NSString *)firstCharactor:(NSString *)aString
{
//转成了可变字符串
NSMutableString *str = [NSMutableString stringWithString:aString];
//先转换为带声调的拼音
CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformMandarinLatin,NO);
NSLog(@"%@", str);
//再转换为不带声调的拼音
CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformStripDiacritics,NO);
NSLog(@"%@", str);
//先传化为拼音
NSString *pinYin = [str capitalizedString];
//获取首字母
return [pinYin substringToIndex:1];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// NSArray *array = [self.personDic.allKeys sortedArrayUsingSelector:@selector(compare:)];
//
// NSMutableArray *persons = [self.personDic objectForKey:array[section]];
// return persons.count;
//1.
// NSMutableArray *person = self.personDic[self.letterArray[section]];
//2.
NSString *key = [self.letterArray objectAtIndex:section];
NSMutableArray *persons = [self.personDic objectForKey:key];
return persons.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIndentifier = @"CELL";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIndentifier];
}
//分区名字
NSString *sectionName = [self.letterArray objectAtIndex:indexPath.section];
//找到某个分区的数组
NSMutableArray *persons = [self.personDic objectForKey:sectionName];
//找到某一行对应的人
Person *person = [persons objectAtIndex:indexPath.row];
//在cell上进行显示
cell.textLabel.text = person.name;
cell.detailTextLabel.text = person.phoneNumber;
cell.imageView.image = [UIImage imageNamed:person.imageName];
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// return self.personDic.allKeys.count;
return self.letterArray.count;
// NSLog(@"%@", self.letterArray);
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return self.letterArray[section];
}
@end
/
########tableViewEdit
//
// HomeViewController.m
// LessonUITableViewEdit
//
// Created by lanouhn on 15/3/31.
// Copyright (c) 2015年 小代码. All rights reserved.
//
#import "HomeViewController.h"
@interface HomeViewController () <UITableViewDataSource, UITableViewDelegate> {
UITableView *catTableView;
BOOL flag;
}
@property (nonatomic, retain)NSMutableArray *catArray;
@end
@implementation HomeViewController
- (void)dealloc {
[_catArray release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor grayColor];
self.navigationItem.title = @"猫猫排行榜";
flag = NO;
catTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
// tableView.rowHeight = 80;
catTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
catTableView.separatorColor = [UIColor redColor];
catTableView.dataSource = self;
catTableView.delegate = self;
self.catArray = [NSMutableArray arrayWithObjects:@"波斯猫", @"加菲猫", @"机器猫", @"龙猫", @"熊猫", @"黑猫警长", @"猫头鹰", @"大脸猫", nil];
[self.view addSubview:catTableView];
[catTableView release];
//编辑(包括添加和删除)
//1.让tableView进入编辑状态
//2.指定那些行可以进行编辑(dataSource中的方法)
//3.指定进入编辑状态的样式(delegate中的方法)
//4.编辑完成(dataSource中的方法)
//移动
//1.让tableView进入编辑状态
//2.指定哪些行可以移动(dataSource中的方法)
//3.移动完成(dataSource中的方法)
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(startEdit:)];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
}
-(void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[catTableView setEditing:editing animated:YES];
}
- (void)startEdit:(UIBarButtonItem *)item {
if (flag) {
item.title = @"编辑";
} else {
item.title = @"完成";
}
flag =!flag;
//进入编辑状态的两个方法
// [tableView setEditing:YES];
[catTableView setEditing:flag animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UITableViewDataSource
//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.catArray.count;
}
//cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIndentifier = @"CELL";
UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:cellIndentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIndentifier] autorelease];
}
cell.textLabel.text = [self.catArray objectAtIndex:indexPath.row];
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
//指定哪些行可以进行编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
return NO;
}
return YES;
}
//编辑完成
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%ld", editingStyle);
//editingStyle用于区分是添加操作还是删除操作
if (editingStyle == UITableViewCellEditingStyleDelete) {
//删除数据
[self.catArray removeObjectAtIndex:indexPath.row];
//tableView删除某些行
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
}
if (editingStyle == UITableViewCellEditingStyleInsert) {
//添加数据
[self.catArray addObject:@"HelloKitty"];
//tableView添加某些行
NSIndexPath *addIndexPath = [NSIndexPath indexPathForRow:self.catArray.count - 1 inSection:indexPath.section];
[tableView insertRowsAtIndexPaths:@[addIndexPath] withRowAnimation:UITableViewRowAnimationTop];
}
}
//哪些可以移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
//移动完成
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
NSLog(@"%ld%ld", sourceIndexPath.row, destinationIndexPath.row);
// [self.catArray exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
//1.保留原来的数据, 记得保留一份
NSString *tempString = [[self.catArray objectAtIndex:sourceIndexPath.row] retain];
//2.删除旧位置上的数据
[self.catArray removeObjectAtIndex:sourceIndexPath.row];
//3.在新的位置上插入数据
[self.catArray insertObject:tempString atIndex:destinationIndexPath.row];
[tempString release];
for (NSString *string in self.catArray) {
NSLog(@"%@", string);
}
}
#pragma mark - UITableViewDelegate
//设置编辑样式(默认样式删除)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == self.catArray.count - 1) {
return UITableViewCellEditingStyleInsert;
}
return UITableViewCellEditingStyleDelete;
}
@end
#######
//限制移动(在同一区域内移动,UITableViewDelegate的方法)
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {
//判断 原来的位置 和 希望移动到的位子 是否在同一个分区
if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
//在同一个分区, 允许移动到期望的位置, 返回proposedDestinationIndexPath
return proposedDestinationIndexPath;
} else {
//不在同一个分区, 不允许夸区域移动, 回到原来的位置, 返回sourceIndexPath
return sourceIndexPath;
}
}