UITableViewCell
#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 = @"联系人";
self.view.backgroundColor = [UIColor brownColor];
}
//读取数据
- (void)reloadData
{
//获取文件路径
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Contact.plist" ofType:nil];
//通过文件路径获取数据
self.groupDic = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
//获取所有的key
NSArray *keys = [_groupDic allKeys];
//转换为可变数组并排序
self.allKeys = [NSMutableArray arrayWithArray:keys];
[_allKeys sortUsingSelector:@selector(compare:)];
//封装对象
//遍历
for (NSString *key in _allKeys) {
NSMutableArray *group = [_groupDic valueForKey:key];
//创建对象数组
NSMutableArray *newGroup = [[NSMutableArray alloc] init];
//遍历数组
for (NSMutableDictionary *perDic in group) {
//创建对象
Contact *contact = [[Contact alloc] init];
//赋值
[contact setValuesForKeysWithDictionary:perDic];
//放入数组
[newGroup addObject:contact];
}
//替换数组
[_groupDic setValue:newGroup forKey:key];
}
}
//获取对应分组
- (NSMutableArray *)getGroupWithSection:(NSInteger)section
{
NSString *key = [_allKeys objectAtIndex:section];
NSMutableArray *group = [_groupDic valueForKey:key];
return group;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return _allKeys.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSMutableArray *group = [self getGroupWithSection:section];
// Return the number of rows in the section.
return group.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"aa";
static NSString *girl = @"girl";
//获取对应分组
NSMutableArray *group = [self getGroupWithSection:indexPath.section];
//获取对应联系人
Contact *contact = [group objectAtIndex:indexPath.row];
if ([contact.sex isEqualToString:@"男"]) {
ContactCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[ContactCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}
cell.contact = contact;
return cell;
}
GirlCell *cell = [tableView dequeueReusableCellWithIdentifier:girl];
if (!cell) {
cell = [[GirlCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:girl];
}
cell.contact = contact;
// 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.contact = contact;
//
//// //cell赋值
//// cell.textLabel.text = contact.name;
//// cell.detailTextLabel.text = contact.phoneNumber;
//// cell.imageView.image = [UIImage imageNamed:contact.photoName];
//
// // Configure the cell...
//
//// cell.nameView.text = contact.name;
//// cell.photoView.image = [UIImage imageNamed:contact.photoName];
//// cell.phoneView.text = contact.phoneNumber;
//// cell.descriptionView.text = contact.description;
return cell;
}
//分区title
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [_allKeys objectAtIndex:section];
}
//侧边导航栏
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return _allKeys;
}
//row高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//获取对应分组
NSMutableArray *group = [self getGroupWithSection:indexPath.section];
//获取对应的联系人
Contact *contact = [group objectAtIndex:indexPath.row];
//获取高度
CGFloat height = [GirlCell heightWithString:contact.description];
return 120 + height;
}