⼀、⾃定义Cell
 
 
UITableView中系统的cell共提供了四种默认样式,分别是:
UITableViewCellStyleDefault
UITableViewCellStyleValue1
UITableViewCellStyleValue2
UITableViewCellStyleSubtitle
 
⼆、Model类型对象的使⽤
 
Model类主要是为了给我们提供数据,简单⽽⾔即⾃定义 类且继承于NSObject的称之为Model。⽽继承于UIView的 称之为View类。 
现在我们的数据提供都是存放在数组和字典中,OC中的 KVC就是帮助我们将字典转换为Model类⽽存在的
Model的使用
创建步骤:
• 创建⼀个类并继承于NSObject
• 添加和字典中对应的属性
• 在视图控制器中将字典通过KVC为Model赋值
• 将Model对象添加到数组中并刷新TableView
 
三、多种Cell混合使⽤
 
使用场景
⼀个重⽤标识符只能针对于⼀种Cell样式,不同的Cell需要 基于不同的重⽤标识符来进⾏区分,⽽重⽤标识符的区分需 要根据不同的情况来划分,⽐如:
• model属性划分(不同的数据内容,⽐如⼀个数据中有 type字段,为1代表图⽚类型,为0代表⽂字类型)
Model *model = [self.tableArray
                    objectAtIndex:indexPath.row];
    //根据model属性划分
    if (model.type == 0) {
        FirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:firstIdentify];
        return cell;
    }
   
    if (model.type == 1) {
        SecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:secondIdentify];
        return cell;
    }
• 固定的⾏显⽰的Cell类型不⼀样
// 第⼀⾏显⽰第⼀种Cell
    if (indexPath.row == 0) {
        FirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:firstIdentify];
        return cell;
    }
    // 第⼆⾏显⽰第⼆种Cell
    if (indexPath.row == 1) {
        SecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:secondIdentify];
        return cell;
    }
 
四、Cell⾃适应⾼度
 
 
  • 文本自适应高度   根据⽂本内容设定Label⾼度
 
NSString *str = @"《纽约时报》称,尽管双⽅可能会在⽓候变化这样的问题上达成共识,但在中美存在争议的议题上,预计主席不会做出什么让步。这些问题包括⺴络间谍、在南海争议海域填海造岛,以及中国对在其境内经营的外国企业和⾮政府组织加紧了控制。";
// 获得字体样式属性
NSDictionary *att = @{NSFontAttributeName:[UIFont systemFontOfSize:17.0]};
// 根据字体样式属性获得⾼度
CGRect rect = [str boundingRectWithSize:CGSizeMake(200, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:att context:nil];
[self.showLabel setFrame:CGRectMake(0, 0, 200, rect.size.height)];
 
  • 图片自适应高度   根据图⽚宽度进⾏等⽐例缩放
UIImage *aImage = [UIImage imageNamed:@"1.png"];
    // 获得图⽚真实⾼度和宽度
    CGFloat height = aImage.size.height;
    CGFloat width = aImage.size.width;
    // 缩放后宽度固定为200
    CGFloat scale = width / 200;
    CGFloat realHeight = height / scale;
    [self.myImageView setFrame:CGRectMake(0,0,200,realHeight)];
 
代码步骤:
//1、自定义cell的第一步,将所有cell要显示的子视图控件声明成属性
@property (nonatomic, retain) UIImageView *headerImageView;//头像
@property (nonatomic, retain) UILabel *nameLabel;//姓名
@property (nonatomic, retain) UILabel *genderLabel;//性别
@property (nonatomic, retain) UILabel *ageLabel;//年龄
 
//2、重写cell的初始化方法,frame给定为0,将控件添加到cell上面进行显示,一定要注意使用self.contentView添加
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _headerImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        _headerImageView.backgroundColor = [UIColor orangeColor];
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _nameLabel.backgroundColor = [UIColor yellowColor];
        _genderLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _genderLabel.backgroundColor = [UIColor cyanColor];
        _ageLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _ageLabel.backgroundColor = [UIColor purpleColor];
        //自定义cell内部添加子视图, 不能使用self,应该是使用sele.contentView
       
        [self.contentView addSubview:_nameLabel];
        [self.contentView addSubview:_genderLabel];
        [self.contentView addSubview:_ageLabel];
        [self.contentView addSubview:_headerImageView];
    }
    return self;
}
 
//3、重写layoutSubviews方法,给定内部控件的具体位置
- (void)layoutSubviews {
    [super layoutSubviews];
    _headerImageView.frame = CGRectMake(5, 5, 50, 80);
    _nameLabel.frame = CGRectMake(65, 5, 100, 20);
    _genderLabel.frame = CGRectMake(65, 35, 100, 20);
    _ageLabel.frame = CGRectMake(65, 65, 100, 20);
}
 
//4、导入模型,将模型与cell绑定,声明模型属性
//在cell内部绑定一个模型属性
@property (nonatomic, retain) Student *modelStu;
 
//5、重写模型的setter方法,内部使用模型为内部控件赋值
- (void)setModelStu:(Student *)modelStu {
    if (_modelStu != modelStu) {
        [_modelStu release];
        _modelStu = [modelStu retain];
        //为内部控件进行赋值
        _headerImageView.image = [UIImage imageNamed:_modelStu.picture];
        _nameLabel.text = _modelStu.name;
        _genderLabel.text = _modelStu.gender;
        _ageLabel.text = _modelStu.age;
    }
}
 
