首页 技术 正文
技术 2022年11月11日
0 收藏 694 点赞 2,403 浏览 1924 个字

static cell 与 dynamic cell 混合使用

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

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

IOS-static cell 与 dynamic cell 混合使用

Wi-Fi

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

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

IOS-static cell 与 dynamic cell 混合使用

UITableViewController

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

IOS-static cell 与 dynamic cell 混合使用

设置identifier

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

IOS-static cell 与 dynamic cell 混合使用

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
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,493
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297