Scenes/ViewController
PersonTableViewController.m
@property (nonatomic, retain) NSMutableDictionary *allDataDict;
@property (nonatomic, retain) NSMutableArray *allKeysArray; // 保存排序好的所有key值
//#pragma mark - 加载数据
- (void)loadData
{
// 1. 找到文件在包中的路径
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Class150102AllStudents" ofType:@"plist"];
// 2. 根据路径,读取文件
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePath];
NSLog(@"%@", dict);
// 3. 将读取出来的数据,存入到新的可变字典里
self.allDataDict = [NSMutableDictionary dictionary];
// 3.1 遍历原字典中的所有key值
for (NSString *key in dict.allKeys) {
// 3.2 有一个key就应该有一个对应的可变数组
NSMutableArray *mutableArray = [NSMutableArray array];
// 3.4 遍历原字典中的数组中的小数组
for (NSDictionary *item in dict[key]) {
// 3.5 将item小字典,转化为Person对象
Person *p = [[Person new] autorelease];
[p setValuesForKeysWithDictionary:item]; // 通过字典,设置p对象属性的值
// 3.6 将转好的p对象添加到可变数组中
[mutableArray addObject:p];
}
// 3.3 添加到可变数组中
[_allDataDict setObject:mutableArray forKey:key];
}
// 4. 获取到所有的key值,然后排序
self.allKeysArray = [NSMutableArray arrayWithArray:_allDataDict.allKeys];
// 使用block排序
[_allKeysArray sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
if ([obj1 compare:obj2] == NSOrderedDescending) {
return 1;
}
return 0;
}];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *Identifier = @"asd";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:Identifier] autorelease];
}
NSString *key = _allKeysArray[indexPath.section];
NSMutableArray * array = _allDataDict[key];
Person *p = array[indexPath.row];
cell.textLabel.text = p.name;
cell.imageView.image = [UIImage imageNamed:p.headImageName];
cell.detailTextLabel.text = p.phoneNumber;
// 显示 详情符号
cell.accessoryType = UITableViewCellAccessoryDetailButton;
return cell;
}
- 继承UITableViewCell,写了BoyCell
- 在BoyCell.h声明属性
dealloc / 创建并且添加了
在PersonTableViewControlller.m中引入 BoyCell.h 文件
- 将每行显示什么内容方法中的UITableViewCell替换成了BoyCell
cell.nameLabel.text = p.name; ….
在BoyCell.h中声明Person类,并且声明了Person属性
- 在BoyCell.m中重写perosn属性的setter方法,然后将person参数上的值,赋给了页面上
在外界,直接通过 cell.person = p; 实现cell的赋值
实现了计算文字高度的方法
在重写的setter方法中,计算了自我介绍文字的高度,并修改了_introduceLabel的高度
在BoyCell.h文件中声明了计算cell高度的方法,并实现
- 在设置cell高度的代理方法中使用BoyCell提供的计算cell高度的方法,得到cell应该的高度,并返回