今天在看官方的TableView Guide,突然想起来最近写的一个代码中实现tableViewCell复用的时候有点问题:

var cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: identifer)

cell.textLabel.text = myPeripherals[indexPath.row].name

这里可以看出来,每次调用tableView:cellForRowAtIndexPath: 时都会新建一个cell对象,这样肯定是不好的。

在OC中的正统的实现方式应该是:

先是用dequeueReusableCellWithIdentifier去将队列中已有的cell,则将其取出,重新利用。

如果cell为nil,则重新实例化一个TableViewCell对象,再使用~~~~

所以这样我就像将我有问题的Swift代码改回来,如下:

但是这样会崩溃,不管是写成optional value的形式:

var cell? = tableView.dequeueReusableCellWithIdentifier(identifer) as? UITableViewCell

都会报同样的错误,搞了半天才发现,原来这个错误不是cell == nil导致的,因为“Create new cell”没有被输出过。

出错的原因就在cell.detailTextLabel上出现,说明cell没有被设置成Subtitle的风格。

再去看苹果官方的TableViewSuite Sample Code,中对tableViewCell的调用发现

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *MyIdentifier = @"MyIdentifier";

    /*
Retrieve a cell with the given identifier from the table view.
The cell is defined in the main storyboard: its identifier is MyIdentifier, and its selection style is set to None.
*/
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; // Set up the cell.
NSString *timeZoneName = [self.timeZoneNames objectAtIndex:indexPath.row];
cell.textLabel.text = timeZoneName; return cell;
}

这里也是直接调用dequeueReusableCellWithIdentifier:MyIdentifier函数,没有在实例化一个tableViewCell对象。

这里也对这个进行了一定的说明:

/* Retrieve a cell with the given identifier from the table view. The cell is defined in the main storyboard: its identifier is MyIdentifier, and its selection style is set to None. */
再加上苹果官方TableView Guide文档中写到:

If the dequeueReusableCellWithIdentifier: method asks for a cell that’s defined in a storyboard, the method always returns a valid cell. If there is not a recycled cell waiting to be reused, the method creates a new one using the information in the storyboard itself. This eliminates the need to check the return value for nil and create a cell manually.

就是用dequeueReusableCellWithIdentifier:MyIdentifier函数会一直得到一个有效的在storyboard中定义的cell,而且用storyboard中所定义的信息去设置这个cell,这样就省去了每次去检测cell是否是nil,再去手动的创建。

从这里可以看到,我的代码中出现的错误就是在storyboard中没有将cell设置为subtitle的样式,否则应该不会出错。

所以下面这种方式是最简单的:

var identifer: String = "myCell"
var cell = tableView.dequeueReusableCellWithIdentifier(identifer) as UITableViewCell
cell.textLabel.text = a[indexPath.row].name
cell.detailTextLabel.text = "detail"

但是要注意,这样方式需要在storyboard中设置好cell的一些必要的属性。

如果是这样,那自定义cell又该如何实现呢?

加个你custom的cell类的名字是MyTableViewCell,则实现方法如下

- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyTableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"MyIdentifier"];
cell.firstLabel.text = [NSString stringWithFormat:@"%d", indexPath.row];
cell.secondLabel.text = [NSString stringWithFormat:@"%d", NUMBER_OF_ROWS -
indexPath.row];
return cell;
}

这个是官方提供的OC代码,将其转换成Swift代码就可以了。如下

    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{
var cell: MyTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as MyTableViewCell
cell.first.text = ""
cell.second.text = "" return cell }

