iOS开源:UITableView - 高度缓存库
pxjm2077
8年前
<h2>It is translation of the <a href="/misc/goto?guid=4958873479117876841" rel="nofollow,noindex">UITableView-FDTemplateLayoutCell</a> by Swift</h2> <h2>Overview</h2> <p>It can be through the Auto layout and Frame layout way to obtain the height of the cell, and you can use the same key or indexpath way to cache the height of the cell</p> <p style="text-align:center"><img src="https://simg.open-open.com/show/aae13cc2fa93e24103c63e819c0dad88.gif"></p> <h2>Installation</h2> <ul> <li>CocoaPods</li> </ul> <pre> <code class="language-objectivec">pod 'FDTemplateLayoutCell'</code></pre> <ul> <li>Carthage</li> </ul> <pre> <code class="language-objectivec">github "huangboju/FDTemplateLayoutCell.swift"</code></pre> <h2>Usage</h2> <ul> <li>No cache</li> </ul> <pre> <code class="language-objectivec">override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return tableView.fd_heightForCell(with: "cell") { (cell) in // Configure this cell with data, same as what you've done in "-tableView:cellForRowAtIndexPath:" // Like: // cell.data = datas[indexPath.row] } }</code></pre> <ul> <li>IndexPath cache</li> </ul> <pre> <code class="language-objectivec">override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return tableView.fd_heightForCell(with: "cell", cacheBy: indexPath) { (cell) in // Configure this cell with data, same as what you've done in "-tableView:cellForRowAtIndexPath:" // Like: // cell.data = datas[indexPath.row] } }</code></pre> <ul> <li>Key cache</li> </ul> <pre> <code class="language-objectivec">override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return tableView.fd_heightForCell(with: "cell", cacheByKey: entity.identifier ?? "") { (cell) in // Configure this cell with data, same as what you've done in "-tableView:cellForRowAtIndexPath:" // Like: // cell.data = datas[indexPath.row] } }</code></pre> <h2>Frame layout mode</h2> <p>FDTemplateLayoutCell.swift offers 2 modes for asking cell's height.</p> <ul> <li>Auto layout mode using "-systemLayoutSizeFittingSize:"</li> <li>Frame layout mode using "-sizeThatFits:"</li> </ul> <p>You can use this API change mode for asking cell's height,default is true, because use "frame layout" rather than "auto layout".</p> <pre> <code class="language-objectivec">cell.fd_usingFrameLayout = false</code></pre> <p>And if you're using frame layout mode, you must override func sizeThatFits(_ size: CGSize) -> CGSize in your customized cell and return content view's height (separator excluded)</p> <pre> <code class="language-objectivec">override func sizeThatFits(_ size: CGSize) -> CGSize { return CGSize(width: size.width, height: h1 + h2 + ... + hn) }</code></pre> <h2>Debug log</h2> <p>Debug log helps to debug or inspect what is this "FDTemplateLayoutCell.swift" extention doing, turning on to print logs when "calculating", "precaching" or "hitting cache".Default to "false", log by "print".</p> <pre> <code class="language-objectivec">tableView.fd_debugLogEnabled = true</code></pre> <p>It will print like this:</p> <pre> <code class="language-objectivec">** FDTemplateLayoutCell ** hit cache by index path[0, 17] - 123.5 ** FDTemplateLayoutCell ** hit cache by index path[0, 18] - 237.5 ** FDTemplateLayoutCell ** hit cache by index path[0, 18] - 237.5 ** FDTemplateLayoutCell ** hit cache by index path[0, 19] - 159.5 ** FDTemplateLayoutCell ** hit cache by index path[0, 19] - 159.5 ** FDTemplateLayoutCell ** hit cache by index path[0, 20] - 262.0 ** FDTemplateLayoutCell ** hit cache by index path[0, 20] - 262.0 ** FDTemplateLayoutCell ** hit cache by index path[0, 21] - 288.0 ** FDTemplateLayoutCell ** hit cache by index path[0, 21] - 288.0 ** FDTemplateLayoutCell ** hit cache by index path[0, 22] - 299.0 ** FDTemplateLayoutCell ** hit cache by index path[0, 22] - 299.0 ** FDTemplateLayoutCell ** hit cache by index path[0, 23] - 176.5</code></pre> <h2>About self-satisfied cell</h2> <p>a fully <strong>self-satisfied</strong> cell is constrainted by auto layout and each edge("top", "left", "bottom", "right") has at least one layout constraint against it. It's the same concept introduced as "self-sizing cell" in iOS8 using auto layout.</p> <p>A bad one :( - missing right and bottom <img src="https://simg.open-open.com/show/87a46393fdc687c9d19a3642fb2c22f1.png"></p> <p>A good one :)</p> <p style="text-align:center"><img src="https://simg.open-open.com/show/86689059d173e77a1da6cd0a1065e609.png"></p> <h2>Notes</h2> <p>A template layout cell is created by dequeueReusableCell(withIdentifier: <#T##String#>) method, it means that you MUST have registered this cell reuse identifier by one of:</p> <ul> <li>A prototype cell of UITableView in storyboard.</li> <li>Use register(UINib, forCellReuseIdentifier: String)</li> <li>Use register(AnyClass, forCellReuseIdentifier: String)</li> </ul> <h2>Contact</h2> <p>If you find an issue, just open a ticket. Pull requests are warmly welcome as well.</p> <h3>License</h3> <p>FDTemplateLayoutCell.swift is released under the MIT license. See LICENSE for details.</p> <p> </p>