1. 自定义Cell

 1> 为什么要自定义Cell

  • UITableView 中系统的Cell共提供了四种默认样式,  分别是:

    UITableViewCellStyleDefault

    UITableViewCellStyleValue1

    UITableViewCellStyleValue2  

    UITableViewCellStyleSubtitle

  但是在实际使用过程中, Cell样式的布局上千差万别, 因此我们需要自定义Cell

  • 在前期我们学过自定义视图, 即创建一个类继承于 UIView 或者 其他的视图, 在自定义类中创建其子视图, 这样就会形成一个新的自定义视图。

  • 系统提供的cell满足不了复杂的样式,因此: 定义Cell和自定义视图一样,自己创建一种符合我们需求的Cell并使用这个Cell。

 2> 自定义Cell的步骤

  • 创建一个类继承于 UITableViewCell

  • 实现 UITableViewCell 的初始化方法:

 // 初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
//初始化子视图
[self initLayout];
}
return self;
} //初始化子视图布局方法
- (void)initLayout { //1.头像
self.headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(, , , )]; self.headerImageView.layer.masksToBounds = YES; self.headerImageView.layer.cornerRadius = CGRectGetWidth(self.headerImageView.frame) / ; // cell提供一个contactVIew属性,专门用来自定义cell,防止在cell布局的时候发生布局混乱,如果是自定义cell,记得将子控件添加到contactVIew上
[self.contentView addSubview:self.headerImageView]; //2.姓名
self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.headerImageView.frame) + , CGRectGetMinY(self.headerImageView.frame), , )]; [self.contentView addSubview:self.nameLabel]; //3.电话号码
self.phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.nameLabel.frame), CGRectGetMaxY(self.nameLabel.frame) + , , )]; [self.contentView addSubview:self.phoneLabel];
}

contentView 是 Cell 提供一个属性,专门用来自定义 Cell ,防止在 Cell 布局的时候发生布局混乱,如果是自定义 Cell,记得将子控件添加到 contactVIew 上

效果图:

  • 确保所有的你想添加的子视图都在自定义 Cell 的初始化方法中创建, 由于 UITableView 的重用机制, 一个 Cell 在第一次创建成功并用于下一次显示的时候,不会再走初始化方法, 这样可以避免子视图的重复创建。
  • 在 Cell 的子视图创建成功后,将子视图设置为属性,类似于 UITableViewCell 所自带的 textLabel 和 detailTextLabel 属性。便于在 UITableView 的协议中给自定义视图赋值。

2. 多种Cell混合使用

 1> 概述

  在 TableView 中不仅仅只显示一种 Cell , 多种 Cell 可以并存

 2> 使用场景

  一个重用标识符只能针对于一种 Cell 样式, 不同的 Cell 需要基于不同的重用标识符来进行区分, 重用标识符的区分需要根据不同的情况来划分, 如:

  • model 属性划分(不同的数据内容,比如一个数据中有 type 字段, 为1代表图片类型, 为0代表文字类型)
 Model *model = [self.tableArray objectAtIndex:indexPath.row];
//根据model属性划分
if (model.type == ) {
FirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:firstIdentify];
return cell;
}
if (model.type == ) {
SecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:secondIdentify];
return cell;
}
  • 固定的行显示的 Cell 类型不一样
 // 第一行显示第一种Cell
if (indexPath.row == ) {
FirstTableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:firstIdentify];
return cell;
}
// 第二行显示第二种Cell
if (indexPath.row == ) {
SecondTableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:secondIdentify];
return cell;
}

3. Cell自适应高度

 1> 自适应高度的两种应用

  ① 文本自适应高度:根据文本内容设定Label高度

   第一步: 修改 Label 的高度(在 - (void)layoutSubView 方法中 )

 - (void)layoutSubviews {

     // 1. 获取文本高度
CGFloat textHeight = [Tool textHeightWithText:self.myLabel.text font:[UIFont systemFontOfSize:]];
// 2.修改label的frame
self.myLabel.frame = CGRectMake(, , self.bounds.size.width, textHeight);
}    

   获取文本高度的方法(一般封装在工具类中)

 // 计算文本高度
+ (CGFloat)textHeightWithText:(NSString *)text font:(UIFont *)font { // iOS7.0中求文本高度的方法,返回一个CGRect的高度 // 第一个参数
CGSize size = CGSizeMake([UIScreen mainScreen].bounds.size.width, ); // 第二个参数:设置以行高为单位
CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : font} context:nil]; return rect.size.height;
}

  第二步:设置 Cell 的高度

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

     CGFloat textHeight = [Tool textHeightWithText:self.myModel.textString font:[UIFont systemFontOfSize:]];

     return textHeight;
}

   ② 图片自适应高度:根据图片宽度进行等比例缩放

  第一步:修改imageView的高度

 - (void)layoutSubviews {
// 1.获取图片的高度
CGFloat imageH = [Tool imageHeightWithImage:self.myImageView.image];
// 2.修改imageView的高度
self.myImageView.frame = CGRectMake(, textHeight, [UIScreen mainScreen].bounds.size.width, imageH);
}

  获取图片高度的方法(一般封装在工具类中)

 + (CGFloat)imageHeightWithImage:(UIImage *)image {

     // 获取图片的宽度和高度
CGFloat width = image.size.width;
CGFloat height = image.size.height;
// 计算图片高度
float scile = height / width;
float screenH = [UIScreen mainScreen].bounds.size.width; return scile * screenH;
}

  第二步:设置 Cell 的高度

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

    CGFloat imageHeight = [Tool imageHeightWithImage:self.myModel.image];

    return imageHeight;
}

  在上面 获取图片高度的方法 中存在一个 bug , 当计算图片高度的数据类型为CGFloat时, 对于某些图片就会出现程序崩溃

  错误代码:

 + (CGFloat)imageHeightWithImage:(UIImage *)image {

     // 获取图片的宽度和高度
CGFloat width = image.size.width;
CGFloat height = image.size.height;
// 计算图片高度
CGFloat scile = height / width;
CGFloat screenH = [UIScreen mainScreen].bounds.size.width; return scile * screenH;
}

  错误信息:

  ③ 两种使用同时出现

   其他部分全部相同,处理在第二步设置Cell的高度, 代码如下:

 // 设置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// 得到文字自适应高度
