iOS分组排序
#import "TongXunLu.h"
#import "Student.h"
#import "UpdataVC.h"
#import "AddVC1.h"
#import "pinyin.h"
#import "ChineseString.h"
#import "ZhongWenPaiXu.h"
#import "PaiTou.h"
#import "paiTou1.h"
@implementation TongXunLu
@synthesize stuArray,stu,tableV,stu1,tempA,tempB,dictDict1;
- (void)viewDidLoad
{
[superviewDidLoad];
self.title=@"漂亮通讯录";
//初始化左右按钮
self.navigationItem.leftBarButtonItem=[selfeditButtonItem];
self.navigationItem.rightBarButtonItem=[[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAddtarget:selfaction:@selector(addStu1:)];
//初始化表视图。
tableV=[[UITableViewalloc]initWithFrame:CGRectMake(0, 0, 320, 416)];
self.tableV.delegate=self;
self.tableV.dataSource=self;
[self.view addSubview:tableV];
[tableV release];
//获取学生字典信息。
self.stuArray=[StudentfindAll];
self.tempA=[[NSMutableArrayalloc]init];
ChineseString *tempString1;
NSString *sss;
Student *student1;
for (int i=0; i<[[ZhongWenPaiXu zhongWenPaiXu:stuArray] count]; i++) {
tempString1=[[ZhongWenPaiXu zhongWenPaiXu:stuArray]objectAtIndex:i];
sss=tempString1.string;
student1=[Student findByName:sss];
// NSLog(@"sss====%@",sss);
// NSLog(@"sss====%@",student1.name);
[tempA addObject:student1];
}
NSLog(@"[[tempA objectAtIndex:0] name]====%@",[[tempAobjectAtIndex:0] name]);
// [self.stuArray removeAllObjects];
//
// self.stuArray = tempA;
self.tempB=[paiTou1zhongWenPaiXu:stuArray];
self.dictDict1=[PaiTouzhongWenPaiXu:stuArray];
NSLog(@"tempB==%@",tempB);
}
-(void)addStu1:(id)sender{
AddVC1 *addVC=[[AddVC1 alloc]init];
[self.navigationControllerpushViewController:addVC animated:YES];
}
#pragma mark=====uitableViewDelegate,uitableViewDataSource代理方法======
//点击编译按钮时表格的编译样式。
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return tempB;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
returnUITableViewCellEditingStyleDelete;
}
//设置编译开始。
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
[super setEditing:editing animated:animated];
[self.tableV setEditing:editing animated:animated];
}
//设置section的title
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [tempB objectAtIndex:section];
}
//设置section的数量。
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [tempB count];
}
//设置每个section的行数。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
// return [stuArray count];
// return [tempB objectAtIndex:section];
return [[dictDict1objectForKey:[tempBobjectAtIndex:section]] count];
}
//显示每个单元格的信息。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIndentify=@"cell";//重用标示符。
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIndentify];//根据标示符重用cell。
if (cell==nil) {//如果没有可以重用的cell我们就新建一个。
cell=[[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:cellIndentify]autorelease];
}
//每一行的学生。
// stu=[tempA objectAtIndex:(indexPath.section+indexPath.row)];
NSString *ssss=[tempB objectAtIndex:indexPath.section];
NSMutableArray *ssss1=[dictDict1 objectForKey:ssss];
stu=[Student findByName:[ssss1 objectAtIndex:indexPath.row]];
cell.textLabel.text=stu.name;
cell.detailTextLabel.text=stu.tel;
cell.imageView.image=[UIImageimageWithData:stu.image];
return cell;
}
//点击单元格的方法。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UpdataVC *editVC=[[UpdataVC alloc]init];
[self.navigationControllerpushViewController:editVC animated:YES];
//每一行的学生信息。
stu=[tempA objectAtIndex:indexPath.row];
editVC.name.text=stu.name;
editVC.detailText.text=stu.detailText;
editVC.tel.text=stu.tel;
editVC.image.image=[UIImage imageWithData:stu.image];
editVC.sid.text=[NSString stringWithFormat:@"%d",stu.sid];
// [editVC release];//这里不能释放,不知道为什么,因为下个视图还要pop返回一下,这个操作也相当与release所以这里如果不对下个视图返回的话就可以大胆的release了。否则一定不能release。
}
//确定表格可以编译的行。
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
returnYES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
stu=[tempA objectAtIndex:indexPath.row];
//下面是删除的顺序,顺序不能错。
[Student deleteBySid:stu.sid];
[stuArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
if (editingStyle == UITableViewCellEditingStyleInsert) {//这个是添加的方法。这里没有用也没有添加数据库。
NSArray *insertIndexPaths=[NSArray arrayWithObjects:indexPath, nil];
[tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight];
}
}
//移动表格是方法。
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
NSUInteger fromRow=[fromIndexPath row];
NSUInteger toRow=[toIndexPath row];
id object =[tempA objectAtIndex:fromRow];
[tempAremoveObjectAtIndex:fromRow];
[tempA insertObject:object atIndex:toRow];
}
//这个方法就是确定他能执行移动操作的
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
returnYES;
}
-(void)viewWillAppear:(BOOL)animated{
self.stuArray=[StudentfindAll];
[self.tableVreloadData];
self.tempA=[[NSMutableArrayalloc]init];
ChineseString *tempString1;
NSString *sss;
Student *student1;
for (int i=0; i<[[ZhongWenPaiXu zhongWenPaiXu:stuArray] count]; i++) {
tempString1=[[ZhongWenPaiXu zhongWenPaiXu:stuArray]objectAtIndex:i];
sss=tempString1.string;
student1=[Student findByName:sss];
// NSLog(@"sss====%@",sss);
// NSLog(@"sss====%@",student1.name);
[tempA addObject:student1];
}
// NSLog(@"[[tempA objectAtIndex:0] name]====%@",[[tempA objectAtIndex:0] name]);
}
//
// PaiTou.m
// tongXunLu
//
// Created by Ibokan on 12-10-14.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import "PaiTou.h"
#import "ZhongWenPaiXu.h"
#import "ChineseString.h"
#import "pinyin.h"
@implementation PaiTou
+(NSMutableDictionary *)zhongWenPaiXu:(NSMutableArray *)newArray{
//中文排序。
NSMutableArray *chineseStringsArray=[NSMutableArray array];
NSMutableDictionary *dictDict=[[NSMutableDictionaryalloc]init];
for(int i=0;i<[newArray count];i++){
ChineseString *chineseString=[[ChineseString alloc]init];
chineseString.string=[NSString stringWithString:[[newArray objectAtIndex:i]name] ];
if(chineseString.string==nil){
chineseString.string=@"";
}
if(![chineseString.string isEqualToString:@""]){
NSString *pinYinResult=[NSString string];
for(int j=0;j<chineseString.string.length;j++){
NSString *singlePinyinLetter=[[NSString stringWithFormat:@"%c",pinyinFirstLetter([chineseString.string characterAtIndex:j])]uppercaseString];
pinYinResult=[pinYinResult stringByAppendingString:singlePinyinLetter]; }
chineseString.pinYin=pinYinResult;
}else{
chineseString.pinYin=@"";
}
[chineseStringsArray addObject:chineseString];
}
//Step2输出
// NSLog(@"\n\n\n转换为拼音首字母后的NSString数组");
// for(int i=0;i<[chineseStringsArray count];i++){
// ChineseString *chineseString=[chineseStringsArray objectAtIndex:i];
// NSLog(@"原String:%@----拼音首字母String:%@",chineseString.string,chineseString.pinYin);
// }
//Step3:按照拼音首字母对这些Strings进行排序
NSArray *sortDescriptors = [NSArrayarrayWithObject:[NSSortDescriptorsortDescriptorWithKey:@"pinYin"ascending:YES]];
[chineseStringsArray sortUsingDescriptors:sortDescriptors];
//Step3输出
NSLog(@"\n\n\n按照拼音首字母后的NSString数组");
NSMutableSet *set=[NSMutableSetset];
for(int i=0;i<[chineseStringsArray count];i++){
ChineseString *chineseString=[chineseStringsArray objectAtIndex:i];
NSLog(@"原String:%@----拼音首字母String:%@",chineseString.string,chineseString.pinYin);
NSLog(@"排好的名字%@----名字的首字母String:%@",chineseString.string,[chineseString.pinYin substringToIndex:1]);
NSString *temps=[chineseString.pinYin substringToIndex:1];
NSLog(@"temps==%@",temps);
[set addObject:temps];
NSLog(@"set count ===%d set====%@",[set count],[set anyObject]);
}
NSLog(@"set count ===%d set====%@",[set count],[set allObjects]);
NSMutableArray *tempArray2=[NSMutableArray arrayWithArray:[set allObjects]];
NSLog(@"tempArray2==%@",tempArray2);
for (int m=0; m<[tempArray2 count]; m++) {
NSMutableArray *arrayLast=[[NSMutableArrayalloc]init];
for (int k=0; k<[chineseStringsArray count]; k++) {
ChineseString *chineseString=[chineseStringsArray objectAtIndex:k];
NSString *temps=[chineseString.pinYin substringToIndex:1];
if ([[tempArray2 objectAtIndex:m] isEqualToString:temps]) {
[arrayLast addObject:chineseString.string];//相同首字母的数组添加名字。
[dictDict setValue:arrayLast forKey:temps];
}
NSLog(@"dictDict count==%d,,,,%@",[dictDict count],[[dictDict objectForKey:temps]objectAtIndex:0] );
}
}
return dictDict;
}
@end
//
// PaiTou.m
// tongXunLu
//
// Created by Ibokan on 12-10-14.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import "paiTou1.h"
#import "ZhongWenPaiXu.h"
#import "ChineseString.h"
#import "pinyin.h"
@implementation paiTou1
+(NSMutableArray *)zhongWenPaiXu:(NSMutableArray *)newArray{
//中文排序。
NSMutableArray *chineseStringsArray=[NSMutableArray array];
NSMutableDictionary *dictDict=[[NSMutableDictionaryalloc]init];
for(int i=0;i<[newArray count];i++){
ChineseString *chineseString=[[ChineseString alloc]init];
chineseString.string=[NSString stringWithString:[[newArray objectAtIndex:i]name] ];
if(chineseString.string==nil){
chineseString.string=@"";
}
if(![chineseString.string isEqualToString:@""]){
NSString *pinYinResult=[NSString string];
for(int j=0;j<chineseString.string.length;j++){
NSString *singlePinyinLetter=[[NSString stringWithFormat:@"%c",pinyinFirstLetter([chineseString.string characterAtIndex:j])]uppercaseString];
pinYinResult=[pinYinResult stringByAppendingString:singlePinyinLetter]; }
chineseString.pinYin=pinYinResult;
}else{
chineseString.pinYin=@"";
}
[chineseStringsArray addObject:chineseString];
}
//Step2输出
// NSLog(@"\n\n\n转换为拼音首字母后的NSString数组");
// for(int i=0;i<[chineseStringsArray count];i++){
// ChineseString *chineseString=[chineseStringsArray objectAtIndex:i];
// NSLog(@"原String:%@----拼音首字母String:%@",chineseString.string,chineseString.pinYin);
// }
//Step3:按照拼音首字母对这些Strings进行排序
NSArray *sortDescriptors = [NSArrayarrayWithObject:[NSSortDescriptorsortDescriptorWithKey:@"pinYin"ascending:YES]];
[chineseStringsArray sortUsingDescriptors:sortDescriptors];
//Step3输出
NSLog(@"\n\n\n按照拼音首字母后的NSString数组");
NSMutableSet *set=[NSMutableSetset];
for(int i=0;i<[chineseStringsArray count];i++){
ChineseString *chineseString=[chineseStringsArray objectAtIndex:i];
NSLog(@"原String:%@----拼音首字母String:%@",chineseString.string,chineseString.pinYin);
NSLog(@"排好的名字%@----名字的首字母String:%@",chineseString.string,[chineseString.pinYin substringToIndex:1]);
NSString *temps=[chineseString.pinYin substringToIndex:1];
NSLog(@"temps==%@",temps);
[set addObject:temps];
NSLog(@"set count ===%d set====%@",[set count],[set anyObject]);
}
NSLog(@"set count ===%d set====%@",[set count],[set allObjects]);
NSMutableArray *tempArray2=[NSMutableArray arrayWithArray:[set allObjects]];
NSLog(@"tempArray2==%@",tempArray2);
for (int k=0; k<[chineseStringsArray count]; k++) {
ChineseString *chineseString=[chineseStringsArray objectAtIndex:k];
NSString *temps=[chineseString.pinYin substringToIndex:1];
for (int m=0; m<[tempArray2 count]; m++) {
NSMutableArray *arrayLast=[[NSMutableArray alloc]init];
if ([[tempArray2 objectAtIndex:m] isEqualToString:temps]) {
[arrayLast addObject:chineseString.string];//相同首字母的数组添加名字。
[dictDict setValue:arrayLast forKey:temps];
}
}
NSLog(@"dictDict count==%d,,,,%@",[dictDict count],[dictDict objectForKey:temps] );
}
for (int i=0; i<[tempArray2 count]; i++) {
for (int j=i; j<[tempArray2 count]-1; j++) {
if ([[tempArray2 objectAtIndex:j] compare: [tempArray2 objectAtIndex:j+1]]==1) {
[tempArray2 addObject:[tempArray2 objectAtIndex:j]];
[tempArray2 removeObjectAtIndex:j];
}
}
}
return tempArray2;
}
@end