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

Wi-Fi
界面的第一组与第三组的行数都是固定的,且布局并不相同,类似这样的布局使用static cell很快就可以完成,然而第二组却需要使用dynamic cell。这时候就会用到静态cell与动态cell混合使用的情况。
首先,在Storyboard中添加一个UITableViewController,设置为Static cell,并设置好第一组与第三组的内容,第二组需要设置一个空的Cell,如图:

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

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

接下来需要用代码注册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】
原文链接:http://www.jianshu.com/p/c3645c13af4b
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
IOS-static cell 与 dynamic cell 混合使用的更多相关文章
- iOS学习之UI自定义cell
一.自定义Cell 为什么需要自定义cell:系统提供的cell满足不了复杂的样式,因此:自定义Cell和自定义视图一样,自己创建一种符合我们需求的Cell并使用这个Cell.如下图所示的这些Cell ...
- ios之UI中自定义cell
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- iOS学习之UITableView中Cell的操作
接着iOS学习之Table View的简单使用 这篇,这里主要讲UITableView 中的Cell的操作,包括标记.移动.删除.插入. 为了简单快捷,直接从原来那篇的代码开始,代码下载地址:http ...
- iOS开发UITableView的动画cell
1.动画cell 针对cell的动画,在Delegate中对cell的layer进行操作: 2.实现代码 #import "ViewController.h" #import &q ...
- iOS开发总结-UITableView 自定义cell和动态计算cell的高度
UITableView cell自定义头文件:shopCell.h#import <UIKit/UIKit.h>@interface shopCell : UITableViewCell@ ...
- ios 获取button所在的cell对象, 注意:ios7 =< System Version < ios8 获取cell对象的差别
ios7 =< System Version< ios8 : ios7 =< System Version < ios8 下 button.superview.supervi ...
- UITableView错误 ‘unable to dequeue a cell with identifier Cell'
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier; - (id)dequeueReusableCellWithIdentif ...
- reason: 'unable to dequeue a cell with identifier Cell
今天在cell重用的时候出现一下错误 reason: 'unable to dequeue a cell with identifier Cell - must register ...
- IOS开发之UITabBarController与UINavigationController混合使用
ios开发中UITabBarController与UINavigationController混合使用是很多app的基础页面结构,下面是简单的的页面初始化的方法,在AppDelegate.m的 - ( ...
随机推荐
- Linux学习 -->解决Ubuntu系统上 No command 'crond' found
前两天,准备在Ubuntu服务器上,定时执行Gitlab备份的命令,如下所示 编辑 vi /etc/crontab 文件,添加如下定时脚本 # edited by ouyang 2017-8-11 添 ...
- importlib模块与__import__详解
importlib模块与__import__都可以通过过字符串来导入另外一个模块,但在用法上和本质上都有很大的不同. 通过下面示例说明,有如下一个工程目录: name = 'test' def get ...
- mysql 约束条件介绍
mysql 约束条件介绍 约束条件与数据类型的宽度一样,都是可选参数 作用:用于保证数据的完整性和一致性主要分为: PRIMARY KEY (PK) 标识该字段为该表的主键,可以唯一的标识记录 FO ...
- 最长括号化长度 java
1:求最长括号, ()(()()( 例如,它的最长符合括号化的长度为4 package com.li.huawei; import java.util.Arrays; import java.util ...
- PHP指定概率算法
转载来源链接: https://blog.csdn.net/sinat_35861727/article/details/54980807 PHP指定概率算法,可用于刮刮卡,大转盘等抽奖算法. 假设: ...
- POJ2506:Tiling(递推+大数斐波那契)
http://poj.org/problem?id=2506 #include <iostream> #include <stdio.h> #include <strin ...
- redis客户端hiredis
Hiredis 在官网 http://redis.io/clients 中有说明This is the official C client. Support for the whole command ...
- 排序问题Java
package zhuzhuangtu; import java.util.*; import java.io.*; public class Main{ public static void mai ...
- (17)ClippingNode的使用
概述 ClippingNode(裁剪节点)可以用来对节点进行裁剪,可以根据一个模板切割图片的节点,生成任何形状的节点显示. ClippingNode是Node的子类,可以像普通节点一样放入Layer, ...
- 1、安装electron
安装electron安装并非一帆风顺,我有FQ哈,所以网络方面我就不说了,你们不行的话,可以用cnpm,我说的是另一个问题 我是这样解决的,用以下命令就好了 sudo npm install -g e ...