自定义UITableViewCell的样式

首先定义一个Student对象

@interface Student : NSObject

@property (nonatomic, copy) NSString *name; // 姓名
@property (nonatomic, copy) NSString *sex;  // 性别
@property (nonatomic, copy) NSString *introduce; // 个人简历
@property (nonatomic, copy) NSString *hobby; // 爱好
@property (nonatomic, copy) NSString *phone; // 电话
@property (nonatomic, copy) NSString *photo; // 照片

/**
 *  自定义初始化方法
 *
 *  @param name      姓名
 *  @param sex       性别
 *  @param introduce 自我介绍
 *  @param hoby      爱好
 *
 *  @return student对象
 */

- (instancetype)initWithName:(NSString *)name sex:(NSString *)sex introduce:(NSString *)introduce hoby:(NSString *)hoby phone:(NSString *)phone photo:(NSString *)photo;

// 便利构造器方法
+ (Student *)studentWithName:(NSString *)name sex:(NSString *)sex introduce:(NSString *)introduce hoby:(NSString *)hoby phone:(NSString *)phone photo:(NSString *)photo;

// 用于初始化接收到的Student信息
- (instancetype)initStudentInfo:(NSDictionary *)studentInfo;

设置了基本的属性方法

#import "Student.h"

@implementation Student
- (void)dealloc
{
    [_name release];
    [_sex release];
    [_introduce release];
    [_hobby release];
    [_photo release];
    [_phone release];
    [super dealloc];
}
- (instancetype)initWithName:(NSString *)name sex:(NSString *)sex introduce:(NSString *)introduce hoby:(NSString *)hoby phone:(NSString *)phone photo:(NSString *)photo
{
    self = [super init];
    if (self) {
        self.name = name;
        self.sex = sex;
        self.introduce = introduce;
        self.hobby = hoby;
        self.phone = phone;
        self.photo = photo;
    }
    return self;
}
// 遍历构造器方法
+ (Student *)studentWithName:(NSString *)name sex:(NSString *)sex introduce:(NSString *)introduce hoby:(NSString *)hoby phone:(NSString *)phone photo:(NSString *)photo
{
    Student *student = [[Student alloc]initWithName:name sex:sex introduce:introduce hoby:hoby phone:phone photo:photo];
    return [student autorelease];
}
// 自定义了一个初始化方法通过字典的方式获得
- (instancetype)initStudentInfo:(NSDictionary *)studentInfo
{
    self = [super init];
    if (self) {
        self.name = [studentInfo objectForKey:@"name"];
        self.sex = [studentInfo objectForKey:@"sex"];
        self.introduce = [studentInfo objectForKey:@"introduce"];
        self.hobby = [studentInfo objectForKey:@"hobby"];
        self.phone = [studentInfo objectForKey:@"phone"];
        self.photo = [studentInfo objectForKey:@"photo"];
    }
    return self;
}
@end

第二部初始化自己定义的一个UItableViewController对象

@interface StudentsViewController : UITableViewController

@property (nonatomic, retain) NSMutableArray *students; // 定义一个区的数组

@end

#import "StudentsViewController.h"
#import "Student.h"
#import "StudentCell.h"
@interface StudentsViewController ()

@end

@implementation StudentsViewController
- (void)dealloc
{
    [_students release];
    [super dealloc];
}
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        [self setupStudents];
    }
    return self;
}


- (void)setupStudents
{
    self.students = [NSMutableArray arrayWithCapacity:8];
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"Students" ofType:@"plist"];
    NSArray *arr = [NSArray arrayWithContentsOfFile:filePath];
    for (NSDictionary *dic in arr) {
        Student *student = [[Student alloc]initStudentInfo:dic];
        [_students addObject:student];
        [student release];
    }
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return [self.students count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    StudentCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[[StudentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
    }
    Student *student = [self.students objectAtIndex:indexPath.row];
    [cell setStudent:student];
    return cell;
}

// 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 story board-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.
}

 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 返回指定学生的高度
    return [StudentCell heightForStudent:[self.students objectAtIndex:indexPath.row]];
}
@end

#import <UIKit/UIKit.h>
#import "Student.h"
@interface StudentCell : UITableViewCell

@property (nonatomic, retain) UILabel *nameLabel;  // 名字标签
@property (nonatomic, retain) UILabel *sexLabel;   // 性别标签
@property (nonatomic, retain) UILabel *phoneLabel; // 电话标签
@property (nonatomic, retain) UILabel *hobbyLabel; // 爱好标签
@property (nonatomic, retain) UILabel *introduceLabel; // 爱好标签

