static cell 与 dynamic cell 混合使用

关于静态cell与动态cell的混合使用,google一下便会有很多相关文章,这里也是看过一些前辈的经验(已经忘记具体是从哪篇文章得到的帮助)之后自己做的笔记。

在某些界面中static cell与dynamic cell混合使用会事半功倍,比如手机上的Wi-Fi功能等,效果图如下:

Wi-Fi

Wi-Fi界面的第一组与第三组的行数都是固定的,且布局并不相同,类似这样的布局使用static cell很快就可以完成,然而第二组却需要使用dynamic cell。这时候就会用到静态cell与动态cell混合使用的情况。

首先,在Storyboard中添加一个UITableViewController,设置为Static cell,并设置好第一组与第三组的内容,第二组需要设置一个空的Cell,如图:

UITableViewController

然后,给第二组的空白Cell设置identifier值,如图:

设置identifier

然后,第二组需要自定义cell,这里使用XIB来完成,如图:

dynamic cell

接下来需要用代码注册Nib,如下:

 let nib = UINib(nibName: "WiFiTableViewCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "WiFiTableViewCell")

最后,为了让 static cell 与 dynamic cell 能够混合使用,需要实现TableView的代理,代码如下:

    // 以下代理方法必须实现
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.section == {
let cell = tableView.dequeueReusableCellWithIdentifier("WiFiTableViewCell") as? WiFiTableViewCell
return cell!
}
return super.tableView(tableView, cellForRowAtIndexPath: indexPath)
} override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == {
return //这里返回第二组的行数
}
return super.tableView(tableView, numberOfRowsInSection: section)
} override func tableView(tableView: UITableView, indentationLevelForRowAtIndexPath indexPath: NSIndexPath) -> Int {
if indexPath.section == {
return super.tableView(tableView, indentationLevelForRowAtIndexPath: NSIndexPath(forRow: , inSection: ))
}
return super.tableView(tableView, indentationLevelForRowAtIndexPath: indexPath)
} override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.section == {
return
}
return super.tableView(tableView, heightForRowAtIndexPath: indexPath)
}

值得注意的是:

  • 在storyboard中需要给第二组设置一行空的Cell,并设置identifier值。
  • 在代码中也需要注册Cell
  • 上面提到的代理方法必须实现

最后,本文主要是记录关于 static cell 与 dynamic cell 的混合使用,因此很多细节问题并没有处理,更多关于static cell另外的一些使用,可以点击这里【更优雅地使用Static Cell】

【Demo下载】

文/Chakery(简书作者)
原文链接:http://www.jianshu.com/p/c3645c13af4b
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

IOS-static cell 与 dynamic cell 混合使用的更多相关文章

  1. iOS学习之UI自定义cell

    一.自定义Cell 为什么需要自定义cell:系统提供的cell满足不了复杂的样式,因此:自定义Cell和自定义视图一样,自己创建一种符合我们需求的Cell并使用这个Cell.如下图所示的这些Cell ...

  2. ios之UI中自定义cell

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  3. iOS学习之UITableView中Cell的操作

    接着iOS学习之Table View的简单使用 这篇,这里主要讲UITableView 中的Cell的操作,包括标记.移动.删除.插入. 为了简单快捷,直接从原来那篇的代码开始,代码下载地址:http ...

  4. iOS开发UITableView的动画cell

    1.动画cell 针对cell的动画,在Delegate中对cell的layer进行操作: 2.实现代码 #import "ViewController.h" #import &q ...

  5. iOS开发总结-UITableView 自定义cell和动态计算cell的高度

    UITableView cell自定义头文件:shopCell.h#import <UIKit/UIKit.h>@interface shopCell : UITableViewCell@ ...

  6. ios 获取button所在的cell对象, 注意:ios7 =&lt; System Version &lt; ios8 获取cell对象的差别

    ios7 =< System Version< ios8 : ios7 =< System Version < ios8  下 button.superview.supervi ...

  7. UITableView错误 ‘unable to dequeue a cell with identifier Cell'

    - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier; - (id)dequeueReusableCellWithIdentif ...

  8. reason: 'unable to dequeue a cell with identifier Cell

    今天在cell重用的时候出现一下错误 reason:  'unable  to  dequeue  a  cell  with  identifier  Cell  -  must  register ...

  9. IOS开发之UITabBarController与UINavigationController混合使用

    ios开发中UITabBarController与UINavigationController混合使用是很多app的基础页面结构,下面是简单的的页面初始化的方法,在AppDelegate.m的 - ( ...

随机推荐

  1. flask中secret_key的作用

    https://segmentfault.com/q/1010000007295395

  2. CF734F Anton and School 构造+数论

    正解:构造 解题报告: 先放下传送门QwQ 这题首先要知道一个结论:(x&y)+(x|y)=x+y 还是能理解的趴? 所以我们把bi+ci就能得到∑a+n*a[i] 然后我们就能成功求出∑a ...

  3. Unity-DLL反编译学习

        本文由博主(SunboyL)原创,转载请注明出处:http://www.cnblogs.com/xsln/p/DLL_DeCompilation.html     在Unity实际开发过程中, ...

  4. Xshell连接阿里云Centos6.8

    由于我将xshell更新到了Xshell v5.0 Build 1332,在连接阿里云服务器的时候遇到了一些问题. 以前我登录到服务器的时候直接输入登录密码就行了,但是现在由于openssh服务器类型 ...

  5. SQL调优学习之——sqlserver分页从低效到高效

    背景 首先感谢网友@aixuexi 在评论中的提醒,原博文介绍的几种都不是最高效,现已修改加入另一种更高效的方法. 以前都是使用mysql和oracle,对sqlserver的使用不多.最近因项目原因 ...

  6. MySQL的redo log结构和SQL Server的log结构对比

    MySQL的redo log结构和SQL Server的log结构对比 innodb 存储引擎 mysql技术内幕 log buffer根据一定规则将内存中的log block刷写到磁盘,这个规则是 ...

  7. java-mybaits-00701-与spring整合

    1.1     整合思路   需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSes ...

  8. 二、 Mosquitto 使用说明

    一. 继上一篇文章<<Mosquitto 介绍&安装>> 之后.本章介绍 Mosquitto 的 简单使用. 1> 创建用户 # groupadd mosquit ...

  9. linux rsync安装与使用

    rsync Usage: /etc/init.d/rsync {start|stop|reload|force-reload|restart|status} rsync默认配置文件 # default ...

  10. PR曲线 ROC曲线的 计算及绘制

    在linear model中,我们对各个特征线性组合,得到linear score,然后确定一个threshold,linear score < threshold 判为负类,linear sc ...