##DAY12 UITableViewCell自定义

##DAY12 UITableViewCell自定义

#pragma mark -------自定义视图步骤---------

自定义视图步骤:

1)在自定义cell类中,将所有cell要显示的子视图控件都声明成属性

2)重写cell的初始化方法,对内部控件进行布局,frame指定为0(CGRectZero),将控件添加到cell上面进行显示,一定要注意使用self.contentView添加;

//自定义cell内部添加子视图,不能使用self,应该使用self.contentView

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

3)在cell内部,重写layoutSubViews方法(先向父类的此方法发送消息),给定内部控件的具体位置;

4)建立模型类,设置属性、异常处理;

//内部什么都不做,异常处理,解决赋值个数不匹配的问题

- (void)setValue:(id)value forUndefinedKey:(NSString *)key{

    if ([key isEqualToString:@"id"]) {

        self.ID = value;

    }

    if ([key isEqualToString:@"description"]) {

        self.descriptions = value;

    }

}

5)在cell内部导入模型,将模型设置成属性;

6)在cell内部,重写模型属性的setter方法,内部使用模型为内部控件完成赋值;

//在cell内部绑定一个模型属性

//重写模型的setter方法,完成赋值

- (void)setStudent:(Student *)student{

    if(_student != student){

        [_student release];

        _student = [student retain];

    }

    //为内部控件进行赋值,如果写在里面,当数据相同时,第二个cell就没有被赋值

    _headerImageView.image = [UIImage imageNamed:_student.picture];

    _nameLabel.text = _student.name;

    _genderLabel.text = _student.gender;

    _ageLabel.text = _student.age;

    }

7)内存管理,自定义cell类,模型类中释放属性。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *reuseIdentifier = @"reuse";

    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];

    if (cell == nil) {

        cell = [[[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier] autorelease];

    }

    Student *student = _dataArr[indexPath.row];

    cell.student = student;//简化

    return cell;

}

#pragma mark ———cell自适应高度———

#define kWidth [[UIScreen mainScreen] bounds].size.width//宏定义会直接替换,类方法中不能使用self.view

#define kImageWidth ((kWidth - 30) / 4)

//求一段文本的显示高度

+ (CGFloat)heightForString:(NSString *)string {

    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];

    //下面的方法会根据参考宽度和字体的size计算出一个宽度返回出去,这里的CGSizeMake()中的两个参数,第一个是参考宽度,;第二个参数是返回的最大高度

    //kImageWidth 即 (([[UIScreen mainScreen] bounds].size.width-30)/4)宏定义会直接替换,类方法中不能使用self.view

    return [string boundingRectWithSize:CGSizeMake(3 * kImageWidth, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:diccontext:nil].size.height;

}

//返回cell的高度

+ (CGFloat)cellHeightForStudent:(Student *)student {

    return 65 + [BoyTableViewCell heightForString:student.introduce] > 120 ? 65 + [BoyTableViewCell heightForString:student.introduce] : 120 ;

}

- (void)layoutSubviews {

    [super layoutSubviews];

    CGFloat imageWidth = (kWidth - 30) / 4;

    _headerImageView.frame = CGRectMake(10, 5, imageWidth, 110);

    _introduceLabel.frame = CGRectMake(20 + imageWidth, 65, 3 * imageWidth, [BoyTableViewCell heightForString:_introduceLabel.text]);

}

#pragma mark ------MyTableViewController.m-------

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    Student *stu = _dataArr[indexPath.row];

    if ([stu.sex isEqualToString:@"男"]) {

        return [BoyTableViewCell cellHeightForStudent:stu];

    }else if([stu.sex isEqualToString:@"女"]){

        return  [GirlTableViewCell cellHeightForStudent:stu];

    }

    return 270;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{}

原文地址:https://www.cnblogs.com/chongyu/p/5192440.html