Swift TabeleViewCell dequeueReusableCellWithIdentifier 使用的新的细节,原来现在可以这样的更多相关文章

  1. Swift 3.1 的一些新特性

    Swift 3.1 的一些新特性   推荐序 本文来自泊学的投稿,介绍了 Swift 3.1 的新特性,感谢泊学授权发表.以下为文章正文. 正文 Apple 终于发布了Xcode 8.3以及Swift ...

  2. Swift 3到5.1新特性整理

    本文转载自:https://hicc.me/whats-new-in-swift-3-to-5-1/,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有. Hipo 2.0 重写从 Swif ...

  3. iOS8.3发布了Swift 1.2带来哪些新变化

    苹果前几日在面向开发者推送iOS 8.3 Beta的同时,还发布了版本号为6D520o的Xcode 6.3 Beta,其中便包含了iOS 8.3 Beta和OS X v10.10 SDK,并进一步提升 ...

  4. Swift 2.0 到底「新」在哪?

    [编者按]2015年6月,一年一度的苹果 WWDC 大会如期而至,在大会上苹果发布了 Swift 2.0,引入了很多新的特性,以帮助开发者更快.更简单地构建应用.本篇文章作者是 Maxime defa ...

  5. Swift 3必看:新的访问控制fileprivate和open

    在swift 3中新增加了两中访问控制权限 fileprivate和 open.下面将对这两种新增访问控制做详细介绍. fileprivate 在原有的swift中的 private其实并不是真正的私 ...

  6. iOS - Swift Swift 语言新特性

    1.Swift 2.0 带来哪些新变化 常规变化: 1.OS X 10.11.iOS 9 和 watchOS 2 SDK 采纳了一些 Objective-C 的特性用来提高 Swift 的编程体验, ...

  7. iOS开发——新特性篇&swift新特性(__nullable和__nonnull)

    swift新特性(__nullable和__nonnull) 最近在看老师写代码的时候经常遇到两个陌生的关键字,但是当我在我的电脑上敲得时候就是敲不出,后来才知道这是为了swift与OC混编的时候产生 ...

  8. iOS开发——新特性OC篇&Swift 2.0新特性

    Swift 2.0新特性     转眼间,Swift已经一岁多了,这门新鲜.语法时尚.类型安全.执行速度更快的语言已经渐渐的深入广大开发者的心.我同样也是非常喜爱这门新的编程语言. 今年6月,一年一度 ...

  9. 现代的新语言--Swift初探

    新的语言 WWDC简短的介绍,新的语言Swift就问世了,尽管新语言的名字导致贴吧下歌手粉丝和开发人员们争抢地盘- -,只是雨燕就是这么来了. WWDC keynote里给Swift打上了非常多标签: ...

随机推荐

  1. 终于解决了PHP调用SOAP过程中的种种问题。(转)

    最近在做公司和第三方的一个合作项目,需要调用统一验证接口和统一支付接口.由于牵涉公司机密,所以我要单独写一层PHP的接口给第三方用.前面那个验证接口主要卡在了des加密的方式上,这个有时间再说.这篇主 ...

  2. git大百科

    1,命令: git忽略提交文件:git rm --cache .idea/workspace.xml 因为你已经把他加到tracked file里了 用 git rm --cached java/.i ...

  3. HTTP协议概述

    虽然cURL支持多种协议,但日常我们最常用的还是HTTP协议,下文中着重介绍HTTP的相关使用方法,因此我们要对HTTP协议有所了解. HTTP,超文本传送协议,通过因特网传送万维网文档的数据传送协议 ...

  4. git备忘(长久更新)

    一直想了解一下git,正好最近的有一个问题就是,实验室写的代码,怎么同步到自己宿舍的笔记本上面来.最开始想用dropbox,但是用VS的人都知道,工程文件里面会给你生成乱七八糟的很多东西,很占空间,d ...

  5. CICS中设置程序常驻内存

    CICS中设置程序常驻内存 Permanent=no 修改为Permanent=yse --------------------- 对CICS的参数进行调节,RD中

  6. asp.net身份认证

    在网上看到几篇比较好的文章很详细讲解了Form.Membership.以及Identity身份认证 Form身份认证: http://www.cnblogs.com/fish-li/archive/2 ...

  7. Elasticsearch的PHP的API使用(一)

    前提:在服务器上安装Elasticsearch  (host:192.168.1.10)   http://192.168.1.10:9200?_search?pretty 1:安装PHP的Elast ...

  8. JAVA编程思想第一题出现错误

    //: object/E01_DefaultInitialization.java public class E01_DefaultInitialization{ int i ; char c ; p ...

  9. 搭建高性能计算环境(七)、应用软件的安装之MS

    1,上传软件包MaterialsStudio70.tgz.msi_7.lic到服务器上. 2,安装ms一般会创建一个普通用户msi,软件安装在msi账号下. 创建用户msi: useradd msi ...

  10. ISBN和标准编码关系以及概念

    <中国标准书号>(China standard Book Number)是1986年由国家标准局批准颁发的一项国家标准(GB5795-86).该标准是在采用国际标准LSO2108——国际标 ...