<AppDelegate.m>
#import "AppDelegate.h" #import "ContactListViewController.h" @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]; [self.window makeKeyAndVisible]; ContactListViewController *contactLVC = [[ContactListViewController alloc]init]; UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:contactLVC]; self.window.rootViewController = naVC; [contactLVC release]; [naVC release]; return YES; } - (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } @end
#import <UIKit/UIKit.h> @interface ContactListViewController : UIViewController @end
<ContactListViewController.m>
#import "ContactListTableViewController.h" #import "Contact.h" #import "ContactCell.h" #import "GirlCell.h" @interface ContactListTableViewController () @property (nonatomic,retain)NSMutableArray *allKeys; @property (nonatomic,retain)NSMutableDictionary *groupDic; @end @implementation ContactListTableViewController -(void)dealloc { self.allKeys = nil; self.groupDic = nil; [super dealloc]; } - (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Uncomment the following line to preserve selection between presentations. // self.clearsSelectionOnViewWillAppear = NO; // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem; //读取数据 [self reloadData]; //自定义 navigationbar self.navigationItem.title = @"联系人"; } //================================= 读取数据 ============================================ -(void)reloadData { //读取数据包 NSString *filePath = [[NSBundle mainBundle]pathForResource:@"CorrectContact.plist" ofType:nil]; self.groupDic = [NSMutableDictionary dictionaryWithContentsOfFile:filePath]; //获取所有 key 值 NSArray *key = [_groupDic allKeys]; self.allKeys = [NSMutableArray arrayWithArray:key]; //排序 [_allKeys sortUsingSelector:@selector(compare:)]; //遍历外部字典 for (NSString *key in _allKeys) { //查找数组,新建数组存放对象 NSMutableArray *group = [_groupDic valueForKey:key]; NSMutableArray *newGroup = [NSMutableArray array ]; //遍历数组 for (NSMutableDictionary *dic in group) { //新建联系人对象 Contact *contact = [[Contact alloc]init]; //字典数据赋值给对象 [contact setValuesForKeysWithDictionary:dic]; //对象放入新数组 [newGroup addObject:contact]; [contact release]; } //数组添加至外部字典 [_groupDic setValue:newGroup forKey:key]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(NSMutableArray *)getGroupWithSection:(NSUInteger)section { return [_groupDic valueForKey:[_allKeys objectAtIndex:section]]; } #pragma mark - Table view data source //================================= suorce and delegate ============================================ - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return [_allKeys count]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return [[_groupDic valueForKey:[_allKeys objectAtIndex:section]] count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { /* cell = [tableView dequeueReusableCellWithIdentifier:identifier] 无需注册,但需要在此语句下进行 alloc init 的创建 cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath] 需要注册,不需要进行 alloc 和 init 因为在forIndexPath:indexPath语句中,系统从 cell 的重用队列的集合中自动取出 cell. 注册方式: 1.代码注册 [tableView registerClass:<#(Class)#> forCellReuseIdentifier:<#(NSString *)#>] [tableView registerNib:<#(UINib *)#> forCellReuseIdentifier:<#(NSString *)#>] 2.storyboard 注册cell 和 xib 注册cell 在 storyboard 中的 cell 属性的 identifier 中添加字符串,则系统会自动注册 cell 在 xib 中,cell 属性的 identifier 中添加字符串 后仍然需要代码执行注册,且 identifier 需要一致 */ //*********************系统 cell *********************** // static NSString *identifier = @"had"; // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; // // if (!cell) { // cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier]; // } // //获取对应联系人 // NSMutableArray *group = [self getGroupWithSection:indexPath.section]; // Contact *contact = [group objectAtIndex:indexPath.row]; // // cell.textLabel.text = contact.name; // cell.detailTextLabel.text = contact.phoneNumber; // cell.imageView.image = [UIImage imageNamed:contact.photoName]; // //*********************自定义 cell *********************** // // static NSString *identifier = @"had"; // // ContactCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; // // if (!cell) { // cell = [[ContactCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier]; // } // // //获取对应联系人 // NSMutableArray *group = [self getGroupWithSection:indexPath.section]; // Contact *contact = [group objectAtIndex:indexPath.row]; // //// cell.nameView.text = contact.name; //// cell.phoneView.text = contact.phoneNumber; //// cell.photoView.image = [UIImage imageNamed:contact.photoName]; //// cell.descriptionView.text = contact.description; //// // //当 model 类的属性进行内部赋值完成后可以直接由对象赋值来代替以上 // cell.contact = contact; // //*********************自定义一个页面的多样化cell *********************** static NSString *boy = @"boy"; static NSString *girl = @"girl"; NSMutableArray *group = [_groupDic valueForKey:[_allKeys objectAtIndex:indexPath.section]]; Contact *contact = [group objectAtIndex:indexPath.row]; if ([contact.sex isEqualToString:@"男"]) { ContactCell *cell = [tableView dequeueReusableCellWithIdentifier:boy]; if (!cell) { cell = [[ContactCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:boy]; } cell.contact = contact; return cell; } else // if([contact.sex isEqualToString:@"女"]) { GirlCell *cell = [tableView dequeueReusableCellWithIdentifier:girl]; if (!cell) { cell = [[GirlCell alloc ]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:girl ]; } cell.contact = contact; return cell; } // Configure the cell... } -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return [_allKeys objectAtIndex:section]; } -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return _allKeys; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSMutableArray *group = [_groupDic valueForKey:[_allKeys objectAtIndex:indexPath.section]]; Contact *contact = [group objectAtIndex:indexPath.row]; CGFloat height = [GirlCell heightWithString:contact.description]; return 150 + height; } @end
<Contact.h>
#import <Foundation/Foundation.h> @interface Contact : NSObject @property (nonatomic,copy)NSString *name; @property (nonatomic,copy)NSString *sex; @property (nonatomic,copy)NSString *age; @property (nonatomic,copy)NSString *phoneNumber; @property (nonatomic,copy)NSString *photoName; @property (nonatomic,copy)NSString *description; @end
<Contact.m>
#import "Contact.h" @implementation Contact -(void)dealloc { self.name = nil; self.age = nil; self.sex = nil; self.phoneNumber = nil; self.photoName = nil; self.description = nil; [super dealloc]; } @end
#import <UIKit/UIKit.h> #import "Contact.h" @interface ContactCell : UITableViewCell @property (nonatomic,retain)UIImageView *photoView; @property (nonatomic,retain)UILabel *nameView; @property (nonatomic,retain)UILabel *phoneView; @property (nonatomic,retain)UILabel *descriptionView; @property (nonatomic,retain)Contact *contact; //设置 model 类属性 +(CGFloat)hightWithString:(NSString *)string; @end
<ContactCell.m>
#import "ContactCell.h" #define kPhotoViewX 20 #define kPhotoViewY 10 #define kPhotoViewWidth 80 #define kPhontViewHiht 100 @implementation ContactCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code //头像 self.photoView = [[UIImageView alloc] initWithFrame:CGRectMake(kPhotoViewX, kPhotoViewY, kPhotoViewWidth, kPhontViewHiht)]; _photoView.backgroundColor = [UIColor cyanColor]; [self.contentView addSubview:_photoView]; [_photoView release]; //姓名 self.nameView = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_photoView.frame) + 20, CGRectGetMinY(_photoView.frame) + 5, 160, 40)]; _nameView.textAlignment = NSTextAlignmentCenter; [self.contentView addSubview:_nameView]; [_nameView release]; //电话 self.phoneView = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_photoView.frame) + 10, CGRectGetMaxY(_nameView.frame) + 10, 180, 40)]; _phoneView.textAlignment = NSTextAlignmentCenter; [self.contentView addSubview: _phoneView]; [_phoneView release]; //描述 self.descriptionView = [[UILabel alloc]initWithFrame:CGRectMake( 10 , CGRectGetMaxY(_photoView.frame) + 5, 300, 30)]; _descriptionView.backgroundColor = [UIColor orangeColor]; _descriptionView.numberOfLines = 0; [self.contentView addSubview: _descriptionView]; [_descriptionView release]; } return self; } //通过 setter 方法 -(void)setContact:(Contact *)contact { if (_contact != contact) { [_contact release]; _contact = [contact retain]; } _nameView.text = _contact.name; _phoneView.text = _contact.phoneNumber; _photoView.image = [UIImage imageNamed:_contact.photoName]; _descriptionView.text = _contact.description; //获取text文字需要的告诉 CGFloat hight = [ContactCell hightWithString:_descriptionView.text]; _descriptionView.frame = CGRectMake(_descriptionView.frame.origin.x, _descriptionView.frame.origin.y , _descriptionView.frame.size.width, hight); } +(CGFloat)hightWithString:(NSString *)string { //创建字体 NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil]; CGRect rect = [string boundingRectWithSize:CGSizeMake(300, 1000000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil]; return rect.size.height; } - (void)awakeFromNib { // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } @end
<GirlCell.h>
#import <UIKit/UIKit.h> #import "Contact.h" @interface GirlCell : UITableViewCell @property (nonatomic,retain)UIImageView *photoView; @property (nonatomic,retain)UILabel *nameView; @property (nonatomic,retain)UILabel *phoneView; @property (nonatomic,retain)UILabel *descriptionView; @property (nonatomic,retain)Contact *contact; +(CGFloat)heightWithString:(NSString *)string; @end
<GirlCell.m>
#import "GirlCell.h" @implementation GirlCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code self.nameView = [[UILabel alloc]initWithFrame:CGRectMake(10, 15, 160, 40)]; _nameView.textAlignment = NSTextAlignmentCenter; [self.contentView addSubview:_nameView]; [_nameView release]; self.phoneView = [[UILabel alloc]initWithFrame:CGRectMake(5, CGRectGetMaxY(_nameView.frame) + 10, 180, 40)]; _phoneView.textAlignment = NSTextAlignmentCenter; [self.contentView addSubview:_phoneView]; [_phoneView release]; self.descriptionView = [[UILabel alloc]initWithFrame:CGRectMake(5, CGRectGetMaxY(_photoView.frame) + 120, 300, 30)]; _descriptionView.numberOfLines = 0; _descriptionView.backgroundColor = [UIColor cyanColor]; [self.contentView addSubview:_descriptionView]; [_descriptionView release]; self.photoView = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_phoneView.frame) + 10, 15, 80, 100)]; [self.contentView addSubview:_photoView]; [_photoView release]; } return self; } -(void)setContact:(Contact *)contact { if (_contact != contact) { [_contact release]; _contact = [contact retain]; } _nameView.text = _contact.name; _phoneView.text = _contact.phoneNumber; _descriptionView.text = _contact.description; _photoView.image = [UIImage imageNamed:_contact.photoName]; CGFloat height = [GirlCell heightWithString:_descriptionView.text]; _descriptionView.frame = CGRectMake(_descriptionView.frame.origin.x, _descriptionView.frame.origin.y , _descriptionView.frame.size.width, height); } +(CGFloat)heightWithString:(NSString *)string { NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17],NSFontAttributeName,nil]; CGRect rect = [string boundingRectWithSize:CGSizeMake(300, 100000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil]; return rect.size.height; } - (void)awakeFromNib { // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } @end