不等高的cell

给模型增加frame数据

  • 所有子控件的frame
  • cell的高度
@interface XMGStatus : NSObject
/**** 文字\图片数据 ****/
// ..... /**** frame数据 ****/
/** 头像的frame */
@property (nonatomic, assign) CGRect iconFrame;
// .....
/** cell的高度 */
@property (nonatomic, assign) CGFloat cellHeight;
@end
  • 重写模型cellHeight属性的get方法
- (CGFloat)cellHeight
{
if (_cellHeight == 0) {
// ... 计算所有子控件的frame、cell的高度
}
return _cellHeight;
}

在控制器中

  • 实现一个返回cell高度的代理方法

    • 在这个方法中返回indexPath位置对应cell的高度
/**
* 返回每一行cell的具体高度
*/
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
XMGStatus *status = self.statuses[indexPath.row];
return status.cellHeight;
}
  • 给cell传递模型数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"tg";
// 访问缓存池
XMGStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 设置数据(传递模型数据)
cell.status = self.statuses[indexPath.row]; return cell;
}

新建一个继承自UITableViewCell的子类,比如XMGStatusCell

@interface XMGStatusCell : UITableViewCell
@end

在XMGStatusCell.m文件中

  • 重写-initWithStyle:reuseIdentifier:方法

    • 在这个方法中添加所有需要显示的子控件
    • 给子控件做一些初始化设置(设置字体、文字颜色等)
/**
* 在这个方法中添加所有的子控件
*/
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
// ......
}
return self;
}

在XMGStatusCell.h文件中提供一个模型属性,比如XMGTg模型

@class XMGStatus;

@interface XMGStatusCell : UITableViewCell
/** 团购模型数据 */
@property (nonatomic, strong) XMGStatus *status;
@end

在XMGTgCell.m中重写模型属性的set方法

  • 在set方法中给子控件设置模型数据
- (void)setStatus:(XMGStatus *)status
{
_status = status; // .......
}

重写-layoutSubviews方法

  • 一定要调用[super layoutSubviews]
  • 在这个方法中设置所有子控件的frame
/**
* 在这个方法中设置所有子控件的frame
*/
- (void)layoutSubviews
{
[super layoutSubviews]; // ......
}

iOS-UI控件之UITableView(二)- 自定义不等高的cell的更多相关文章

  1. iOS UI控件继承关系图

    闲来无事,把UI控件的继承关系图整理下来,供自己和大家使用.

  2. iOS开发——UI进阶篇(三)自定义不等高cell,如何拿到cell的行高,自动计算cell高度,(有配图,无配图)微博案例

    一.纯代码自定义不等高cell 废话不多说,直接来看下面这个例子先来看下微博的最终效果 首先创建一个继承UITableViewController的控制器@interface ViewControll ...

  3. iOS-UI控件之UITableView(三)- 自定义不等高的cell

    Storyboard_不等高 对比自定义等高cell,需要几个额外的步骤(iOS8开始才支持) 添加子控件和contentView之间的间距约束 设置tableViewCell的真实行高和估算行高 / ...

  4. ios UI控件的简单整理

    把该文件拷贝到.m文件中就能够方便的查找 /** 匿名类目:能够声明方法和变量,属性为private(不同意在外部调用,且不能被继承 */ /** 发送数据的托付方,接收数据的时代理发(即代理的反向传 ...

  5. iOS UI控件总结(全)

    1.UIButton UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame = CGRectMake ...

  6. UI控件篇——UIPageControl及其自定义

    UIPageControl类提供一行点来指示当前显示的是多页面视图的哪一页.当然,由于UIPageControl类可视样式的点击不太好操作,所以最好是确保再添加了可选择的导航选项,以便让页面控件看起来 ...

  7. UI控件之UITableView的基本属性

    UITableView:特殊的滚动视图,横向固定,可以在纵向上滚动,自动计算contentSize 创建tableView,初始化时指定样式,默认是plain UITableView *_tableV ...

  8. UI控件之UITableView的协议方法

    <UITableViewDataSource,UITableViewDelegate> //设置表视图的编辑状态 -(void)setEditing:(BOOL)editing anima ...

  9. iOS UI控件

    创建: 2018/04/21 完成: 2018/04/25 更新: 2018/09/24 补充UIActivityIndicatorView的显示和隐藏方法 UIButton  设定项目  项目名   ...

随机推荐

  1. Educational Codeforces Round 17 D. Maximum path DP

    题目链接:http://codeforces.com/contest/762/problem/D 多多分析状态:这个很明了 #include<bits/stdc++.h> using na ...

  2. BZOJ1016 &amp;&amp; JSOI2008] 最小生成树计数

    题目链接:id=1016">点击打开链接 裸题 #pragma comment(linker, "/STACK:1024000000,1024000000") #i ...

  3. HDU1045 Fire Net —— 二分图最大匹配

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others)  ...

  4. YTU 1005: 渊子赛马

    1005: 渊子赛马 时间限制: 1000 Sec  内存限制: 64 MB 提交: 338  解决: 49 题目描述 赛马是一古老的游戏,早在公元前四世纪的中国,处在诸侯割据的状态,历史上称为&qu ...

  5. ubuntu删除ppa源

    cd /etc/apt/sources.list.d 都在这里了 drwxr-xr-x 2 root root 4096 5月 22 23:41 ./ drwxr-xr-x 6 root root 4 ...

  6. 【ZJOI 2008】 生日聚会

    [题目链接] 点击打开链接 [算法] 动态规划 f[i][j][x][y]表示当前选了i个男生,j个女生,男生与女生差最大为x,女生与男生差最大为y的方案数 转移很显然,笔者不再赘述 [代码] #in ...

  7. 【转】浏览器中输入url后发生了什么

    原文地址:http://www.jianshu.com/p/c1dfc6caa520 在学习前端的过程中经常看到这样一个问题:当你在浏览器中输入url后发生了什么?下面是个人学习过程中的总结,供个人复 ...

  8. softmax regression in c++

    #include <iostream>#include <vector>#include <cmath>#include <algorithm>#inc ...

  9. UI:使用 pod 引入 AFNetworking

    cocdpods的安装  参考1  参考2 参考3 注意:MVC是一种搭建项目的思想,不是设计模式. 使用第三方管理控件: 引入CocoaPods的详细步骤:(1)检测有没有引入淘宝镜像gem sou ...

  10. javascript前端面试题及答案整理

    Part1 手写代码 现场手写代码是现在面试中很常见的一类面试题,考察基础的数据结构与算法能力. 1 数组去重的实现 基本数组去重 Array.prototype.unique = function( ...