##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{}

##DAY12 UITableViewCell自定义的更多相关文章

  1. iOS学习31之UITableVIewCell自定义

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

  2. iOS:UITableViewCell自定义单元格

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

  3. UITableViewCell自定义

    ⼀.⾃定义Cell     UITableView中系统的cell共提供了四种默认样式,分别是: UITableViewCellStyleDefault UITableViewCellStyleVal ...

  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. WinForm 窗体与窗体相互嵌套

    只要将要被潜逃的的窗体的TopLeve设置为Flase即可像普通的控件一样,被添加到另外一个窗体中,TopLeve:是否为顶级窗口,下面来看代码: public partial class TTFor ...

  2. 排序算法 -- 数据结构与算法的javascript描述 第12章

    排序是常见的功能,给定一组数据,对其进行排序. 在此之前,我们需要准备个基础工作--自动生成数组,并可以对该组数据做任何处理. /** * 测试类 ,数组 * @param numElements * ...

  3. JavaScript引用类型之RegExp类型(正则表达式)

    ECMAScript中使用RegExp来支持正则表达式.使用下面类似Perl的语法,就可以创建一个正则表达式. var expression=/pattern/flags; 如上代码: pattern ...

  4. iOS程序启动原理(简单)

    1.执行main -> 执行UIApplicationMain UIApplicationMain底层实现 1.创建UIApplication对象 2.创建UIApplication代理对象 3 ...

  5. (跨平台)cocos2d-x C++ or Object-C(前端)调用C# webservices(后台),实现交叉编译到Android/IOS/WinPhone等移动终端设备

    1.2014年4月2号算是正式找到自己的实习工作-杭州美迪软件有限公司(移动物联事业部)合作于:四川管家婆总部移动终端代理,由于在校选编程专业语言C#和在浙大网新培训课程(C#.Asp.net开发)缘 ...

  6. Eclipse开发工具学习之道:用Eclipse生成jar文件

    很多人都不知道怎么在Eclipse下生成jar文件,或者生成了jar文件后又老是用不了,总是会收到 Exception in thread "main" java.lang.NoC ...

  7. sheelエラー、オブジェクトを解析中にエラーが発生しました。

  8. nodejs 文件查找中文,替换为英文

    帮以前同事解决一个需求,中文项目 翻译 英文项目~~~ 考虑到具体实现方面的问题,如果智能的话,肯定是要做中文的语法分析,不过感觉这个有难度. 所以最后的方案是遍历文件,将中文短语匹配出来,再进行人工 ...

  9. echarts 地图与时间轴混搭

    //常量定义public class Constant { public static Integer PM_YEAR_NO = 5; } //action public class ZhiBiaoP ...

  10. OC中NSArray的使用

    不可变数组类容器类,管理一组对象类型的数据.   元素是有序的,索引值从0开始  数组中存储的元素必须是对象,类型任意.   创建数组对象,使⽤用实例初始化或便利构造器.获取元素个数.根据索引值获取对 ...