//
// AppDelegate.h
// Contact
//
// Created by l on 15/9/3.
// Copyright (c) 2015年 . All rights reserved.
//
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
//
// AppDelegate.m
// Contact
//
// Created by l on 15/9/3.
// Copyright (c) 2015年 . All rights reserved.
//
#import "AppDelegate.h"
#import "FirstController.h"
@interface AppDelegate ()
@end
@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];
FirstController * fVC = [[FirstController alloc]initWithStyle:(UITableViewStyleGrouped)];
UINavigationController * navigationVC = [[UINavigationController alloc]initWithRootViewController:fVC];
self.window.rootViewController = navigationVC;
[self.window makeKeyAndVisible];
[fVC release];
[navigationVC release];
return YES;
}
@end
/
//
// FirstController.h
// Contact
//
// Created by l on 15/9/3.
// Copyright (c) 2015年 . All rights reserved.
//
#import <UIKit/UIKit.h>
@interface FirstController : UITableViewController
@end
//
// FirstController.m
// Contact
//
// Created by l on 15/9/3.
// Copyright (c) 2015年 . All rights reserved.
//
#import "FirstController.h"
#import "BoyCell.h"
#import "GrilCell.h"
#import "DetailViewController.h"
#import "AddViewController.h"
@interface FirstController ()<AddVCDelegate>
@property(nonatomic,retain)NSMutableDictionary * dict;
@property(nonatomic,retain)NSMutableArray * arr;
@property(nonatomic,retain)NSMutableArray * valueArray;
@end
@implementation FirstController
-(void)getData
{
NSString * filePath = [[NSBundle mainBundle]pathForResource:@"Class25ContactList.plist"ofType:nil];
NSDictionary * dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
self.dict = [NSMutableDictionary dictionaryWithCapacity:0];
self.arr = [NSMutableArray arrayWithCapacity:0];
for (NSString * key in dic) {
[self.arr addObject:key];
self.valueArray= [NSMutableArray arrayWithCapacity:0];
for (NSDictionary * contactDic in dic[key]) {
Student * stu = [[Student alloc]init];
[stu setValuesForKeysWithDictionary:contactDic];
[_valueArray addObject:stu];
[stu release];
}
[self.dict setObject:_valueArray forKey:key];
}
[self.arr sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2];
}];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self getData];
self.tableView.rowHeight = 160;
self.navigationItem.rightBarButtonItem = self.editButtonItem;
UIBarButtonItem *barButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemAdd) target:self action:@selector(pushNextView:)];
self.navigationItem.leftBarButtonItem = barButton;
self.navigationItem.title = @"联系人";
}
-(void)pushNextView:(UIBarButtonItem*)button
{
AddViewController * addVC = [[AddViewController alloc]init];
addVC.delegate = self;
[self.navigationController pushViewController:addVC animated:YES];
}
-(void)passValue:(Student *)stu
{
NSString *str = [stu.name substringToIndex:1];
str = [str uppercaseString];
BOOL isHave = [_arr containsObject:str];
if (!isHave) {
[_arr addObject:str];
[_arr sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2];
}];
NSMutableArray *arr = [NSMutableArray arrayWithCapacity:0];
[arr addObject:stu];
[_dict setObject:arr forKey:str];
}
else
{
[_dict[str] addObject:stu];
}
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [self.arr count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.dict[self.arr[ section]] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Student * stu = self.dict[self.arr[indexPath.section]][indexPath.row];
if ([stu.gender isEqualToString:@"男"]) {
BoyCell * cell = [tableView dequeueReusableCellWithIdentifier:@"text"];
if (cell == nil ) {
cell = [[[BoyCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"text"]autorelease];
}
[cell configureCellForModel:stu];
return cell;
}
else {
GrilCell * cell = [tableView dequeueReusableCellWithIdentifier:@"test"];
if (cell == nil ) {
cell =[[[GrilCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"test"]autorelease];
}
[cell getConfigure:stu];
return cell;
}
}
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString * key = self.arr[section];
return key;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController * detailVC = [[DetailViewController alloc]init];
detailVC.stu = self.dict[self.arr[indexPath.section]][indexPath.row];
[self.navigationController pushViewController:detailVC animated:YES];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return self.arr;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSMutableArray * array =self.dict[self.arr [indexPath.section]];
[array removeObjectAtIndex:indexPath.row ];
if ([self.dict[self.arr [indexPath.section]] count] == 0 ) {
[self.dict removeObjectForKey:self.arr[indexPath.section]];
[self.arr removeObjectAtIndex:indexPath.section];
NSIndexSet * set = [[NSIndexSet alloc]initWithIndex:indexPath.section];
[tableView deleteSections: set withRowAnimation:(UITableViewRowAnimationFade)];
}else
{
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
/*
#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
// Contact
//
// Created by l on 15/9/3.
// Copyright (c) 2015年 . All rights reserved.
//
#import <UIKit/UIKit.h>
#import "Student.h"
@interface DetailViewController : UIViewController
@property(nonatomic, retain)UIImageView *imageView;
@property(nonatomic, retain)UILabel *nameLabel;
@property(nonatomic, retain)UILabel *genderLabel;
@property(nonatomic, retain)UILabel *ageLabel;
@property(nonatomic, retain)UILabel *phoneNumberLabel;
@property(nonatomic, retain)UILabel *hobbyLabel;
@property(nonatomic, retain)UIButton *button1;
@property(nonatomic, retain)UIButton *button2;
@property(nonatomic, retain)UIButton *button3;
@property(nonatomic,retain)Student * stu;
@end
//
// DetailViewController.m
// Contact
//
// Created by l on 15/9/3.
// Copyright (c) 2015年 . All rights reserved.
//
#import "DetailViewController.h"
@interface DetailViewController ()
@end
@implementation DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
if ([self.stu.gender isEqualToString:@"男"]) {
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 100, 150, 200)];
_imageView.image =[UIImage imageNamed: @"1.jpg"];
[self.view addSubview:_imageView];
}
if ([self.stu.gender isEqualToString:@"女"]) {
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 100, 150, 200)];
_imageView.image =[UIImage imageNamed: @"3.jpg"];
[self.view addSubview:_imageView];
}
_nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 110, 130, 30)];
_nameLabel.text =self.stu.name;
[self.view addSubview:_nameLabel];
_genderLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 185, 130, 30)];
_genderLabel.text = self.stu.gender;
[self.view addSubview:_genderLabel];
_ageLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 250, 130, 30)];
_ageLabel.text = [NSString stringWithFormat:@"%ld",(long)self.stu.age];
[self.view addSubview:_ageLabel];
_phoneNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 330, 300, 60)];
_phoneNumberLabel.layer.borderWidth = 1.0;
_phoneNumberLabel.layer.cornerRadius = 10.0;
_phoneNumberLabel.text = self.stu.phoneNumber;
[self.view addSubview:_phoneNumberLabel];
_hobbyLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 400, 300, 60)];
_hobbyLabel.layer.borderWidth = 1.0;
_hobbyLabel.layer.cornerRadius = 10.0;
_hobbyLabel.text = self.stu.hobby;
[self.view addSubview:_hobbyLabel];
_button1 = [UIButton buttonWithType:(UIButtonTypeSystem)];
_button1.frame = CGRectMake(30, 520, 140, 60);
_button1.layer.borderWidth = 1.0;
_button1.layer.cornerRadius = 10.0;
[_button1 setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
[_button1 setTitle:@"发信息" forState:(UIControlStateNormal)];
[self.view addSubview:_button1];
_button2 = [UIButton buttonWithType:(UIButtonTypeSystem)];
_button2.frame = CGRectMake(190, 520, 140, 60);
_button2.layer.borderWidth = 1.0;
_button2.layer.cornerRadius = 10.0;
[_button2 setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
[_button2 setTitle:@"打电话" forState:(UIControlStateNormal)];
[self.view addSubview:_button2];
_button3 = [UIButton buttonWithType:(UIButtonTypeSystem)];
_button3.frame = CGRectMake(30, 600, 300, 40);
_button3.layer.borderWidth = 1.0;
_button3.layer.cornerRadius = 10.0;
[_button3 setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
[_button3 setTitle:@"删除联系人" forState:(UIControlStateNormal)];
[self.view addSubview:_button3];
// Do any additional setup after loading the view.
}
- (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
//
// AddViewController.h
// Contact
//
// Created by l on 15/9/3.
// Copyright (c) 2015年 . All rights reserved.
//
#import <UIKit/UIKit.h>
#import "Student.h"
@class Student;
@protocol AddVCDelegate <NSObject>
-(void)passValue:(Student *)stu;
@end
@interface AddViewController : UIViewController
@property(nonatomic, retain)UIImageView *pictureView;
@property(nonatomic, retain)UITextField *nameText;
@property(nonatomic, retain)UITextField *genderText;
@property(nonatomic, retain)UITextField *ageText;
@property(nonatomic, retain)UITextField *phoneNumberText;
@property(nonatomic, retain)UITextField *hobbyText;
@property(nonatomic,assign)id<AddVCDelegate>delegate;
@end
//
// AddViewController.m
// Contact
//
// Created by l on 15/9/3.
// Copyright (c) 2015年 . All rights reserved.
//
#import "AddViewController.h"
#import "Student.h"
@interface AddViewController ()
@end
@implementation AddViewController
- (void)viewDidLoad {
[super viewDidLoad];
_nameText = [[UITextField alloc] initWithFrame:CGRectMake(200, 110, 130, 30)];
_nameText.borderStyle = UITextBorderStyleRoundedRect;
_nameText.placeholder = @"输入姓名";
[self.view addSubview:_nameText];
_genderText = [[UITextField alloc] initWithFrame:CGRectMake(200, 185, 130, 30)];
_genderText.placeholder = @"输入性别";
_genderText.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:_genderText];
_ageText = [[UITextField alloc] initWithFrame:CGRectMake(200, 250, 130, 30)];
_ageText.placeholder = @"输入年龄";
_ageText.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:_ageText];
_phoneNumberText = [[UITextField alloc] initWithFrame:CGRectMake(30, 300, 300, 30)];
_phoneNumberText.placeholder = @"输入号码";
_phoneNumberText.layer.borderWidth = 1.0;
_phoneNumberText.layer.cornerRadius = 10.0;
[self.view addSubview:_phoneNumberText];
_hobbyText = [[UITextField alloc] initWithFrame:CGRectMake(30, 350, 300, 30)];
_hobbyText.placeholder = @"输入爱好";
_hobbyText.layer.borderWidth = 1.0;
_hobbyText.layer.cornerRadius = 10.0;
[self.view addSubview:_hobbyText];
UIBarButtonItem * rightbarButton = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:(UIBarButtonItemStyleDone) target:self action:@selector(addContact:)];
UIBarButtonItem * leftbarButton = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:(UIBarButtonItemStyleDone) target:self action:@selector(popRoot:)];
self.navigationItem.rightBarButtonItem = rightbarButton;
self.navigationItem.leftBarButtonItem = leftbarButton;
self.navigationItem.title = @"添加联系人";
}
-(void)popRoot:(UIBarButtonItem *)buttonItem
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
-(void)addContact:(UIBarButtonItem *)buttonItem
{
if ( _phoneNumberText.text.length == 0 || _nameText.text.length== 0 ) {
UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"输入姓名或号码不能为空" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
[alert addAction:cancelAction];
[self presentViewController:alert animated:YES completion:nil];
}
else
{
Student * stu = [[[Student alloc]init]autorelease];
stu.name = _nameText.text;
stu.gender= _genderText.text ;
stu.age = _ageText.text;
stu.phoneNumber = _phoneNumberText.text;
stu.hobby = _hobbyText.text;
if (_delegate && [_delegate respondsToSelector:@selector(passValue:)]) {
[_delegate passValue:stu];
}
[self.navigationController popToRootViewControllerAnimated:YES];
}
}
- (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
///
//
// Student.h
// Contact
//
// Created by l on 15/9/3.
// Copyright (c) 2015年 . All rights reserved.
//
#import <Foundation/Foundation.h>
#import "AddViewController.h"
@class AddViewController;
@interface Student : NSObject
@property(nonatomic,retain)NSString * name;
@property(nonatomic,retain)NSString * gender;
@property(nonatomic,retain)NSString * age;
@property(nonatomic,retain)NSString * phoneNumber;
@property(nonatomic,retain)NSString * hobby;
@property(nonatomic,retain)NSString * picture;
@end
//
// Student.m
// Contact
//
// Created by l on 15/9/3.
// Copyright (c) 2015年 . All rights reserved.
//
#import "Student.h"
@implementation Student
-(void)dealloc
{
[_name release];
[_gender release];
[_phoneNumber release];
[_hobby release];
[_picture release];
[_age release];
[super dealloc];
}
@end
//
// BoyCell.h
// Contact
//
// Created by l on 15/9/3.
// Copyright (c) 2015年 . All rights reserved.
//
#import <UIKit/UIKit.h>
#import "Student.h"
@interface BoyCell : UITableViewCell
@property(nonatomic,retain)UILabel * nameLabel;
@property(nonatomic,retain)UILabel * phoneNumberlabel;
@property(nonatomic,retain)UIImageView * pictureView ;
@property(nonatomic,retain)UILabel * hobbyLabel;
-(void)configureCellForModel:(Student *)stu;
+(CGFloat)heightForRow:(Student *)stu;
+(CGFloat)heightForString:(NSString *)string;
@end
//
// BoyCell.m
// Contact
//
// Created by l on 15/9/3.
// Copyright (c) 2015年 . All rights reserved.
//
#import "BoyCell.h"
@implementation BoyCell
- (void)awakeFromNib {
}
-(void)dealloc
{
[_nameLabel release];
[_phoneNumberlabel release];
[_pictureView release];
[_hobbyLabel release];
[super dealloc];
}
-(void)configureCellForModel:(Student *)stu
{
self.nameLabel.text = stu.name;
self.phoneNumberlabel.text = stu.phoneNumber;
self.hobbyLabel.text = stu.hobby;
self.hobbyLabel.numberOfLines = 0 ;
self.hobbyLabel.frame = CGRectMake(110, 90, 120, [BoyCell heightForString:stu.hobby]);
self.pictureView.image = [UIImage imageNamed: stu.picture];
}
+(CGFloat)heightForRow:(Student *)stu
{
return [self heightForString:stu.hobby] + 90;
}
+(CGFloat)heightForString:(NSString *)string
{
NSDictionary * dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:18] forKey:NSFontAttributeName];
CGRect bounds =[string boundingRectWithSize:CGSizeMake(120, 0) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
return bounds.size.height;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_pictureView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 100, 150)];
[self.contentView addSubview:_pictureView];
_nameLabel = [[UILabel alloc]initWithFrame:CGRectMake( 110, 5, 120, 30)];
[self.contentView addSubview:_nameLabel];
_phoneNumberlabel = [[UILabel alloc]initWithFrame:CGRectMake(110,50, 120, 30)];
[self.contentView addSubview:_phoneNumberlabel];
_hobbyLabel = [[UILabel alloc]initWithFrame:CGRectMake(110, 90, 120, 30)];
[self.contentView addSubview:_hobbyLabel];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
//
// GrilCell.h
// Contact
//
// Created by l on 15/9/3.
// Copyright (c) 2015年 . All rights reserved.
//
#import <UIKit/UIKit.h>
#import "Student.h"
@interface GrilCell : UITableViewCell
@property(nonatomic,retain)UILabel * nameLabel;
@property(nonatomic,retain)UILabel * phoneNumberlabel;
@property(nonatomic,retain)UIImageView * pictureView ;
@property(nonatomic,retain)UILabel * hobbyLabel;
-(void)getConfigure:(Student *)stu;
+(CGFloat)heightForRow:(Student *)stu;
+(CGFloat)heightForString:(NSString *)string;
@end
//
// GrilCell.m
// Contact
//
// Created by l on 15/9/3.
// Copyright (c) 2015年 . All rights reserved.
//
#import "GrilCell.h"
@implementation GrilCell
- (void)awakeFromNib {
// Initialization code
}
-(void)dealloc
{
[_nameLabel release];
[_phoneNumberlabel release];
[_pictureView release];
[_hobbyLabel release];
[super dealloc];
}
-(void)getConfigure:(Student *)stu
{
self.nameLabel.text = stu.name;
self.phoneNumberlabel.text = stu.phoneNumber;
self.pictureView.image = [UIImage imageNamed:stu.picture];
self.hobbyLabel.text = stu.hobby;
self.hobbyLabel.numberOfLines = 0 ;
self.hobbyLabel.frame = CGRectMake(5, 90, 150, [GrilCell heightForString:stu.hobby]);
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_pictureView = [[UIImageView alloc]initWithFrame:CGRectMake(160, 5, 100, 150)];
[self.contentView addSubview:_pictureView];
_nameLabel = [[UILabel alloc]initWithFrame:CGRectMake( 5, 5, 120, 30)];
[self.contentView addSubview:_nameLabel];
_phoneNumberlabel = [[UILabel alloc]initWithFrame:CGRectMake(5,50, 150, 30)];
[self.contentView addSubview:_phoneNumberlabel];
_hobbyLabel = [[UILabel alloc]initWithFrame:CGRectMake(5,90 , 150, 30)];
[self.contentView addSubview:_hobbyLabel];
}
return self;
}
+(CGFloat)heightForRow:(Student *)stu
{
return [self heightForString:stu.hobby] + 90;
}
+(CGFloat)heightForString:(NSString *)string
{
NSDictionary * dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:18] forKey:NSFontAttributeName];
CGRect bounds =[string boundingRectWithSize:CGSizeMake(120, 0) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
return bounds.size.height;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
iOS编程-------AddressBook_自定义cell 自定义单元格通讯录
最新推荐文章于 2021-03-23 20:34:14 发布