//求一段文本的显示高度
+ (CGFloat)heightForString:(NSString *)string {
   
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];
   
    CGRect rect = [string boundingRectWithSize:CGSizeMake(3 *kImageWidth, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
   
    return rect.size.height;
}

//返回cell的高度
+ (CGFloat)cellHeightForStudent:(Student *)student {
   
    CGFloat totalHeight = 65 + [BoyTableViewCell heightForString:student.introduce];
   
    return  totalHeight > 120 ? totalHeight : 120;

}
 
//6、内存管理
- (void)dealloc {
    [_nameLabel release];
    [_genderLabel release];
    [_ageLabel release];
    [_headerImageView release];
    [_modelStu release];
    [super dealloc];
}
 
 
//在MyTableViewController.m中
 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    //先获取模型
    Student *student = _dataArray[indexPath.row];
    if ([student.sex isEqualToString:@"男"]) {
        return [BoyTableViewCell cellHeightForStudent:student];
    } else if ([student.sex isEqualToString:@"女"]) {
        return [GirlTableViewCell cellHeightForStudent:student];
    }
    return 0;
}

UITableViewCell自定义的更多相关文章

  1. ##DAY12 UITableViewCell自定义

    ##DAY12 UITableViewCell自定义 #pragma mark -------自定义视图步骤--------- 自定义视图步骤: 1)在自定义cell类中,将所有cell要显示的子视图 ...

  2. iOS学习31之UITableVIewCell自定义

    1. 自定义Cell 1> 为什么要自定义Cell UITableView 中系统的Cell共提供了四种默认样式,  分别是: UITableViewCellStyleDefault UITab ...

  3. iOS:UITableViewCell自定义单元格

    UITableViewCell:自定义的单元格,可以在xib中创建单元格,也可以在storyBorad中创建单元格.有四种创建方式 <1>在storyBorad中创建的单元格,它是静态的单 ...

  4. UITableViewCell 自定义绘制 性能高

    // //  FoodListTableViewCellB.m //  BabyFood // //  Created by zhuang chaoxiao on 16/3/7. //  Copyri ...

  5. 纯代码自定义不等高cell

    数据模型.plist解析这里就不过多赘述. 错误思路之一: 通过在heightForRowAtIndexPath:方法中调用cellForRowAtIndexPath:拿到cell,再拿到cell的子 ...

  6. UITableViewCell Property “icon” cannot be found in forward class object “DJWeiBo”

    UITableViewCell 自定义表格 实体属性不显示错误 Property “icon” cannot be found in forward class object “DJWeiBo”引入实 ...

  7. UITabelViewCell自定义(zhuan)

    很多时候,我们需要自定义UITableView来满足我们的特殊要求.这时候,关于UITableView和cell的自定义和技巧太多了,就需要不断的总结和归纳.   1.添加自定义的Cell.   这个 ...

  8. IOS之UI -- UITableView -- 2 -- 等高的Cell

    内容大纲: 1.纯代码 添加子控件 2.Autolayout纯代码 -- Masonry框架的使用 3.自定义等高的cell -- storyboard的使用(更加简单) 4.静态cell 等高的Ce ...

  9. 关于直接创建视图UITableViewController显示(初学)

    今天渣渣想直接创建一个UITableView视图作为根视图来用结果发现有警告,才明白TableView和view是不能直接作为根视图的,需要放在ViewController上.做个笔记详细了解下. 参 ...

随机推荐

  1. 使用uiautomator做UI测试

    转载~~~~~~~~~~~~~~~~~~~~~~~~ 若有侵权,请及时联系本博主,博主将第一时间撤销 在Android 4.1发布的时候包含了一种新的测试工具–uiautomator,uiautoma ...

  2. 使用Jquery解析Json基础知识

    前言 在WEB数据传输过程中,json是以文本,即字符串的轻量级形式传递的,而客户端一般用JS操作的是接收到的JSON对象,所以,JSON对象和JSON字符串之间的相互转换.JSON数据的解析是关键. ...

  3. 整理课堂笔记 pl/sql orcale异常

      1>>>>>异常错误处理 1 >预定义的异常处理 预定义说明的部分 ORACLE 异常错误对这种异常情况的处理,只需在PL/SQL块的异常处理部分,直接引用相应 ...

  4. ubuntu常用命令记录集

    1.查找当前目录下包含某字符串的文件 #find ./ -type f |xargs grep "string" 2.查找文件 #find ./ -name filename 3. ...

  5. Sprint第二个冲刺(第八天)

    一.Sprint介绍 任务进度: 二.Sprint周期 看板: 燃尽图:

  6. css美化checkbox radio样式

    /*check,radio*/ input.check_txt[type='checkbox']{ display: none; } input.check_txt[type='checkbox'] ...

  7. 页面缩放对css的影响

    昨天发现一个上线的项目css样式明显不对,但是查看别人的电脑上的页面样式都是没问题的,于是找了半天原因,原来是我的浏览器对这个页面缩放了,导致样式问题. 发现了页面缩放会作用在同一个域名下的所有页面, ...

  8. 浙江理工2015.12校赛-A

    孙壕请一盘青岛大虾呗 Time Limit: 5 Sec Memory Limit: 128 MB Submit: 577 Solved: 244 Description 话说那一年zstu与gdut ...

  9. [Oracle] SQL*Loader 详细使用教程(3)- 控制文件

    控制文件是SQL*Loader里最重要的文件,它是一个文本文件,用来定义数据文件的位置.数据的格式.以及配置数据加载过程的行为,在sqlldr中以control参数指定控制文件.   在控制文件里配置 ...

  10. php总结 --- 10. xml操作

    xml 和array互换 /** * 数组编码为XML * @param array $data 数据 * @return mixed 编码后数据 */ function xmlencode($dat ...