@property (nonatomic , retain) UIImageView * photoImageView; // 图片

/**
 *  设置setStudent方法为自定义的对象赋值
 *
 *  @param student 传入的需要显示的对象
 */

- (void)setStudent:(Student *)student;
/**
 *  通过类方法调整高度显示
 *
 *  @param student 传入的student对象信息
 *
 *  @return 返回Student实例对象信息
 */

+ (CGFloat)heightForStudent:(Student *)student;

@end

#import "StudentCell.h"


#define KLeftPadding 10  // 距离最左边的距离
#define KTopPadding 10   // 距离顶部的距离
#define LLeftRightSpacing 10 // 水平方向的间距
#define KupDownSpacing 10  // 垂直方向的间距
#define KLabelHeight 20   // 普通Label的高度

@implementation StudentCell
- (void)dealloc
{
    [_nameLabel release];
    [_sexLabel release];
    [_phoneLabel release];
    [_hobbyLabel release];
    [_photoImageView release];
    [super dealloc];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        _photoImageView = [[UIImageView alloc]initWithFrame:CGRectMake(KLeftPadding, KTopPadding, 100, 150)];
        [self.contentView addSubview:_photoImageView];
        
        _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(KLeftPadding + 100 + LLeftRightSpacing, KTopPadding, 200, KLabelHeight)];
        [self.contentView addSubview:_nameLabel];
        
        _sexLabel = [[UILabel alloc]initWithFrame:CGRectMake(KLeftPadding + 100 + LLeftRightSpacing , KTopPadding + ( KLabelHeight + KupDownSpacing ), 200, KLabelHeight)];
        [self.contentView addSubview:_sexLabel];
        
        _phoneLabel = [[UILabel alloc]initWithFrame:CGRectMake(KLeftPadding + 100 + LLeftRightSpacing, ( KLabelHeight + KupDownSpacing) * 2 + KTopPadding , 200, KLabelHeight)];
        [self.contentView addSubview:_phoneLabel];
        
        _hobbyLabel = [[UILabel alloc]initWithFrame:CGRectMake(KLeftPadding + 100 + LLeftRightSpacing, (KLabelHeight + KupDownSpacing) * 3 + KTopPadding, 200, KLabelHeight)];
        [self.contentView addSubview:_hobbyLabel];
        
        _introduceLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        
        // 通过其设置多行显示功能
        _introduceLabel.numberOfLines = 0;
        // 用来设置字体的大小
        _introduceLabel.font = [UIFont systemFontOfSize:10];
        [self.contentView addSubview:_introduceLabel];
        
    }
    return self;
}
- (void)setStudent:(Student *)student
{
    // ios7之前(获取文字的空间大小)
    // CGSize size = [_introduceLabel.text sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(200, 10000) lineBreakMode:NSLineBreakByCharWrapping];
    
    
    // 根据内容计算高度
    NSString *introduce = student.introduce;
    // 自己返回字符串的高度
    CGRect bounds = [introduce boundingRectWithSize:CGSizeMake(200, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:10] forKey:NSFontAttributeName] context:nil];
    self.nameLabel.text = student.name;
    self.sexLabel.text = student.sex;
    self.phoneLabel.text = student.phone;
    self.hobbyLabel.text = student.hobby;
    self.photoImageView.image = [UIImage imageNamed:student.photo];
    // 通过其功能设置多行的高度问题
    self.introduceLabel.frame = CGRectMake(KLeftPadding + 100 + LLeftRightSpacing, KTopPadding + (KupDownSpacing + KLabelHeight) * 4 , 200, bounds.size.height);
    self.introduceLabel.text = student.introduce;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}
// 设置总的行高
+ (CGFloat)heightForStudent:(Student *)student
{
    // 根据内容计算高度
    NSString *introduce = student.introduce;
    // 自己返回字符串的高度(ios特有的特性计算字符所占的矩形空间大小)
    CGRect bounds = [introduce boundingRectWithSize:CGSizeMake(200, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:10] forKey:NSFontAttributeName] context:nil];
    
    return KTopPadding + ( KLabelHeight + KupDownSpacing) * 4 + bounds.size.height + KTopPadding  ;
}
@end

以上就是通过数组的方式自定义的一个cell对象

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值