IOS_UI_UITableView 应用 通讯录(从前往后传值不编辑)

#import <UIKit/UIKit.h>


@interface AppDelegate : UIResponder <UIApplicationDelegate>


@property (strong, nonatomic) UIWindow *window;



@end

#import "AppDelegate.h"

#import "MainViewController.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]];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    

    MainViewController *main = [[MainViewController alloc]init];

    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:main];

    self.window.rootViewController = navi;

    [navi release];

    [main release ];

    

    

    return YES;

}


#import <UIKit/UIKit.h>


@interface MainViewController : UIViewController


@end

#import "MainViewController.h"

#import "SecondViewController.h"

//2.4

#import "Student.h"

#import "NSString+Characters.h"

//4.签协议

@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate,SecondVCDelegate>

@property (nonatomic,retain) UITableView *tableView;

//2.2 .所有的学生数据都归类到这个字典中,首字母为key,对应值是数组

@property (nonatomic,retain) NSMutableDictionary *contactDic;

//4.装首字母的数组,排序

@property (nonatomic,retain) NSMutableArray *firstLArr;

//7.记录点击的cell的位置

@property(nonatomic,retain)NSIndexPath *indexPath;

@end


@implementation MainViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.title = @"通讯录";

    //3.调用方法处理数据

    //好处: 方便调试,可以随时改变一段代码的执行位置,方便反复调用

    [self handleData];

    

    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];

    [self.view addSubview:self.tableView];

    [self.tableView release];

    self.tableView.dataSource = self;

    self.tableView.delegate = self;

    

    //1.先建一个student的类

    

    

    

    

//    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addMassageAction:)] autorelease];

}

//2.写一个小方法处理数据

//数组获取字典,创建学生字典,提首字母,根据首字母在字典中获取数组,第一次取没有就创建一个数组然后把对象添加到数组里,获取对象,获取首字母,创建对象,把首字母相同的归为同一数组,

- (void)handleData

{

    //处理数据的自定义方法

    //2.1 读取数据

    NSString *path = [[NSBundle mainBundle]pathForResource:@"student" ofType:@"plist"];

    //装有字典的数组

    NSMutableArray *arr = [NSMutableArray arrayWithContentsOfFile:path];

    //2.3 初始化

    self.contactDic = [NSMutableDictionary dictionary];

    //2.4遍历数组,归类所有数据

    for (NSDictionary *dic in arr) {

        //创建student对象 先引头文件

        Student *stu = [Student studentWithDic:dic];

        //获取stu的首字母

        NSString *firstLetter = [stu.name firstCharacterOfName];

        //获取首字母在字典中的对应数据

        NSMutableArray *stuArr = [self.contactDic objectForKey:firstLetter];

        if (!stuArr ) {

            //如果取出的数组为nil,说明字典中没有这个key对应的值,就创建一个

            stuArr = [NSMutableArray array];

            //将新创建的数组返回去赋给数组

            [self.contactDic setObject:stuArr forKey:firstLetter];

            

                 }

    //创建stu对象添加到数组里

    [stuArr addObject:stu];

        [self.contactDic setObject:stuArr forKey:firstLetter];

    }

    NSLog(@"%@",self.contactDic);

    //4.1

    self.firstLArr = [NSMutableArray arrayWithArray:[self.contactDic allKeys]];

    //4.2 排序

    [self.firstLArr sortUsingSelector:@selector(compare:)];

//    NSLog(@"%@",self.firstLArr);

    

}

#pragma mark- tableView协议方法

//5.分区协议方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return _contactDic.count;

}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    //每个分区里面有多少行

    NSString *key = [self.firstLArr objectAtIndex:section];

    NSMutableArray *stuArr = [self.contactDic objectForKey:key];

    return stuArr.count;

}

//6.每个分区的标题

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

    //提出首字母 每个分区的顶部标题

    return [self.firstLArr objectAtIndex:section];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *str = @"reuse";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];

    if (!cell) {

        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str] autorelease];

    }

    //7

    NSString *key = [self.firstLArr objectAtIndex:indexPath.section];

    NSMutableArray *stuArr = [self.contactDic objectForKey:key];

    Student *stu = [stuArr objectAtIndex:indexPath.row];

    cell.textLabel.text = stu.name;

    cell.detailTextLabel.text = stu.age;

     cell.imageView.image = [UIImage imageNamed:@"/Users/dllo/Desktop/UI/UI_exercise/UI_exercise/UI_exercise7/UI_zuoye/UI_zuoye/a8773912b31bb0518c7b120e347adab44aede0bd.jpg"];

    return cell;

}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    SecondViewController *secondVC = [[SecondViewController alloc]init];

    //2

    //推出详情页面的时候给VC赋值

    NSString *key = [self.firstLArr objectAtIndex:indexPath.section];

    NSArray *stuArr = [self.contactDic objectForKey:key];

    

    Student *stu = [stuArr objectAtIndex:indexPath.row ];

    //给下一个界面的student赋值

    //从前往后传值 签协议 设代理 给下一个界面赋值 到下一个界面接收

    secondVC.stu = stu;

    

    //5.指定代理人

    secondVC.delegate = self;

    //8 记录点击的cell的位置 用来在协议方法中使用

    self.indexPath = indexPath;

//    secondVC.modalTransitionStyle =  UIModalTransitionStylePartialCurl ;

    [self.navigationController pushViewController:secondVC animated:YES];

    [secondVC release];

    

}

//6.实现协议方法

