美文网首页Ios开发iOS学习开发Swift
Swift-dequeueReusableCell与dequeu

Swift-dequeueReusableCell与dequeu

作者: FlyElephant | 来源:发表于2017-03-23 13:51 被阅读1367次

UITableView中单元格复用有两种方法,dequeueReusableCell与dequeueReusableCell:indexPath.
<pre><code>` open func dequeueReusableCell(withIdentifier identifier: String) -> UITableViewCell? // Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.

@available(iOS 6.0, *)
open func dequeueReusableCell(withIdentifier identifier: String, for indexPath: IndexPath) -> UITableViewCell // newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered`</code></pre>

① 第一种方法最常用,不管UITableView是否注册,都可以执行以下代码:
<pre><code>var cell:UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: "CustomTableCell") if cell == nil { cell = UITableViewCell.init(style: .default, reuseIdentifier: "CustomTableCell") }</code></pre>

② 第二种方法要求UITableView必须注册类或者nib文件:
<pre><code>self.tableView.register(UINib.init(nibName: "CustomTableCell", bundle:.main), forCellReuseIdentifier: "CustomTableCell")</code></pre>

<pre><code>let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "CustomTableCell", for: indexPath)</code></pre>
如果注册过,第一种方法的cell == nil判断则不需要执行~

相关文章

  • Swift-dequeueReusableCell与dequeu

    UITableView中单元格复用有两种方法,dequeueReusableCell与dequeueReusabl...

  • UITableViewCell重用相关

    1、cell重用方法 dequeueReusableCellWithIdentifier:(推荐使用)dequeu...

  • IOS面试题(TableView) ----- 重用机制

    问题: 请说一下tableview的重用机制 先看个例子 这里是tableview通常写法, 而其中dequeu...

  • && 与& ,||与|

    回忆知识点i++,,++i变量在前 先用变量符号(☞++/--)在前 先计算

  • 认真与身板

    认真与身板 认真与态度 认真与自信 认真与信心 认真与诚心 认真与正心 认真与正念 认真与正面 认真与精诚 认真与...

  • 与荒野,与你,与自己

    周末了,想跟大家分享一首诗 《阿莱夫》 诗作者:赖尔逊 阿莱夫在草原上盖了一栋房子, 犹如大海上的灯塔。 但你无法...

  • 与雪与丘与故土

  • 与海与浪与念

    木君 下午,在一段段风雨的催促下来到了绥中。天是被蒙起来的,太阳早已不知躲到哪里去了。微弱的日光和着轻柔的海风洒在...

  • 晚风与柳 孤独与狗 桥与落叶 马与白隙 云与苍天 梭与星月 天与地 生与死 树与来路 花与过往 我与你 爱与恨 夜色与酒

  • 海街日记

    和解。与他人和解、与家人和解、与自己和解;与得到和解、与失去和解;与过去和解、与现在、未来和解;与现实和解、与虚幻...

网友评论

    本文标题:Swift-dequeueReusableCell与dequeu

    本文链接:https://www.haomeiwen.com/subject/swbznttx.html