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的 - ( ...
随机推荐
- HDU_2888_Check Corners
Check Corners Time Limit: 2000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- tcp/udp/socket 端口映射,转发小工具
1) 利用 Python 的 Socket 端口转发,用于远程维护 https://github.com/knownsec/rtcp 2) 反向端口转发工具 Reverse TCP Port to U ...
- 一、Mosquitto 介绍&安装
一.Mosquitto 介绍 一款实现了消息推送协议 MQTT v3.1 的开源消息代理软件,提供轻量级的,支持可发布/可订阅的的消息推送模式,使设备对设备之间的短消息通信变得简单,比如现在应用广泛的 ...
- redis.conf配置文件说明
# Redis 配置文件 # 当配置中需要配置内存大小时,可以使用 1k, 5GB, 4M 等类似的格式,其转换方式如下(不区分大小写) # # 1k => 1000 bytes # 1kb = ...
- Android 压力测试工具Monkey
原文地址http://www.syhm52.com/tools/17.html 一.Monkey定义探索软件测试工具有哪些,本文主要介绍Monkey工具.Monkey测试是Android平台自动化测试 ...
- C++ string 类
标准c++中string类函数介绍 注意不是CString之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必 担心内存是否足够.字符串长度等等,而且作为 ...
- Codeforces Round #523 (Div. 2) Solution
A. Coins Water. #include <bits/stdc++.h> using namespace std; int n, s; int main() { while (sc ...
- 2018 China Collegiate Programming Contest Final (CCPC-Final 2018)
Problem A. Mischievous Problem Setter 签到. #include <bits/stdc++.h> using namespace std; #defin ...
- FM/AM收音机原理
收音机这东西很早就开始用了,但一直都没有了解过它的原理,听说是很简单.下面记录一些笔记. 1. 基本概念 收音机是一种小型的无线电接收机,主要用于接受无线电广播节目,收听无线电发射台.首先说一下收音机 ...
- 实现Vue-MVVM-step1
一个利用defineProperty实现的MVVM双向数据绑 <!DOCTYPE html> <html lang="en"> <head> & ...