StudentCell.m文件 //自适应行高既要里面的label适应 又要cell自适应,所以应该有两处改动
@implementation StudentCell
- (void)dealloc
{
[_nameLabel release];
[_sexLabel release];
[_phoneLabel release];
[_photoView release];
[_hobbyLabel release];
[super dealloc];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.photoView = [[[UIImageView alloc] initWithFrame:CGRectMake(5, 2, 50, 40)] autorelease];
self.nameLabel = [[[ UILabel alloc] initWithFrame:CGRectMake(60, 2, 100, 30)] autorelease];
self.sexLabel = [[[UILabel alloc] initWithFrame:CGRectMake(5, 50, 100, 30)] autorelease];
self.phoneLabel = [[[UILabel alloc] initWithFrame:CGRectMake(5, 100, 200, 30)] autorelease];
self.hobbyLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 140, 200, 100)];
self.hobbyLabel.numberOfLines = 0;//想要自适应宽高就把行数设置为0
self.hobbyLabel.font = [UIFont systemFontOfSize:16];这里是设置字体大小
[self addSubview:self.nameLabel];
[self addSubview:_sexLabel];//这里用下划线还是点都是一样的
[self addSubview:_photoView];
[self addSubview:_phoneLabel];
[self addSubview:_hobbyLabel];
// Initialization code
}
return self;
}
- (void)newFrame
{
self.photoView.frame = CGRectMake(270, 2, 50, 40);
self.nameLabel.frame = CGRectMake(215, 2, 100, 30);
self.sexLabel.frame = CGRectMake(215, 50, 100, 30);
self.phoneLabel.frame =CGRectMake(115, 100, 200, 30);
self.hobbyLabel.frame =CGRectMake(115, 140, 200, 100);
}
- (void)awakeFromNib
{
// Initialization code
}
- (void)setStudent:(Students *)student
{ //重写set
self.nameLabel.text = student.name;
self.sexLabel.text = student.sex;
// if ([student.sex isEqualToString:@"女"]) {
// [self newFrame];
// }
CGRect rect =[student.hobby boundingRectWithSize:CGSizeMake(200, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:self.hobbyLabel.font,NSFontAttributeName, nil] context:nil];
self.hobbyLabel.frame = CGRectMake(115, 140, 200,rect.size.height);//指定字号,指定宽度下面得到高度是多少
self.phoneLabel.text = student.phone;
self.hobbyLabel.text = student.hobby;
self.photoView.image = [UIImage imageNamed:student.photo];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
RootTableViewController.m文件代码
<pre name="code" class="html">@implementation RootTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Students" ofType:@"plist"];//这是一个单例 获取文件的地址一般用这个
NSArray *students = [[NSArray alloc] initWithContentsOfFile:filePath];//用实例变量数组接受一下文件中的数据
_studentArray = [[NSMutableArray alloc] initWithCapacity:[students count]];
for (int i = 0; i < [students count]; i++) {
NSDictionary *dic = [students objectAtIndex:i];
Students *stu = [[Students alloc] init];
[stu setValuesForKeysWithDictionary:dic];//KVC赋值
[_studentArray addObject:stu];
[stu release];
}
[students release];
// NSLog(@"%@",_studentArray);
// 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;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return [_studentArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//*****方案三 用自己继承与UITableViewCell的类 自己定义属性
Students *stu = [_studentArray objectAtIndex:indexPath.row];
if ([stu.sex isEqualToString:@"男"]) {
static NSString *identifier = @"reuse";
StudentCell *cell1 = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell1==nil) {
cell1 = [[StudentCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
}
cell1.student = _studentArray[indexPath.row];
return cell1;
}
else
{
static NSString *cellIdentifier = @"reuse";
SecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[SecondTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.student = _studentArray[indexPath.row];//最简洁一行代码
// Students *stu = [_studentArray objectAtIndex:indexPath.row];
// [cell setStudent:stu];
// cell.nameLabel.text = stu.name;
// cell.sexLabel.text = stu.sex;
if ([stu.sex isEqualToString:@"女"]) {
[cell newFrame];
}
// cell.phoneLabel.text = stu.phone;
// cell.photoView.image = [UIImage imageNamed:stu.photo];
// cell.hobbyLabel.text = stu.hobby;
return cell;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{//设置行高 这个很重要
Students *stu = [_studentArray objectAtIndex:indexPath.row];
if ([stu.sex isEqualToString:@"男"]) {
CGRect rect =[stu.hobby boundingRectWithSize:CGSizeMake(200, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16],NSFontAttributeName, nil] context:nil];
//这里可以把这行代码放在外面,我的这里返回值没有改变,其实可以改变一下男女不同高度返回值
return 140+rect.size.height;
}
else
{
CGRect rect =[stu.hobby boundingRectWithSize:CGSizeMake(200, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16],NSFontAttributeName, nil] context:nil];
return 140+rect.size.height;
}
return 0;
}
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
// //*****方案二 ,把建立label写进if语句中,这样label就一起重用了 不会出现重叠现象
//
// static NSString *cellIdentifier = @"reuse";
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// if (cell == nil) {
//
// cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
//
// UILabel *sexLabel = [[UILabel alloc] initWithFrame:CGRectMake(100,0, 150, 40)];
// sexLabel.tag = 1000;
// [cell.contentView addSubview:sexLabel];//自己创建一个sexlabel加到cell的contentview上面
// [sexLabel release];
//
// }
//
//
// UILabel *sexLabel = (UILabel *)[cell.contentView viewWithTag:1000];//因为if中sexLabe是个局部变量,所以需要出来接收一下在使用.接收的时候使用tag值.
// NSDictionary *stu = [_studentArray objectAtIndex:indexPath.row];
// cell.textLabel.text = [stu objectForKey:@"name"];
// sexLabel.text = [stu objectForKey:@"sex"];
//
//
// return cell;
//
//}
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
// //******方案一 创建label在if语句外面 需要在创建一个label之前先消除同样tag值得label
// static NSString *cellIdentifier = @"reuse";
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// if (cell == nil) {
// cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
// }
// [[cell.contentView viewWithTag:1000] removeFromSuperview];//每次先remove掉tag值为1000的再添加,否则会一层层重叠label
// UILabel *sexLabel = [[UILabel alloc] initWithFrame:CGRectMake(100,0, 150, 40)];
// sexLabel.tag = 1000;
// [cell.contentView addSubview:sexLabel];//自己创建一个sexlabel加到cell的contentview上面
// [sexLabel release];
//
//
//
//
// NSDictionary *stu = [_studentArray objectAtIndex:indexPath.row];
// cell.textLabel.text = [stu objectForKey:@"name"];
// sexLabel.text = [stu objectForKey:@"phone"];
//
// // Configure the cell...
//
// return cell;
//}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
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