CGFloat textHeight = [Tool textHeightWithText:self.myModel.textString font:[UIFont systemFontOfSize:]];
// 得到图片自适应高度
CGFloat imageHeight = [Tool imageHeightWithImage:self.myModel.image];
// 返回两者的和
return textHeight + imageHeight;
}

   

iOS学习31之UITableVIewCell自定义的更多相关文章

  1. iOS学习笔记-自定义过渡动画

    代码地址如下:http://www.demodashi.com/demo/11678.html 这篇笔记翻译自raywenderlick网站的过渡动画的一篇文章,原文用的swift,由于考虑到swif ...

  2. iOS学习之自定义弹出UIPickerView或UIDatePicker(动画效果)

    前面iOS学习之UIPickerView控件的简单使用 用到的UIPickerView弹出来是通过 textField.inputView = selectPicker;   textField.in ...

  3. iOS学习资料整理

    视频教程(英文) 视频 简介 Developing iOS 7 Apps for iPhone and iPad 斯坦福开放教程之一, 课程主要讲解了一些 iOS 开发工具和 API 以及 iOS S ...

  4. iOS 学习

    iOS 学习资料 (适合初学者) 本文资料来源于GitHub 一.视频教程(英文) Developing iOS 7 Apps for iPhone and iPad斯坦福开放教程之一, 课程主要讲解 ...

  5. iOS 学习资料汇总

    (适合初学者入门) 本文资料来源于GitHub 一.视频教程(英文) Developing iOS 7 Apps for iPhone and iPad斯坦福开放教程之一, 课程主要讲解了一些 iOS ...

  6. iOS开发UI篇—UITableviewcell的性能问题

    iOS开发UI篇—UITableviewcell的性能问题 一.UITableviewcell的一些介绍 UITableView的每一行都是一个UITableViewCell,通过dataSource ...

  7. [置顶] iOS学习笔记47——图片异步加载之EGOImageLoading

    上次在<iOS学习笔记46——图片异步加载之SDWebImage>中介绍过一个开源的图片异步加载库,今天来介绍另外一个功能类似的EGOImageLoading,看名字知道,之前的一篇学习笔 ...

  8. IOS学习笔记48--一些常见的IOS知识点+面试题

      IOS学习笔记48--一些常见的IOS知识点+面试题   1.堆和栈什么区别? 答:管理方式:对于栈来讲,是由编译器自动管理,无需我们手工控制:对于堆来说,释放工作由程序员控制,容易产生memor ...

  9. iOS学习笔记20-地图(二)MapKit框架

    一.地图开发介绍 从iOS6.0开始地图数据不再由谷歌驱动,而是改用自家地图,当然在国内它的数据是由高德地图提供的. 在iOS中进行地图开发主要有三种方式: 利用MapKit框架进行地图开发,利用这种 ...

随机推荐

  1. 常用shell命令操作

    1.找出系统中所有的*.c 和*.h 文件 (-o 或者) $find / -name "*.cpp" -o -name "*.h" 2.设定 eth0 的 I ...

  2. jquery文件上传控件 Uploadify 问题记录

    Uploadify v3.2.1 首先引用下面的文件 <!--上传控件 uploadify--> <script type="text/javascript" s ...

  3. 公众号开发——测试工具【ngrok】

    工具下载:ngrok 目录清单: ngrok.exe ngrok.cfg run.bat 点击bat启动. 可修改域名,右键bat文件修改. 成功效果图: 注:80端口被占用了怎么办?    ——   ...

  4. Linux USB Project

    转自:http://www.linux-usb.org/ Welcome to the home of the Linux USB Project This web site was created ...

  5. CSS3学习

    1.CSS3边框 border-radius:创建圆角边框 border-radius:25px; -moz-border-radius:25px; /* 老的 Firefox */ box-shad ...

  6. python多线程之Condition(条件变量)

    #!/usr/bin/env python # -*- coding: utf-8 -*- from threading import Thread, Condition import time it ...

  7. 【openGL】关于画点

    #include "stdafx.h" #include <GL/glut.h> #include <stdlib.h> #include <math ...

  8. rhel7初体验

    Redhat7界面明显比之前的版本华丽了不少,貌似Redhat对普通用户的使用也要进行普及 可以在安装的同时修改root密码和创建新用户

  9. flume-ng 集群搭脚本

    #!/bin/bash # author: xirong # date : -- ##### 搭建 flume 集群的脚本 # 注意: # . 需要 jdk7 环境,如果没有 Java 环境,请配置 ...

  10. JqueryEasyUI 解决IE下加载时页面错乱的问题 分类: JavaScript JqueryEasyUI 2014-09-20 09:50 545人阅读 评论(1) 收藏

    问题描述: 一直觉得jqueryeasyui在IE下的渲染效果不大好,尤其刚进入页面时的加载,页面会出现布局错乱,虽然是一闪而过,但是给用户的体验不好: 可以通过在页面onload时,增加一个遮罩层, ...