#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
#import "AppDelegate.h"
#import "RootTableViewController.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]] autorelease];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
RootTableViewController *rootVC = [[RootTableViewController alloc] init];
UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:rootVC];
self.window.rootViewController = navC;
[navC release];
[rootVC release];
return YES;
}
@end
#import <UIKit/UIKit.h>
@interface RootTableViewController : UITableViewController
@end
#import "RootTableViewController.h"
#import "StudentCell.h"//导入自定义的单元格头文件
#import "Student.h"//导入student数据模型头文件
#import "StudentCell2.h"
@interface RootTableViewController ()
@property (nonatomic, retain) NSMutableArray *studentArray;
@end
@implementation RootTableViewController
- (void)dealloc{
[_studentArray release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"联系人";
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Students" ofType:@"plist"];
NSArray *sourceArray = [NSArray arrayWithContentsOfFile:filePath];
self.studentArray = [NSMutableArray array];
for (NSDictionary *dic in sourceArray) {
Student *student = [[Student alloc] init];
[student setValuesForKeysWithDictionary:dic];
[_studentArray addObject:student];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_studentArray count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
Student *student = _studentArray[indexPath.row];
return [StudentCell cellHeightForStudent:student];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Student *student = _studentArray[indexPath.row];
if ([student.sex isEqualToString:@"女"]) {
static NSString *resuerIndenfier = @"cell2";
StudentCell2 *cell = [tableView dequeueReusableCellWithIdentifier:resuerIndenfier];
if (cell == nil) {
cell = [[[StudentCell2 alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:resuerIndenfier] autorelease];
}
cell.student = student;
return cell;
}else{
static NSString *reuseIdentifier = @"cell";
StudentCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (cell == nil) {
cell = [[[StudentCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:reuseIdentifier] autorelease];
}
cell.student = student;
return cell;
}
}
@end
#import <UIKit/UIKit.h>
@class Student;
@interface StudentCell : UITableViewCell
@property (nonatomic, retain, readonly) UIImageView *iconImageView;
@property (nonatomic, retain, readonly) UILabel *nameLabel;
@property (nonatomic, retain, readonly) UILabel *genderLabel;
@property (nonatomic, retain, readonly) UILabel *phoneNumberLabel;
@property (nonatomic, retain, readonly) UILabel *introduceLabel;
@property (nonatomic, retain) Student *student;
+ (CGFloat)heightForString:(NSString *)string;
+ (CGFloat)cellHeightForStudent:(Student *)student;
@end
#import "StudentCell.h"
#import "Student.h"
#define KLeft 10;
#define KTop 10
#define KHorizonSpacing 10 //水平间距
#define KVerticalSpacing 10 //竖直间距
@implementation StudentCell
- (void)dealloc{
[_iconImageView release];
[_nameLabel release];
[_genderLabel release];
[_phoneNumberLabel release];
[_introduceLabel release];
[_student release];
[super dealloc];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
CGFloat totalWidth = [UIScreen mainScreen].bounds.size.width;
CGFloat labelHeight = 30;
CGFloat iconX = KLeft;
CGFloat iconY = KTop;
CGFloat iconWidth = 150;
CGFloat iconHeight = 225;
_iconImageView = [[UIImageView alloc] initWithFrame:(CGRectMake(iconX, iconY, iconWidth, iconHeight))];
_iconImageView.backgroundColor = [UIColor yellowColor];
[self.contentView addSubview:_iconImageView];
CGFloat nameX = iconX + iconWidth + KHorizonSpacing;
CGFloat nameY = KTop;
CGFloat nameWidth = totalWidth - nameX - KLeft;
CGFloat nameHeight = labelHeight;
_nameLabel = [[UILabel alloc] initWithFrame:(CGRectMake(nameX, nameY, nameWidth, nameHeight))];
_nameLabel.backgroundColor = [UIColor cyanColor];
_nameLabel.alpha = 0.5;
_nameLabel.font = [UIFont systemFontOfSize:18];
[self.contentView addSubview:_nameLabel];
CGFloat genderX = nameX;
CGFloat genderY = nameY + nameHeight + KVerticalSpacing;
CGFloat genderWidth = nameWidth;
CGFloat genderHeight = labelHeight;
_genderLabel = [[UILabel alloc] initWithFrame:(CGRectMake(genderX, genderY, genderWidth, genderHeight))];
_genderLabel.backgroundColor = [UIColor cyanColor];
_genderLabel.alpha = 0.5;
_genderLabel.font = [UIFont systemFontOfSize:18];
[self.contentView addSubview:_genderLabel];
CGFloat phoneX = genderX;
CGFloat phoneY = genderY + genderHeight + KVerticalSpacing;
CGFloat phoneWidth = genderWidth;
CGFloat phoneHeight = labelHeight;
_phoneNumberLabel = [[UILabel alloc] initWithFrame:(CGRectMake(phoneX, phoneY, phoneWidth, phoneHeight))];
_phoneNumberLabel.backgroundColor = [UIColor cyanColor];
_phoneNumberLabel.alpha = 0.5;
_phoneNumberLabel.font = [UIFont systemFontOfSize:18];
[self.contentView addSubview:_phoneNumberLabel];
CGFloat introX = phoneX;
CGFloat introY = phoneY + phoneHeight + KVerticalSpacing;
CGFloat introWidth = phoneWidth;
CGFloat introHeight = 2 * labelHeight;
_introduceLabel = [[UILabel alloc] initWithFrame:(CGRectMake(introX, introY, introWidth, introHeight))];
_introduceLabel.backgroundColor = [UIColor cyanColor];
_introduceLabel.alpha = 0.5;
_introduceLabel.font = [UIFont systemFontOfSize:18];
_introduceLabel.numberOfLines = 0;
[self.contentView addSubview:_introduceLabel];
}
return self;
}
- (void)setStudent:(Student *)student{
if (_student != student) {
[_student release];
_student = [student retain];
_iconImageView.image = [UIImage imageNamed:student.icon];
_nameLabel.text = student.name;
_genderLabel.text = student.sex;
_phoneNumberLabel.text = student.phoneNumber;
_introduceLabel.text = student.introduce;
CGRect recIntroduce = _introduceLabel.frame;
recIntroduce = CGRectMake(recIntroduce.origin.x, recIntroduce.origin.y, recIntroduce.size.width, [StudentCell heightForString:student.introduce]);
_introduceLabel.frame = recIntroduce;
CGRect originFrame = self.frame;
CGFloat height = [StudentCell cellHeightForStudent:student];
originFrame = CGRectMake(originFrame.origin.x, originFrame.origin.y, originFrame.size.width,height);
self.frame = originFrame;
}
}
#pragma mark-- 自适应高度
+ (CGFloat)heightForString:(NSString *)string{
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:18], NSFontAttributeName, nil];
CGFloat width = [UIScreen mainScreen].bounds.size.width - 150 - KHorizonSpacing - 2 * KLeft;
CGSize size = CGSizeMake(width, 10000);
CGRect bounding = [string boundingRectWithSize:size options:(NSStringDrawingUsesLineFragmentOrigin) attributes:attributes context:nil];
return bounding.size.height;
}
+ (CGFloat)cellHeightForStudent:(Student *)student{
CGFloat stringHeight = [self heightForString:student.introduce];
CGFloat cellHeight = 2 * KTop + 3 * (KVerticalSpacing + 30) + stringHeight;
CGFloat miniCellHeight = 2 * KTop + 225;
return cellHeight > miniCellHeight ? cellHeight : miniCellHeight;
}
- (void)awakeFromNib {
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
@end
#import <UIKit/UIKit.h>
@class Student;
@interface StudentCell2 : UITableViewCell
@property (nonatomic, retain, readonly) UIImageView *iconImageView;
@property (nonatomic, retain, readonly) UILabel *nameLabel;
@property (nonatomic, retain, readonly) UILabel *genderLabel;
@property (nonatomic, retain, readonly) UILabel *phoneNumberLabel;
@property (nonatomic, retain, readonly) UILabel *introduceLabel;
@property (nonatomic, retain) Student *student;
@end
#import "StudentCell2.h"
#import "Student.h"
#define KLeft 10;
#define KTop 10
#define KHorizonSpacing 10 //水平间距
#define KVerticalSpacing 10 //竖直间距
@implementation StudentCell2
- (void)dealloc{
[_iconImageView release];
[_nameLabel release];
[_genderLabel release];
[_phoneNumberLabel release];
[_introduceLabel release];
[_student release];
[super dealloc];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
CGFloat totalWidth = [UIScreen mainScreen].bounds.size.width;
CGFloat labelHeight = 30;
CGFloat iconWidth = 150;
CGFloat iconHeight = 225;
CGFloat iconX = totalWidth - iconWidth - KLeft;
CGFloat iconY = KTop;
_iconImageView = [[UIImageView alloc] initWithFrame:(CGRectMake(iconX, iconY, iconWidth, iconHeight))];
_iconImageView.backgroundColor = [UIColor yellowColor];
[self.contentView addSubview:_iconImageView];
CGFloat nameX = KLeft;
CGFloat nameY = KTop;
CGFloat nameWidth = totalWidth - iconWidth - KHorizonSpacing - 2 * KLeft;
CGFloat nameHeight = labelHeight;
_nameLabel = [[UILabel alloc] initWithFrame:(CGRectMake(nameX, nameY, nameWidth, nameHeight))];
_nameLabel.backgroundColor = [UIColor cyanColor];
_nameLabel.alpha = 0.5;
_nameLabel.font = [UIFont systemFontOfSize:18];
[self.contentView addSubview:_nameLabel];
CGFloat genderX = nameX;
CGFloat genderY = nameY + nameHeight + KVerticalSpacing;
CGFloat genderWidth = nameWidth;
CGFloat genderHeight = labelHeight;
_genderLabel = [[UILabel alloc] initWithFrame:(CGRectMake(genderX, genderY, genderWidth, genderHeight))];
_genderLabel.backgroundColor = [UIColor cyanColor];
_genderLabel.alpha = 0.5;
_genderLabel.font = [UIFont systemFontOfSize:18];
[self.contentView addSubview:_genderLabel];
CGFloat phoneX = genderX;
CGFloat phoneY = genderY + genderHeight + KVerticalSpacing;
CGFloat phoneWidth = genderWidth;
CGFloat phoneHeight = labelHeight;
_phoneNumberLabel = [[UILabel alloc] initWithFrame:(CGRectMake(phoneX, phoneY, phoneWidth, phoneHeight))];
_phoneNumberLabel.backgroundColor = [UIColor cyanColor];
_phoneNumberLabel.alpha = 0.5;
_phoneNumberLabel.font = [UIFont systemFontOfSize:18];
[self.contentView addSubview:_phoneNumberLabel];
CGFloat introX = phoneX;
CGFloat introY = phoneY + phoneHeight + KVerticalSpacing;
CGFloat introWidth = phoneWidth;
CGFloat introHeight = 4 * labelHeight;
_introduceLabel = [[UILabel alloc] initWithFrame:(CGRectMake(introX, introY, introWidth, introHeight))];
_introduceLabel.backgroundColor = [UIColor cyanColor];
_introduceLabel.alpha = 0.5;
_introduceLabel.font = [UIFont systemFontOfSize:18];
_introduceLabel.numberOfLines = 0;
[self.contentView addSubview:_introduceLabel];
}
return self;
}
- (void)setStudent:(Student *)student{
if (_student != student) {
[_student release];
_student = [student retain];
_iconImageView.image = [UIImage imageNamed:student.icon];
_nameLabel.text = student.name;
_genderLabel.text = student.sex;
_phoneNumberLabel.text = student.phoneNumber;
_introduceLabel.text = student.introduce;
}
}
- (void)awakeFromNib {
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
@end
#import <Foundation/Foundation.h>
@interface Student : NSObject
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *phoneNumber;
@property (nonatomic, copy) NSString *sex;
@property (nonatomic, copy) NSString *introduce;
@end
#import "Student.h"
@implementation Student
- (void)dealloc{
[_icon release];
[_name release];
[_phoneNumber release];
[_sex release];
[_introduce release];
[super dealloc];
}
@end