#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (void)dealloc{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
RootViewController *rootVC = [[RootViewController alloc] init];
rootVC.title = @"标题";
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootVC];
self.window.rootViewController = navigationController;
[navigationController release];
[rootVC release];
return YES;
}
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController
@end
#import "RootViewController.h"
@interface RootViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) NSMutableDictionary *studentDictionary;
@property (nonatomic, retain) NSMutableArray *keyArray;
@end
@implementation RootViewController
- (void)dealloc{
[_studentDictionary release];
[_keyArray release];
[super dealloc];
}
#pragma mark-- tableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [_keyArray count];
}
- (NSUInteger)numberOfGroupInSection:(NSInteger)section{
return [_studentDictionary[_keyArray[section]] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self numberOfGroupInSection:section];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return _keyArray[section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return _keyArray;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier] autorelease];
}
NSDictionary *student = _studentDictionary[_keyArray[indexPath.section]][indexPath.row];
NSString *name = [student objectForKey:@"name"];
cell.textLabel.text = name;
NSString *phoneNumber = [student objectForKey:@"phoneNumber"];
cell.detailTextLabel.text = phoneNumber;
return cell;
}
#pragma mark-- 编辑tableView
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
[super setEditing:editing animated:animated];
[(UITableView *)self.view setEditing:editing animated:animated];
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
NSDictionary *studentInfo = _studentDictionary[_keyArray[sourceIndexPath.section]][sourceIndexPath.row];
[studentInfo retain];
[_studentDictionary[_keyArray[sourceIndexPath.section]] removeObject:studentInfo];
[_studentDictionary[_keyArray[destinationIndexPath.section]] insertObject:studentInfo atIndex:destinationIndexPath.row];
[studentInfo release];
[tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
}
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{
if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
return proposedDestinationIndexPath;
}else{
return sourceIndexPath;
}
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 0) {
return UITableViewCellEditingStyleInsert;
}else{
return UITableViewCellEditingStyleDelete;
}
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView beginUpdates];
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSMutableArray *studentArray = _studentDictionary[_keyArray[indexPath.section]];
if (studentArray.count == 1) {
NSString *groupName = _keyArray[indexPath.section];
[_studentDictionary removeObjectForKey:groupName];
[_keyArray removeObject:groupName];
[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:(UITableViewRowAnimationTop)];
}else{
NSDictionary *studentInfo = studentArray[indexPath.row];
[studentArray removeObject:studentInfo];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];
}
}else{
NSDictionary *newStudent = @{@"name" : @"bilityniu", @"phoneNumber" : @"123456"};
NSMutableArray *studentArray = _studentDictionary[@"B"];
[studentArray addObject:newStudent];
NSUInteger indexPathForSection = [_keyArray indexOfObject:@"B"];
NSUInteger indexPathForRow = [studentArray indexOfObject:newStudent];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:indexPathForRow inSection:indexPathForSection];
[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];
}
[tableView endUpdates];
}
#pragma mark-- 重写loadView
- (void)loadView{
UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];
tableView.rowHeight = 110;
tableView.delegate = self;
tableView.dataSource = self;
self.view = tableView;
[tableView release];
}
#pragma mark-- viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"Class25ContactList" ofType:@"plist"];
NSMutableDictionary *contactDic = [NSMutableDictionary dictionaryWithContentsOfFile:filepath];
self.studentDictionary = contactDic;
NSLog(@"%@", _studentDictionary);
_keyArray = [[[_studentDictionary allKeys] sortedArrayUsingSelector:@selector(compare:)] mutableCopy];
NSLog(@"%@", _keyArray);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end