- (void)insertStudent:(Student *)stu

{//9

    NSString *key = [self.firstLArr objectAtIndex:self.indexPath.section];

    NSMutableArray *stuArr = [self.contactDic objectForKey:key];

    [stuArr replaceObjectAtIndex:self.indexPath.row withObject:stu];

    //更新完数据,要刷新视图

    [self.tableView reloadData];

}

//设置右边的标题

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{

    return self.firstLArr;

    

}


#import <Foundation/Foundation.h>


@interface Student : NSObject

@property (nonatomic, retain) NSString *name;

@property (nonatomic, retain) NSString *age;


//便利构造器,通过一个字典初始化一个学生对象

+ (instancetype)studentWithDic:(NSDictionary *)dic;

@end

#import "Student.h"


@implementation Student

- (void)dealloc

{

    [_age release];

    [_name release];

    [super dealloc];

}

- (instancetype)initWithName:(NSString *)name age:(NSString *)age

{

    self = [super init];

    if (self) {

        self.name = name;

        self.age = age;

       

    }

    return self;

}

+ (instancetype)studentWithDic:(NSDictionary *)dic;

{

    Student *stu = [[Student alloc]init];

    stu.name = [dic objectForKey:@"name"];

    stu.age = [dic objectForKey:@"age"];

    return [stu autorelease];

    

}


@end

#import <UIKit/UIKit.h>


@class Student;//让student当类名用

//3写协议 1.创建代理

@protocol SecondVCDelegate <NSObject>


- (void)insertStudent:(Student *)stu;


@end

@interface SecondViewController : UIViewController


@property(nonatomic,retain) Student *stu;

//2 创建代理属性

@property(nonatomic,assign)id<SecondVCDelegate>delegate;




@end


#import "SecondViewController.h"

#import "Student.h"

@interface SecondViewController ()<UITextFieldDelegate>

@property (nonatomic,retain)UITextField *nameField;

@property (nonatomic,retain)UITextField *ageField;

@end


@implementation SecondViewController

- (void)dealloc

{

    [_nameField release];

    [_ageField release];

    [_stu release];

    [super dealloc];

}

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

 

    

    self.nameField.backgroundColor = [UIColor whiteColor];

    self.nameField = [[UITextField alloc]initWithFrame:CGRectMake(70, 150, 130, 40)];

    [self.view addSubview:self.nameField];

    self.nameField.borderStyle = UITextBorderStyleRoundedRect;

    self.nameField.placeholder = @"请输姓名";

    //1 从前往后传值 接收前一个界面的数据内容

    _nameField.text= self.stu.name;

    

   self.nameField.textAlignmentNSTextAlignmentCenter;

    self.nameField.delegate = self;

    [self.nameField release];

    

    self.ageField.backgroundColor = [UIColor whiteColor];

    self.ageField = [[UITextField alloc]initWithFrame:CGRectMake(70, 250, 130, 40)];

    [self.view addSubview:self.ageField];

    self.ageField.borderStyle = UITextBorderStyleRoundedRect;

    self.ageField.placeholder = @"请输年龄";

    

    _ageField.text = self.stu.age;

    

    self.ageField.textAlignmentNSTextAlignmentCenter;

    self.ageField.delegate = self;

    [self.ageField release];

    

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

    button.frame = CGRectMake(50, 300, 50, 40);

    button.backgroundColor = [UIColor whiteColor];

    [button setTitle:@"save" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

    

}

//3.让代理执行协议方法


- (void)buttonAction:(UIButton *)btn

{

    Student *stu = [[Student alloc]init];

    stu.name = _nameField.text;

    stu.age = _ageField.text;

    [self.delegate insertStudent:stu];

    [self.navigationController popViewControllerAnimated:YES];

    [stu release];

}


#import <Foundation/Foundation.h>


@interface NSString (Characters)


//讲汉字转换为拼音

- (NSString *)pinyinOfName;


//汉字转换为拼音后,返回大写的首字母

- (NSString *)firstCharacterOfName;


@end

#import "NSString+Characters.h"


@implementation NSString (Characters)


//讲汉字转换为拼音

- (NSString *)pinyinOfName

{

    NSMutableString * name = [[[NSMutableString alloc] initWithString:self ] autorelease];

    

    CFRange range = CFRangeMake(0, self.length);

    

    // 汉字转换为拼音,并去除音调

    if ( ! CFStringTransform((__bridge CFMutableStringRef) name, &range, kCFStringTransformMandarinLatin, NO) ||

        ! CFStringTransform((__bridge CFMutableStringRef) name, &range, kCFStringTransformStripDiacritics, NO)) {

        return @"";

    }

    

    return name;

}


//汉字转换为拼音后,返回大写的首字母

- (NSString *)firstCharacterOfName

{

    

    NSMutableString * first = [[[NSMutableString alloc] initWithString:[self substringWithRange:NSMakeRange(0, 1)]] autorelease];

    

    CFRange range = CFRangeMake(0, 1);

    

    // 汉字转换为拼音,并去除音调

    if ( ! CFStringTransform((__bridge CFMutableStringRef) first, &range, kCFStringTransformMandarinLatin, NO) ||

        ! CFStringTransform((__bridge CFMutableStringRef) first, &range, kCFStringTransformStripDiacritics, NO)) {

        return @"";

    }

    

    NSString * result;

    result = [first substringWithRange:NSMakeRange(0, 1)];

    

    return result.uppercaseString;

}

@end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值