tableview优化

来源:互联网 发布:瓷砖店设计软件 编辑:程序博客网 时间:2024/06/05 09:08

tableView优化之高度缓存功能

日常开发中,tableView的使用率很高,所以相对tableView的优化来说可以做很多很多的事情。很多情况下,我们的每一个cell都没有一个固定的高度,而是根据cell里面的内容自适应高度的。那么每次当我们cell将要出现在屏幕的时候,系统都会去计算cell的高度。如果说我能通过某种手段,在首次计算的时候,将每个cell对应的高度保存下载,当下次需要用到cell高度的时候再从保存的地方取出,从而减少了计算量,来达到优化的目的。

因此应运而生了这套高度缓存的算法。

在此声明,这套算法不是老司机原创,是整合并优化了外国一位大神的源码。

再次声明,

这原来是国人算法,扬我国威啊,原型名
UITableView-FDTemplateLayoutCell。


这篇博客中你可能会用到

  • category 、subclass、protocol之间的区别
  • 自动布局相关
  • runTime动态绑定
  • tableViewCell重用的相关

恩,其实东西并不多,重要的还是一个思想。


老规矩,还是先放全部代码。

<code class="objectivec"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">#import <span class="hljs-title">"UITableView+HeightCache.h"</span></span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">#import <span class="hljs-title"><objc/runtime.h></span></span><span class="hljs-class"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">@implementation</span> <span class="hljs-title" style="color: rgb(102, 0, 102);">UITableView</span> (<span class="hljs-title" style="color: rgb(102, 0, 102);">HeightCache</span>)</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">#pragma mark ---接口方法---</span>-(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGFloat</span>)DW_CalculateCellWithIdentifier:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *)identifier                               indexPath:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> *)indexPath                           configuration:(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span>(^)(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">id</span> cell))configuration{    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.bounds</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.size</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.width</span> != <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0);">//防止初始宽度为0(如autoLayout初次加载时)</span>    {        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (!identifier<span class="hljs-variable" style="color: rgb(102, 0, 102);">.length</span> || !indexPath) {<span class="hljs-comment" style="color: rgb(136, 0, 0);">//非空判断</span>            <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>;        }        <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> * key = [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.cache</span> makeKeyWithIdentifier:identifier indexPath:indexPath];<span class="hljs-comment" style="color: rgb(136, 0, 0);">//制作key</span>        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> ([<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.cache</span> existInCacheByKey:key]) {<span class="hljs-comment" style="color: rgb(136, 0, 0);">//如果key存在</span>            <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.cache</span> heightFromCacheWithKey:key];<span class="hljs-comment" style="color: rgb(136, 0, 0);">//从字典中取出高</span>        }        <span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGFloat</span> height = [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span> DW_CalCulateCellWithIdentifier:identifier configuration:configuration];<span class="hljs-comment" style="color: rgb(136, 0, 0);">//不存在则计算高度</span>        [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.cache</span> cacheHeight:height byKey:key];<span class="hljs-comment" style="color: rgb(136, 0, 0);">//并缓存</span>        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> height;    }    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>;}-(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span>)DW_RemoveHeightCacheWithIdentifier:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *)identifier                                indexPath:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> *)indexPath                             numberOfRows:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSInteger</span>)rows{    [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.cache</span> removeHeightByIdentifier:identifier indexPath:indexPath numberOfRows:rows];}-(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span>)DW_RemoveAllHeightCache{    [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.cache</span> removeAllHeight];}-(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span>)DW_InsertCellToIndexPath:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> *)indexPath                 withIdentifier:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *)identifier                   numberOfRows:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSInteger</span>)rows{    [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.cache</span> insertCellToIndexPath:indexPath withIdentifier:identifier numberOfRows:rows toDictionaryForCache:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.cache</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.dicHeightCurrent</span>];}-(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span>)DW_MoveCellFromIndexPath:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> *)sourceIndexPath    sourceIndexPathNumberOfRows:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSInteger</span>)sourceRows                    toIndexPath:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> *)destinationIndexPathdestinationIndexPathNumberOfRows:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSInteger</span>)destinationRows                 withIdentifier:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *)identifier{    [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.cache</span> moveCellFromIndexPath:sourceIndexPath sourceSectionNumberOfRows:sourceRows toIndexPath:destinationIndexPath destinationSectionNumberOfRows:destinationRows withIdentifier:identifier];}<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">#pragma mark ---工具方法---</span><span class="hljs-comment" style="color: rgb(136, 0, 0);">///从重用池中返回计算用的cell</span>-(__kindof <span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewCell</span>  *)DW_CalculateCellWithIdentifier:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *)identifier{    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (!identifier<span class="hljs-variable" style="color: rgb(102, 0, 102);">.length</span>) {        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> <span class="hljs-literal" style="color: rgb(0, 102, 102);">nil</span>;    }    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMutableDictionary</span> <<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> * ,<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewCell</span> *> *DicForTheUniqueCalCell = objc_getAssociatedObject(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span>, _cmd);<span class="hljs-comment" style="color: rgb(136, 0, 0);">//利用runtime取出tableV绑定的存有cell的字典</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (!DicForTheUniqueCalCell) {        DicForTheUniqueCalCell = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMutableDictionary</span> dictionary];<span class="hljs-comment" style="color: rgb(136, 0, 0);">//如果取不到则新建并绑定</span>        objc_setAssociatedObject(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span>, _cmd, DicForTheUniqueCalCell, OBJC_ASSO<span class="hljs-built_in" style="color: rgb(102, 0, 102);">CIATION_RETAIN_NONATOMIC</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0);">//动态绑定(绑定目标,关键字,绑定者,策略)</span>    }    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//以上只是为了只绑定一个字典,类比懒加载</span>    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewCell</span> * cell = DicForTheUniqueCalCell[identifier];    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (!cell) {        cell = [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span> dequeueReusableCellWithIdentifier:identifier];<span class="hljs-comment" style="color: rgb(136, 0, 0);">//从重用池中取一个cell用来计算,必须以本方式从重用池中取,若以indexPath方式取由于-heightForRowAtIndexPath方法会造成循环。</span>        cell<span class="hljs-variable" style="color: rgb(102, 0, 102);">.contentView</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.translatesAutoresizingMaskIntoConstraints</span> = <span class="hljs-literal" style="color: rgb(0, 102, 102);">NO</span>;<span class="hljs-comment" style="color: rgb(136, 0, 0);">//开启约束</span>        cell<span class="hljs-variable" style="color: rgb(102, 0, 102);">.JustForCal</span> = <span class="hljs-literal" style="color: rgb(0, 102, 102);">YES</span>;<span class="hljs-comment" style="color: rgb(136, 0, 0);">//标记只用来计算</span>        DicForTheUniqueCalCell[identifier] = cell;    }    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//同上,保证只有一个用来计算的cell</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> cell;}<span class="hljs-comment" style="color: rgb(136, 0, 0);">///根据重用表示取出cell并操作cell后,计算高度</span>-(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGFloat</span>)DW_CalCulateCellWithIdentifier:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *)identifier                           configuration:(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span>(^)(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">id</span> cell))configuration{    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (!identifier<span class="hljs-variable" style="color: rgb(102, 0, 102);">.length</span>) {        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>;    }    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewCell</span> * cell = [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span> DW_CalculateCellWithIdentifier:identifier];    [cell prepareForReuse];<span class="hljs-comment" style="color: rgb(136, 0, 0);">//放回重用池</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (configuration) {        configuration(cell);<span class="hljs-comment" style="color: rgb(136, 0, 0);">//对cell进行操作</span>    }        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span> DW_CalculateCellHeightWithCell:cell];}<span class="hljs-comment" style="color: rgb(136, 0, 0);">///根据cell计算cell的高度</span>-(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGFloat</span>)DW_CalculateCellHeightWithCell:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewCell</span> *)cell{    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGFloat</span> width = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.bounds</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.size</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.width</span>;    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//根据辅助视图校正width</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (cell<span class="hljs-variable" style="color: rgb(102, 0, 102);">.accessoryView</span>) {        width -= cell<span class="hljs-variable" style="color: rgb(102, 0, 102);">.accessoryView</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.bounds</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.size</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.width</span> + <span class="hljs-number" style="color: rgb(0, 102, 102);">16</span>;    }    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">else</span>    {        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">const</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGFloat</span> accessoryWidth[] = {            [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewCellAccessoryNone</span>] = <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>,            [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewCellAccessoryDisclosureIndicator</span>] = <span class="hljs-number" style="color: rgb(0, 102, 102);">34</span>,            [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewCellAccessoryDetailDisclosureButton</span>] = <span class="hljs-number" style="color: rgb(0, 102, 102);">68</span>,            [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewCellAccessoryCheckmark</span>] = <span class="hljs-number" style="color: rgb(0, 102, 102);">40</span>,            [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewCellAccessoryDetailButton</span>] = <span class="hljs-number" style="color: rgb(0, 102, 102);">48</span>        };        width -= accessoryWidth[cell<span class="hljs-variable" style="color: rgb(102, 0, 102);">.accessoryType</span>];    }    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGFloat</span> height = <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>;    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (!cell<span class="hljs-variable" style="color: rgb(102, 0, 102);">.NoAutoSizing</span> && width > <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>) {<span class="hljs-comment" style="color: rgb(136, 0, 0);">//如果不是非自适应模式则添加约束后计算约束后高度</span>        <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSLayoutConstraint</span> * widthConstraint = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSLayoutConstraint</span> constraintWithItem:cell<span class="hljs-variable" style="color: rgb(102, 0, 102);">.contentView</span> attribute:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSLayoutAttributeWidth</span> relatedBy:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSLayoutRelationEqual</span> toItem:<span class="hljs-literal" style="color: rgb(0, 102, 102);">nil</span> attribute:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSLayoutAttributeNotAnAttribute</span> multiplier:<span class="hljs-number" style="color: rgb(0, 102, 102);">1.0</span> constant:width];<span class="hljs-comment" style="color: rgb(136, 0, 0);">//创建约束</span>        [cell<span class="hljs-variable" style="color: rgb(102, 0, 102);">.contentView</span> addConstraint:widthConstraint];<span class="hljs-comment" style="color: rgb(136, 0, 0);">//添加约束</span>        height = [cell<span class="hljs-variable" style="color: rgb(102, 0, 102);">.contentView</span> systemLayoutSizeFittingSize:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UILayoutFittingCompressedSize</span>]<span class="hljs-variable" style="color: rgb(102, 0, 102);">.height</span>;<span class="hljs-comment" style="color: rgb(136, 0, 0);">//计算高度</span>        [cell<span class="hljs-variable" style="color: rgb(102, 0, 102);">.contentView</span> removeConstraint:widthConstraint];<span class="hljs-comment" style="color: rgb(136, 0, 0);">//移除约束</span>    }    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (height == <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>) {<span class="hljs-comment" style="color: rgb(136, 0, 0);">//如果约束错误可能导致计算结果为零,则以自适应模式再次计算</span>        height = [cell sizeThatFits:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGSizeMake</span>(width, <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>)]<span class="hljs-variable" style="color: rgb(102, 0, 102);">.height</span>;    }    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (height == <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>) {<span class="hljs-comment" style="color: rgb(136, 0, 0);">//如果计算仍然为0,则给出默认高度</span>        height = <span class="hljs-number" style="color: rgb(0, 102, 102);">44</span>;    }    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.separatorStyle</span> != <span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewCellSeparatorStyleNone</span>) {<span class="hljs-comment" style="color: rgb(136, 0, 0);">//如果不为无分割线模式则添加分割线高度</span>        height += <span class="hljs-number" style="color: rgb(0, 102, 102);">1.0</span> /[<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIScreen</span> mainScreen]<span class="hljs-variable" style="color: rgb(102, 0, 102);">.scale</span>;    }    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> height;}<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">#pragma mark ---setter、getter---</span>-(HeightCache *)cache<span class="hljs-comment" style="color: rgb(136, 0, 0);">//懒加载形式</span>{    HeightCache * cacheTemp = objc_getAssociatedObject(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span>, _cmd);    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (!cacheTemp) {        cacheTemp = [HeightCache new];        objc_setAssociatedObject(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span>, _cmd, cacheTemp, OBJC_ASSO<span class="hljs-built_in" style="color: rgb(102, 0, 102);">CIATION_RETAIN_NONATOMIC</span>);    }    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> cacheTemp;}-(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span>)setCache:(HeightCache *)cache{    objc_setAssociatedObject(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">@selector</span>(cache), cache, OBJC_ASSO<span class="hljs-built_in" style="color: rgb(102, 0, 102);">CIATION_RETAIN_NONATOMIC</span>);}<span class="hljs-keyword" style="color: rgb(0, 0, 136);">@end</span><span class="hljs-class"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">@implementation</span> <span class="hljs-title" style="color: rgb(102, 0, 102);">UITableViewCell</span> (<span class="hljs-title" style="color: rgb(102, 0, 102);">HeightCacheCell</span>)</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">#pragma mark ---setter、getter---</span>-(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">BOOL</span>)NoAutoSizing{    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> [objc_getAssociatedObject(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span>, _cmd) boolValue];}-(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span>)setNoAutoSizing:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">BOOL</span>)NoAutoSizing{    objc_setAssociatedObject(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">@selector</span>(NoAutoSizing), @(NoAutoSizing), OBJC_ASSO<span class="hljs-built_in" style="color: rgb(102, 0, 102);">CIATION_RETAIN</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0);">//关键字用getter的方法名,为保持关键字一致</span>}-(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">BOOL</span>)JustForCal{    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> [objc_getAssociatedObject(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span>, _cmd) boolValue];}-(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span>)setJustForCal:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">BOOL</span>)JustForCal{    objc_setAssociatedObject(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">@selector</span>(JustForCal), @(JustForCal), OBJC_ASSO<span class="hljs-built_in" style="color: rgb(102, 0, 102);">CIATION_RETAIN</span>);}<span class="hljs-keyword" style="color: rgb(0, 0, 136);">@end</span><span class="hljs-class"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">@implementation</span> <span class="hljs-title" style="color: rgb(102, 0, 102);">HeightCache</span></span><span class="hljs-comment" style="color: rgb(136, 0, 0);">///制作key</span>-(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *)makeKeyWithIdentifier:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *)identifier                         indexPath:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> *)indexPath{    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> stringWithFormat:<span class="hljs-string" style="color: rgb(0, 136, 0);">@"%@S%ldR%ld"</span>,identifier,indexPath<span class="hljs-variable" style="color: rgb(102, 0, 102);">.section</span>,indexPath<span class="hljs-variable" style="color: rgb(102, 0, 102);">.row</span>];}<span class="hljs-comment" style="color: rgb(136, 0, 0);">///高度是否存在</span>-(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">BOOL</span>)existInCacheByKey:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *)key{    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSNumber</span> * value = [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.dicHeightCurrent</span> valueForKey:key];    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> (value && ![value isEqualToNumber:@-<span class="hljs-number" style="color: rgb(0, 102, 102);">1</span>]);}<span class="hljs-comment" style="color: rgb(136, 0, 0);">///取出缓存的高度</span>-(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGFloat</span>)heightFromCacheWithKey:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *)key{    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSNumber</span> * value = [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.dicHeightCurrent</span> valueForKey:key];    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> ([<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span> is64bit]) {        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> [value doubleValue];    }    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> [value floatValue];}<span class="hljs-comment" style="color: rgb(136, 0, 0);">///64位判断</span>- (<span class="hljs-built_in" style="color: rgb(102, 0, 102);">BOOL</span>)is64bit{<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">#if defined(__LP64__) && __LP64__</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> <span class="hljs-literal" style="color: rgb(0, 102, 102);">YES</span>;<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">#else</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> <span class="hljs-literal" style="color: rgb(0, 102, 102);">NO</span>;<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">#endif</span>}<span class="hljs-comment" style="color: rgb(136, 0, 0);">///高度缓存</span>-(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span>)cacheHeight:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGFloat</span>)height             byKey:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *)key{    [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.dicHeightCurrent</span> setValue:@(height) forKey:key];}<span class="hljs-comment" style="color: rgb(136, 0, 0);">///根据key删除缓存</span>-(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span>)removeHeightByIdentifier:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *)identifier                      indexPath:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> *)indexPath                   numberOfRows:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSInteger</span>)rows{    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (indexPath<span class="hljs-variable" style="color: rgb(102, 0, 102);">.row</span> < rows) {        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>; i < rows - <span class="hljs-number" style="color: rgb(0, 102, 102);">1</span> - indexPath<span class="hljs-variable" style="color: rgb(102, 0, 102);">.row</span>; i ++) {            <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> * indexPathA = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> indexPathForRow:indexPath<span class="hljs-variable" style="color: rgb(102, 0, 102);">.row</span> + i inSection:indexPath<span class="hljs-variable" style="color: rgb(102, 0, 102);">.section</span>];            <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSLog</span>(<span class="hljs-string" style="color: rgb(0, 136, 0);">@"%ld,%ld"</span>,indexPathA<span class="hljs-variable" style="color: rgb(102, 0, 102);">.row</span>,indexPathA<span class="hljs-variable" style="color: rgb(102, 0, 102);">.section</span>);            <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> * indexPathB = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> indexPathForRow:indexPath<span class="hljs-variable" style="color: rgb(102, 0, 102);">.row</span> + i + <span class="hljs-number" style="color: rgb(0, 102, 102);">1</span> inSection:indexPath<span class="hljs-variable" style="color: rgb(102, 0, 102);">.section</span>];            <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSLog</span>(<span class="hljs-string" style="color: rgb(0, 136, 0);">@"%ld,%ld"</span>,indexPathB<span class="hljs-variable" style="color: rgb(102, 0, 102);">.row</span>,indexPathB<span class="hljs-variable" style="color: rgb(102, 0, 102);">.section</span>);            [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span> exchangeValueForIndexPathA:indexPathA andIndexPathB:indexPathB withIdentifier:identifier dictionary:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.dicHeightCacheH</span>];            [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span> exchangeValueForIndexPathA:indexPathA andIndexPathB:indexPathB withIdentifier:identifier dictionary:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.dicHeightCacheV</span>];        }        <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> * indexPathC = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> indexPathForRow:rows - <span class="hljs-number" style="color: rgb(0, 102, 102);">1</span> inSection:indexPath<span class="hljs-variable" style="color: rgb(102, 0, 102);">.section</span>];        <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> * key = [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span> makeKeyWithIdentifier:identifier indexPath:indexPathC];        [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.dicHeightCacheH</span> removeObjectForKey:key];        [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.dicHeightCacheV</span> removeObjectForKey:key];    }}<span class="hljs-comment" style="color: rgb(136, 0, 0);">///删除所有缓存</span>-(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span>)removeAllHeight{    [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.dicHeightCacheH</span> removeAllObjects];    [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.dicHeightCacheV</span> removeAllObjects];}<span class="hljs-comment" style="color: rgb(136, 0, 0);">///插入cell是插入value</span>-(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span>)insertCellToIndexPath:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> *)indexPath            withNumberOfRows:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSInteger</span>)rows                heightNumber:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSNumber</span> *)height                  identifier:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *)identifier        toDictionaryForCache:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMutableDictionary</span> *)dic{    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (indexPath<span class="hljs-variable" style="color: rgb(102, 0, 102);">.row</span> < rows + <span class="hljs-number" style="color: rgb(0, 102, 102);">1</span>) {        [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span> insertCellToIndexPath:indexPath withIdentifier:identifier numberOfRows:rows toDictionaryForCache:dic];        <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> * key = [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span> makeKeyWithIdentifier:identifier indexPath:indexPath];        [dic setValue:height forKey:key];    }}-(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span>)insertCellToIndexPath:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> *)indexPath withIdentifier:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *)identifier numberOfRows:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSInteger</span>)rows toDictionaryForCache:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMutableDictionary</span> *)dic{    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (indexPath<span class="hljs-variable" style="color: rgb(102, 0, 102);">.row</span> < rows + <span class="hljs-number" style="color: rgb(0, 102, 102);">1</span>) {        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>; i < rows - indexPath<span class="hljs-variable" style="color: rgb(102, 0, 102);">.row</span>; i ++) {            <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> * indexPathA = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> indexPathForRow:rows - i inSection:indexPath<span class="hljs-variable" style="color: rgb(102, 0, 102);">.section</span>];            <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> * indexPathB = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> indexPathForRow:rows - i - <span class="hljs-number" style="color: rgb(0, 102, 102);">1</span> inSection:indexPath<span class="hljs-variable" style="color: rgb(102, 0, 102);">.section</span>];            [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span> exchangeValueForIndexPathA:indexPathA andIndexPathB:indexPathB withIdentifier:identifier dictionary:dic];        }    }}<span class="hljs-comment" style="color: rgb(136, 0, 0);">///移动cell时交换value</span>-(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span>)moveCellFromIndexPath:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> *)sourceIndexPath   sourceSectionNumberOfRows:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSInteger</span>)sourceRows                 toIndexPath:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> *)destinationIndexPathdestinationSectionNumberOfRows:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSInteger</span>)destinationRows              withIdentifier:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *)identifier{    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (sourceIndexPath<span class="hljs-variable" style="color: rgb(102, 0, 102);">.section</span> == destinationIndexPath<span class="hljs-variable" style="color: rgb(102, 0, 102);">.section</span>) {        [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span> moveCellInSectionFromIndexPath:sourceIndexPath toIndexPath:destinationIndexPath withIdentifier:identifier];    }    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">else</span>    {        [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span> moveCellOutSectionFromIndexPath:sourceIndexPath sourceSectionNumberOfRows:sourceRows toIndexPath:destinationIndexPath destinationSectionNumberOfRows:destinationRows withIdentifier:identifier];    }}<span class="hljs-comment" style="color: rgb(136, 0, 0);">///组内移动</span>-(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span>)moveCellInSectionFromIndexPath:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> *)sourceIndexPath                          toIndexPath:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> *)destinationIndexPath                       withIdentifier:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *)identifier{    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSInteger</span> rowA = sourceIndexPath<span class="hljs-variable" style="color: rgb(102, 0, 102);">.row</span>;    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSInteger</span> rowB = destinationIndexPath<span class="hljs-variable" style="color: rgb(102, 0, 102);">.row</span>;    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>; i < (MAX(rowA, rowB) - MIN(rowA, rowB)); i ++) {        <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> * indexPathA = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> indexPathForRow:MIN(rowA, rowB) + i inSection:sourceIndexPath<span class="hljs-variable" style="color: rgb(102, 0, 102);">.section</span>];        <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> * indexPathB = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> indexPathForRow:MIN(rowA, rowB) + i + <span class="hljs-number" style="color: rgb(0, 102, 102);">1</span> inSection:sourceIndexPath<span class="hljs-variable" style="color: rgb(102, 0, 102);">.section</span>];        [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span> exchangeValueForIndexPathA:indexPathA andIndexPathB:indexPathB withIdentifier:identifier dictionary:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.dicHeightCacheV</span>];        [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span> exchangeValueForIndexPathA:indexPathA andIndexPathB:indexPathB withIdentifier:identifier dictionary:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.dicHeightCacheH</span>];    }}<span class="hljs-comment" style="color: rgb(136, 0, 0);">///组外移动</span>-(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span>)moveCellOutSectionFromIndexPath:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> *)sourceIndexPath             sourceSectionNumberOfRows:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSInteger</span>)sourceRows                           toIndexPath:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> *)destinationIndexPath        destinationSectionNumberOfRows:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSInteger</span>)destinationRows                        withIdentifier:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *)identifier{    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSNumber</span> * numberH;    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSNumber</span> * numberV;    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSLog</span>(<span class="hljs-string" style="color: rgb(0, 136, 0);">@"%ld"</span>,sourceIndexPath<span class="hljs-variable" style="color: rgb(102, 0, 102);">.row</span>);    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (sourceIndexPath<span class="hljs-variable" style="color: rgb(102, 0, 102);">.row</span> < sourceRows) {        <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> * key = [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span> makeKeyWithIdentifier:identifier indexPath:sourceIndexPath];        numberH = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.dicHeightCacheH</span>[key];        numberV = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.dicHeightCacheV</span>[key];        [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span> removeHeightByIdentifier:identifier indexPath:sourceIndexPath numberOfRows:sourceRows];    }    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSLog</span>(<span class="hljs-string" style="color: rgb(0, 136, 0);">@"%ld,%ld"</span>,destinationIndexPath<span class="hljs-variable" style="color: rgb(102, 0, 102);">.row</span>,destinationIndexPath<span class="hljs-variable" style="color: rgb(102, 0, 102);">.section</span>);    [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span> insertCellToIndexPath:destinationIndexPath withNumberOfRows:destinationRows heightNumber:numberH identifier:identifier toDictionaryForCache:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.dicHeightCacheH</span>];    [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span> insertCellToIndexPath:destinationIndexPath withNumberOfRows:destinationRows heightNumber:numberV identifier:identifier toDictionaryForCache:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.dicHeightCacheV</span>];}<span class="hljs-comment" style="color: rgb(136, 0, 0);">///根据indexPath交换两个Key</span>-(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span>)exchangeValueForIndexPathA:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> *)indexPathA                    andIndexPathB:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> *)indexPathB                   withIdentifier:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *)identifier                       dictionary:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMutableDictionary</span> *)dic{    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> * keyA = [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span> makeKeyWithIdentifier:identifier indexPath:indexPathA];    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> * keyB = [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span> makeKeyWithIdentifier:identifier indexPath:indexPathB];    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSNumber</span> * Temp = dic[keyA];    dic[keyA] = dic[keyB];    dic[keyB] = Temp;}<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">#pragma mark ---懒加载---</span>-(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMutableDictionary</span> *)dicHeightCacheH{    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (!_dicHeightCacheH) {        _dicHeightCacheH = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMutableDictionary</span> dictionary];    }    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> _dicHeightCacheH;}-(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMutableDictionary</span> *)dicHeightCacheV{    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (!_dicHeightCacheV) {        _dicHeightCacheV = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMutableDictionary</span> dictionary];    }    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> _dicHeightCacheV;}-(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMutableDictionary</span> *)dicHeightCurrent<span class="hljs-comment" style="color: rgb(136, 0, 0);">//根据系统状态返回对应字典</span>{    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIDeviceOrientationIsPortrait</span>([<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIDevice</span> currentDevice]<span class="hljs-variable" style="color: rgb(102, 0, 102);">.orientation</span>)?<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.dicHeightCacheV</span>:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.dicHeightCacheH</span>;}<span class="hljs-keyword" style="color: rgb(0, 0, 136);">@end</span></code>

这么多,你这个骗子!
你是不是这么想得。

别慌,东西不多,因为这是一个category,复用性非常高,所以老司机想尽量减少文件个数,这样集成的时候也方便不是。

所以,老司机把三个类写在一个文件里了

之前有人问过我把几个类写在一个文件中有什么好处么

老司机目前只能说,除了看上去装逼点,别的没什么卵用

可能是老司机的理解不深,不过为了集成方便我还是写在一个文件里了。

废话不多说,分段讲解吧。


分段解析

为什么选择category而不是subclass

我想很多人都会有疑问,为什么选择category而不是继承,毕竟category不能添加属性,用起来不是很方便。说到这又要老生常谈了。

Protocol只是声明一套接口,并不能提供具体实现,变相的也算是一种抽象基类的实现方式(OC本身语法并不支持抽象基类)。

Category可以为已有的类提供额外的接口和具体的实现。

Protocol只能提供一套公用的接口声明,并不能提供具体实现,它的行为是,我只负责声明,而不管谁去实现,去如何实现。这样的话,我定义一套接口,可以使任意的类都用不同的方式去实现接口中的方法,就是为遵守了protocol的类提供了一些额外访问这个类的一些接口,像delegate和dataSource用protocol实现是最好的。

Category是对一个功能完备的类的一种补充、扩展,就像一个东西基本功能都完成了,可以用category为这个类添加不同的组件,使得这个类能够适应不同情况的需求(但是这些不同需求最核心的需求要一致)。当然,当某个类非常大的时候,使用category可以按照不同的功能将类的实现分在不同的模块中。还有,虽然category可以访问已有类的实例变量,但不能创建新的实例变量,如果要创建新的实例变量,请使用继承。

继承,它基于Protocol和Category之间,既可以像protocol一样只提供纯粹的接口,也可以像Category一样提供接口的完整实现,可以自由定义类的实例变量(这一点,Protocol倒是可以声明实例变量,但是也仅仅是声明而已),而且继承还可以对类以后的方法进行改写,所以继承的力量是最强大的。

在iOS开发中,继承是完全可以完成protocol和category的功能的,那么在开发过程中多多使用继承体系可好?

需要注意的是使用继承还有很大的代价问题。使用继承来进行扩展是一种耦合度很高的行为,对父类可以说是完全依赖,如果继承体系太过复杂,会造成难以维护的问题。如果仅仅只是对类进行扩展,并不建议使用继承,毕竟使用protocol和category是很简单、轻松的。除此之外,在开发过程中,我们应该尽量将界面、功能相似的类的代码提取到基类里面,然后各个子类继承自这个基类,实现各自的其他特殊部分。这样可以大大的优化代码,如果需要修改的话,只需要这倒对应子类修改即可。

是不是感觉老司机屌屌的,啧啧啧,我百度的。

我选择category就一个原因,扩展方便,二次开发也方便。


类目拆解

老司机说过,这一坨代码是三个类写在了一个文件里,他们都是什么呢?

  • UITableView的category
  • UITableViewCell的category
  • 一个NSObject的子类

他们分别是做什么的呢?

首先,给UITableView添加category是因为为了实现高度缓存,我的方案是在计算高度的时候就模拟数据填充,从而计算出该cell的高度,所以,tableView应该有填充数据和计算高度的方法。故为其添加分类。

而那个继承于NSObject的子类就是用来存储计算出来的高度的,这样当下次需要计算的时候直接从这里取出即可。

至于那个UITableViewCell的类目是为了给cell添加两个标识符,一个用来判断当前cell是否需要一autolayout进行绘制,另一个是用来区分计算用的cell还是展示用的cell。这点现在可能你还不懂,一会我们会做相应介绍。


UITableViewCell的category

为什么先说这个类目呢,因为这个类目的内容最少,而且只用到了runtime的动态绑定,可以拿出来单独介绍一下runtime的相关知识。

什么是runtime

run,运行,time,时间,那么runtime就是运行时。恩,简单不?
然后我们说说。。。诶诶诶,别打别打,开个玩笑。
runtime,简称运行时,是系统在运行期间的一些机制。而对于OC来讲呢,其中最重要的就是消息机制

C语言呢,我们调用函数,在编译期就已经确定了要调用那个函数,而且整个过程是顺序执行的。

而在OC中呢,我们是讲消息发送的。而且我们是等待某个信号触发才执行代码的。我们知道OC事实上是基于C的,那他是怎么实现这套转换的呢?就是通过runtime去实现的。

不信?不信跟我来做个试验。

新开一个工程,删掉所有文件,只留下info.plist和main.m。并且将引入的头文件删除掉。

建一个类,里面随便写一个方法的声明和实现。

然后在main.m中引入这个类初始化并调用刚才声明的方法,如下图。

1835430-7d31aa53377e7333.png

新建一个工程

此时,打开我们的终端。找到刚才的工程的main.m,并且输入
clang -rewrite-objc main.m,点击回车。稍等你就会看到提示转换完成。


转化我们的文件

这回在finder中找到工程的文件夹,在main.m同级文件夹下多了一个文件main.cpp,这就是转换完的文件。我们看到代码还是很多的。直接拖到最下方我们大概能看到点认识的了,int main。。。

这就是我们刚才main函数里面的实现


找到实现

看不懂?待我帮你捋捋,去掉一些类型转换用的修饰符后剩下如下代码,是不是清晰多了?


姑且就叫化简吧

先看被我框选中的代码,objc_msgSend是说发送消息,他有两个参数,一个是实例,一个是方法objc_getClass通过字符串获取到这个类sel_registerName通过字符串获取方法。所以这句话的意思就是给这个类发送了这个消息,消息内容就是一个方法

随后就容易了,给这个实例发送一个sayHello的消息,参数是后面的字符串。

通过这里我们知道我们OC的语言是怎么实现的了吧,就是通过runtime转化成了C++的代码,然后进行运行。

从这你也应该知道为什么OC中叫发送消息,不叫函数调用了吧。

另外你还应该知道为什么OC中方法只声明不实现编译时只报警不报错运行时crash是为什么了吧。

既然说到这里就多少说一说C与OC吧。之前老司机说过,OC是基于C的,那么C语言中是没有对象这个概念呢,我们的对象又是什么呢?

右手啊,程序员哪有什么对象,啧啧啧。

除了右手,还有结构体,OC的对象就是C语言中的结构体


对象的结构

我们看到了,每个类都是一个都是一个结构体,其中有各种指针,指向一个类的各种参数父类、属性列表、方法列表等等

所以说当我们声明了类的方法,方法列表里面就有这个方法了,然后编译通过了,然后调用的时候,方法选择器去本类的方法列表里去寻找方法的实现,如果没有实现,则去其父类中寻找,如果在没有通过一系列消息转发机制会一直找下去,直到最后也没有找到这个方法的实现就crash了。关于消息转发,其实还有很多东西,但是在这里讲就又扯远了,所以等下期吧=。=

本例中我们用runtime做了些什么呢?

使用runtime动态为category绑定属性

之前老司机说过,category是不能添加属性的。那我又要为其添加两个标识符,只能使用runtime去动态绑定了,在类的属性列表里面通过runtime添加上这个属性,那我就可以使用这个属性了。

首先在.h的UITableCell的category的@interface之中添加两个属性

<code class="objectivec"><span class="hljs-class"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">@interface</span> <span class="hljs-title" style="color: rgb(102, 0, 102);">UITableViewCell</span> (<span class="hljs-title" style="color: rgb(102, 0, 102);">HeightCacheCell</span>)</span><span class="hljs-keyword" style="color: rgb(0, 0, 136);">@property</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136);">assign</span> ,<span class="hljs-keyword" style="color: rgb(0, 0, 136);">nonatomic</span>)<span class="hljs-built_in" style="color: rgb(102, 0, 102);">BOOL</span> JustForCal;<span class="hljs-comment" style="color: rgb(136, 0, 0);">//计算用的cell标识符(将计算用的cell与正常显示的cell进行区分,避免不必要的ui响应)</span><span class="hljs-keyword" style="color: rgb(0, 0, 136);">@property</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136);">assign</span> ,<span class="hljs-keyword" style="color: rgb(0, 0, 136);">nonatomic</span>)<span class="hljs-built_in" style="color: rgb(102, 0, 102);">BOOL</span> NoAutoSizing;<span class="hljs-comment" style="color: rgb(136, 0, 0);">//不适用autoSizing标识符(不依靠约束计算,只进行自适应)</span><span class="hljs-keyword" style="color: rgb(0, 0, 136);">@end</span></code>

这是我为期添加的两个属性,具体有什么作用,下文中会提到的,先别急。

然后在.m中添加两个属性的setter、getter方法

<code class="objectivec"><span class="hljs-class"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">@implementation</span> <span class="hljs-title" style="color: rgb(102, 0, 102);">UITableViewCell</span> (<span class="hljs-title" style="color: rgb(102, 0, 102);">HeightCacheCell</span>)</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">#pragma mark ---setter、getter---</span>-(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">BOOL</span>)NoAutoSizing{    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> [objc_getAssociatedObject(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span>, _cmd) boolValue];}-(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span>)setNoAutoSizing:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">BOOL</span>)NoAutoSizing{    objc_setAssociatedObject(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">@selector</span>(NoAutoSizing), @(NoAutoSizing), OBJC_ASSO<span class="hljs-built_in" style="color: rgb(102, 0, 102);">CIATION_RETAIN</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0);">//关键字用getter的方法名,为保持关键字一致</span>}-(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">BOOL</span>)JustForCal{    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> [objc_getAssociatedObject(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span>, _cmd) boolValue];}-(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span>)setJustForCal:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">BOOL</span>)JustForCal{    objc_setAssociatedObject(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">@selector</span>(JustForCal), @(JustForCal), OBJC_ASSO<span class="hljs-built_in" style="color: rgb(102, 0, 102);">CIATION_RETAIN</span>);}<span class="hljs-keyword" style="color: rgb(0, 0, 136);">@end</span></code>

这里老司机有必要说一下两个方法

  • objc_setAssociatedObject
  • objc_getAssociatedObject

OC自解释语言的好处就体现出来了,从函数名你就可以看出来,一个是给对象设置联系,一个是从对象获取联系。反正我英语水平就这样,我也没查字典,对不对的我就不深究了。一个setter一个getter就在这。

objc_setAssociatedObject

objc_setAssociatedObject(self, @selector(NoAutoSizing), @(NoAutoSizing), OBJC_ASSOCIATION_RETAIN);

方法总共四个参数,分别是绑定目标,关键字,绑定者,策略

所以说简单了,绑定目标,就是给谁绑定,当然是UITableViewCell这个category了,所以self。

绑定的关键字就是说我给这个对象绑定一个属性,我总要有一个标示符去表示那个属性吧,这样我要调用这个属性的时候通过标示符去寻找才能找到这个属性。

绑定者,就是我们要为这个属性绑定的值了。

绑定策略就是说绑定的这个属性的引用机制了。这里要说明一点,这个绑定策略如何选择,老司机目前也没有搞懂,所以策略这里都沿用了原作者的写法,等老司机搞懂了之后会告诉大家的。

objc_getAssociatedObject

两个参数,一个绑定目标,一个关键字,通过关键字从绑定目标中获取属性的值。

这下是不是明白这两个setter、getter方法的意义了。

好了,这个category讲完了,他的东西真的很少。

什么,你敲不出来这两个方法?
忘了讲了,你没引入头文件。。。

import <objc/runtime.h>


HeightCache

为什么说这个类呢?怎么还不进入正题呢?说好的UITableView的category呢?

因为这个类是负责存储Cell高度的类,而UITableView得category只是为获取cell高度提供了一个接口,当我们移动cell,添加cell,删除cell的时候要对这个高度的对应关系作出很多的操作,UITableView的category中大量的使用了这里的方法,所以老司机决定先把难啃的骨头解决了。

在.h中添加三个字典

<code class="objectivec"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">@property</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136);">strong</span> ,<span class="hljs-keyword" style="color: rgb(0, 0, 136);">nonatomic</span>)<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMutableDictionary</span> * dicHeightCacheV;<span class="hljs-comment" style="color: rgb(136, 0, 0);">//竖直行高缓存字典</span><span class="hljs-keyword" style="color: rgb(0, 0, 136);">@property</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136);">strong</span> ,<span class="hljs-keyword" style="color: rgb(0, 0, 136);">nonatomic</span>)<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMutableDictionary</span> * dicHeightCacheH;<span class="hljs-comment" style="color: rgb(136, 0, 0);">//水平行高缓存字典</span><span class="hljs-keyword" style="color: rgb(0, 0, 136);">@property</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136);">strong</span> ,<span class="hljs-keyword" style="color: rgb(0, 0, 136);">nonatomic</span>)<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMutableDictionary</span> * dicHeightCurrent;<span class="hljs-comment" style="color: rgb(136, 0, 0);">//当前状态行高缓存字典(中间量)</span></code>

为什么三个字典呢?老司机是这样考虑的,横屏和竖屏情况下同样内容的cell有可能是不同的,如果以同一个高度去取得话有可能出现高度不准确的问题。所以竖屏横屏分别一个字典。那这个current又是什么呢?就是自动返回当前屏幕状态所对应的字典那么一个中间量,这样我们写代码的时候可以不用考虑当前屏幕状态而统一使用current这个字典,减少很多代码量。

去.m中看看是如何实现的。

<code class="objectivec"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">#pragma mark ---懒加载---</span>-(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMutableDictionary</span> *)dicHeightCacheH{    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (!_dicHeightCacheH) {        _dicHeightCacheH = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMutableDictionary</span> dictionary];    }    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> _dicHeightCacheH;}-(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMutableDictionary</span> *)dicHeightCacheV{    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (!_dicHeightCacheV) {        _dicHeightCacheV = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMutableDictionary</span> dictionary];    }    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> _dicHeightCacheV;}-(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMutableDictionary</span> *)dicHeightCurrent<span class="hljs-comment" style="color: rgb(136, 0, 0);">//根据系统状态返回对应字典</span>{    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIDeviceOrientationIsPortrait</span>([<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIDevice</span> currentDevice]<span class="hljs-variable" style="color: rgb(102, 0, 102);">.orientation</span>)?<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.dicHeightCacheV</span>:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.dicHeightCacheH</span>;}</code>

上面两个字典是懒加载,不多说了,在需要的时候创建字典。

重点是这个current。

UIDeviceOrientationIsPortrait()这个方法是判断括号中的状态是否是竖屏状态的一个方法,所以括号里面我们给他当前屏幕状态他就可以判断是不是竖屏了。

[UIDevice currentDevice].orientation而这句代码见名知意,取出当前设备的屏幕状态

让后通过三目运算符返回相应的字典。

是不是写法上很简单,实际使用过程中也很方便。

上面几个工具方法最主要的主要由如下几个方法,交换两个cell高度的值,插入一个cell高度的值,删除一个cell高度的值。通过这三个最基本的方法组合出所有cell操作需要用到的方法。算法都很基础,没什么需要说的。


UITableView的category

最后的主角来了。其实你会发现这里的方法并不多。因为只是向外界提供了插入,删除,删除全部,移动,计算高度五个接口。

我们看一下大体思路:


大体思路

老司机觉得自己画这图也是没谁了。。。

.h中添加一个属性,是我们刚才用来存储高度的那个类的一个实例。

<code class="objectivec"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">@property</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136);">strong</span> ,<span class="hljs-keyword" style="color: rgb(0, 0, 136);">nonatomic</span>)HeightCache * cache;<span class="hljs-comment" style="color: rgb(136, 0, 0);">//缓存实例</span></code>

其实你完全可以写在.m里当做一个私有变量去处理,这样也更安全一些。
老司机写在这里是为了调试的时候更直观的看到缓存高度操作时的状态。实际应用中,如无特殊需要,建议将其写在.m中

.m中,我们先看一下这几个工具方法,这才是核心部分。接口方法都是简单调用这几个工具方法,供外界调用的。


  • -(__kindof UITableViewCell )DW_CalculateCellWithIdentifier:(NSString )identifier
  • -(CGFloat)DW_CalculateCellHeightWithCell:(UITableViewCell *)cell
  • -(CGFloat)DW_CalCulateCellWithIdentifier:(NSString *)identifier configuration:(void(^)(id cell))configuration

核心算法都在这了,我一定会好好解析的。(第一个方法返回值有一个*号我敲不出来,不知道markdown什么鬼冲突。。)

-(__kindof UITableViewCell )DW_CalculateCellWithIdentifier:(NSString )identifier

第一个方法如下:

<code class="objectivec"><span class="hljs-comment" style="color: rgb(136, 0, 0);">///从重用池中返回计算用的cell</span>-(__kindof <span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewCell</span> *)DW_CalculateCellWithIdentifier:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *)identifier{    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (!identifier<span class="hljs-variable" style="color: rgb(102, 0, 102);">.length</span>) {        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> <span class="hljs-literal" style="color: rgb(0, 102, 102);">nil</span>;    }    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMutableDictionary</span> <<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> * ,<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewCell</span> *> *DicForTheUniqueCalCell = objc_getAssociatedObject(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span>, _cmd);<span class="hljs-comment" style="color: rgb(136, 0, 0);">//利用runtime取出tableV绑定的存有cell的字典</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (!DicForTheUniqueCalCell) {        DicForTheUniqueCalCell = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMutableDictionary</span> dictionary];<span class="hljs-comment" style="color: rgb(136, 0, 0);">//如果取不到则新建并绑定</span>        objc_setAssociatedObject(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span>, _cmd, DicForTheUniqueCalCell, OBJC_ASSO<span class="hljs-built_in" style="color: rgb(102, 0, 102);">CIATION_RETAIN_NONATOMIC</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0);">//动态绑定(绑定目标,关键字,绑定者,策略)</span>    }    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//以上只是为了只绑定一个字典,类比懒加载</span>    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewCell</span> * cell = DicForTheUniqueCalCell[identifier];    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (!cell) {        cell = [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span> dequeueReusableCellWithIdentifier:identifier];<span class="hljs-comment" style="color: rgb(136, 0, 0);">//从重用池中取一个cell用来计算,必须以本方式从重用池中取,若以indexPath方式取由于-heightForRowAtIndexPath方法会造成循环。</span>        cell<span class="hljs-variable" style="color: rgb(102, 0, 102);">.contentView</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.translatesAutoresizingMaskIntoConstraints</span> = <span class="hljs-literal" style="color: rgb(0, 102, 102);">NO</span>;<span class="hljs-comment" style="color: rgb(136, 0, 0);">//开启约束</span>        cell<span class="hljs-variable" style="color: rgb(102, 0, 102);">.JustForCal</span> = <span class="hljs-literal" style="color: rgb(0, 102, 102);">YES</span>;<span class="hljs-comment" style="color: rgb(136, 0, 0);">//标记只用来计算</span>        DicForTheUniqueCalCell[identifier] = cell;    }    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//同上,保证只有一个用来计算的cell</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> cell;}</code>

其实每一句注释都表述的很清楚。不过老司机还是会一句一句给你说的,毕竟这才是老司机的风格,恩,就是墨迹

恩,老司机先说一说重用的问题吧。

我们都喜欢用tableView,因为他很好的替我们做了内存控制的问题。

他又是通过什么控制了内存呢?节省了性能呢?通过重用。

这些大家都知道。但是有很多孩子误会了重用啊,孩子你们不懂重用啊。

知道咋回事的这地方跳过吧。

前方高能预警,以下内容很基础真的很基础真的真的很基础,只是给一些真的不知道的人看的。

当一个cell将要离开屏幕时,这个cell会进入重用池。重用池并不是什么特殊的东西,就是系统给他放在一边了。他只是单纯的放在一边了,不进行任何操作。

当一个cell将要进入屏幕的时候,会调用tableView:(UITableView *)tableView cellForRowAtIndexPath这个代理,执行其中的方法

说这两句为了说明什么呢?
第一句我想说明的是,他只是放在重用池了,没有进行任!何!操!作!

重点在哪呢?重点就在于存储的是整个cell,包括cell原有的和你添加的所有子视图

第二句我想说明的是,他会执行代理中的每!一!句!话!

重点在哪呢?你从重用池中取出的cell,他是会对cell进行再次进行绘制

这两句说明了什么?

  • cell上不要布置太多的控件,不然存入重用池也够你吃一壶的。
  • tableView:(UITableView *)tableView cellForRowAtIndexPath这里面不要添加子视图,不然下次取出同一个cell的时候还会在同样的位置再添加一个同样的子视图。你上下来回滑动的时候cell就会添加无数个子视图。
  • 没有什么会影响重新绘制的,记住那句代码一定会走,只要走就一定会绘制。如果说你绘制出了什么问题,不要怪重用,跟他没关。问题一定在别的地方。

恩,这是老司机对重用的理解。

刚才跳过的旅客朋友们,跳到这可以接上了。

接下来我们开始说这个方法。
为什么我们要取到这个cell呢?而不是随便一个cell呢?

因为我们无法保证或者指定只使用一种cell,很多情况下我们是自定义的cell。这样的话每个不同种类的cell上的子视图是不相同的,在自动计算高度的时候对cell的布局有很高要求,所以我们一定要保证我们计算用的cell与展示用的cell是同一种cell

所以说我们这个方法只有一个参数,identifier。因为他是从重用池中取出cell的唯一必要参数。

首先为了安全,先判断传入的identifier是否为空,若为空返回nil。只是为了安全。还有代码的严谨性。老司机又吹牛逼了,还代码严谨性,我的代码通常都考虑不周全的。。。

然后是通过runtime从绑定的属性中取出一个字典。如果取到的这个字典为空则创建一个字典并绑定。

为什么要创建一个字典呢,因为我们要保证只取到这一个cell。这个cell是为了干什么的呢,就是为了计算高度的,那么我每次计算高度的时候只要有这么一个cell就好了,不要去初始化太多根本不用于显示只用于计算的cell。

然后从字典中取出我们的cell,如果取出的cell为空,则从重用池中取出一个cell。并存入字典。

首先字典和cell的判空,都是针对第一次计算cell高度的时候来的。再次进入的时候都不会为空。
必须要说明的是,从重用池中取出cell的方法我们一定要使用dequeueReusableCellWithIdentifier:这种方式去取,不能以dequeueReusableCellWithIdentifier:indexPath那种方式去取。

就像老司机注释中说的一样,若以indexPath那种方式去取会造成鸡生蛋蛋生鸡的问题,你这程序就进入死循环了。

然后cell.contentView.translatesAutoresizingMaskIntoConstraints = NO;这句是做什么呢?事实上,有两种自动布局方式,autoResizing和autoLayout。
autoResizing是UIView的固有属性。是在IOS6之前用来实现自动布局的属性。当然IOS6之后的autoLayout就要比他强大不少了。

事实上这个属性默认情况下是YES。当为YES时,则我们设置约束是无效的。因为后续我们要手动添加一个约束辅助我们计算,所以这里我们将其设为NO

然后将计算标识符置真,标识这个cell只参与计算高度,不负责展示。以后遇到批量处理cell的时候可以判断这个标识符,让其不参与运算。当然老司机这里只是留了一个接口,实际我们有对其进行处理

通过这个方法,我们就成功的拿到了一个计算高度用的cell。


-(CGFloat)DW_CalculateCellHeightWithCell:(UITableViewCell *)cell

第二个方法:

<code class="objectivec"><span class="hljs-comment" style="color: rgb(136, 0, 0);">///根据cell计算cell的高度</span>-(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGFloat</span>)DW_CalculateCellHeightWithCell:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewCell</span> *)cell{    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGFloat</span> width = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.bounds</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.size</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.width</span>;    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//根据辅助视图校正width</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (cell<span class="hljs-variable" style="color: rgb(102, 0, 102);">.accessoryView</span>) {        width -= cell<span class="hljs-variable" style="color: rgb(102, 0, 102);">.accessoryView</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.bounds</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.size</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.width</span> + <span class="hljs-number" style="color: rgb(0, 102, 102);">16</span>;    }    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">else</span>    {        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">const</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGFloat</span> accessoryWidth[] = {            [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewCellAccessoryNone</span>] = <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>,            [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewCellAccessoryDisclosureIndicator</span>] = <span class="hljs-number" style="color: rgb(0, 102, 102);">34</span>,            [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewCellAccessoryDetailDisclosureButton</span>] = <span class="hljs-number" style="color: rgb(0, 102, 102);">68</span>,            [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewCellAccessoryCheckmark</span>] = <span class="hljs-number" style="color: rgb(0, 102, 102);">40</span>,            [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewCellAccessoryDetailButton</span>] = <span class="hljs-number" style="color: rgb(0, 102, 102);">48</span>        };        width -= accessoryWidth[cell<span class="hljs-variable" style="color: rgb(102, 0, 102);">.accessoryType</span>];    }    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGFloat</span> height = <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>;    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (!cell<span class="hljs-variable" style="color: rgb(102, 0, 102);">.NoAutoSizing</span> && width > <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>) {<span class="hljs-comment" style="color: rgb(136, 0, 0);">//如果不是非自适应模式则添加约束后计算约束后高度</span>        <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSLayoutConstraint</span> * widthConstraint = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSLayoutConstraint</span> constraintWithItem:cell<span class="hljs-variable" style="color: rgb(102, 0, 102);">.contentView</span> attribute:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSLayoutAttributeWidth</span> relatedBy:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSLayoutRelationEqual</span> toItem:<span class="hljs-literal" style="color: rgb(0, 102, 102);">nil</span> attribute:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSLayoutAttributeNotAnAttribute</span> multiplier:<span class="hljs-number" style="color: rgb(0, 102, 102);">1.0</span> constant:width];<span class="hljs-comment" style="color: rgb(136, 0, 0);">//创建约束</span>        [cell<span class="hljs-variable" style="color: rgb(102, 0, 102);">.contentView</span> addConstraint:widthConstraint];<span class="hljs-comment" style="color: rgb(136, 0, 0);">//添加约束</span>        height = [cell<span class="hljs-variable" style="color: rgb(102, 0, 102);">.contentView</span> systemLayoutSizeFittingSize:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UILayoutFittingCompressedSize</span>]<span class="hljs-variable" style="color: rgb(102, 0, 102);">.height</span>;<span class="hljs-comment" style="color: rgb(136, 0, 0);">//计算高度</span>        [cell<span class="hljs-variable" style="color: rgb(102, 0, 102);">.contentView</span> removeConstraint:widthConstraint];<span class="hljs-comment" style="color: rgb(136, 0, 0);">//移除约束</span>    }    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (height == <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>) {<span class="hljs-comment" style="color: rgb(136, 0, 0);">//如果约束错误可能导致计算结果为零,则以自适应模式再次计算</span>        height = [cell sizeThatFits:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGSizeMake</span>(width, <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>)]<span class="hljs-variable" style="color: rgb(102, 0, 102);">.height</span>;    }    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (height == <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>) {<span class="hljs-comment" style="color: rgb(136, 0, 0);">//如果计算仍然为0,则给出默认高度</span>        height = <span class="hljs-number" style="color: rgb(0, 102, 102);">44</span>;    }    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.separatorStyle</span> != <span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewCellSeparatorStyleNone</span>) {<span class="hljs-comment" style="color: rgb(136, 0, 0);">//如果不为无分割线模式则添加分割线高度</span>        height += <span class="hljs-number" style="color: rgb(0, 102, 102);">1.0</span> /[<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIScreen</span> mainScreen]<span class="hljs-variable" style="color: rgb(102, 0, 102);">.scale</span>;    }    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> height;}</code>

首先我们要想计算出我们cell的高度,就需要拿到cell的contentView的实际宽度

1.先拿到cell的宽度

2.根据辅助视图样式校正宽度

如果有自定义辅助视图,则按照自定义辅助视图的宽度去校正,如果没有按照系统辅助视图样式去校正宽度,然后根据宽度计算高度

3.然后如果是使用autoLayout进行自适应计算

这个也是老司机为之后留下的接口,可以控制是否进行一autoLayout进行计算。但实际并没有处理。

这里是添加约束的写法,先添加一个宽度约束,然后让系统根据宽度约束自动计算高度,接着去掉我们添加的约束

4.如果根据约束计算结果错误,则以sizeThatFits去计算高度

5.如果计算结果仍然为零,则给出默认值44

6.判断当前tableView的分割线样式,如果有分割线,还要校正高度

好了,至此你已经计算出这个cell应该有的高度了。


-(CGFloat)DW_CalCulateCellWithIdentifier:(NSString *)identifier configuration:(void(^)(id cell))configuration

最后一个方法:

<code class="objectivec"><span class="hljs-comment" style="color: rgb(136, 0, 0);">///根据重用表示取出cell并操作cell后,计算高度</span>-(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGFloat</span>)DW_CalCulateCellWithIdentifier:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *)identifier                           configuration:(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span>(^)(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">id</span> cell))configuration{    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (!identifier<span class="hljs-variable" style="color: rgb(102, 0, 102);">.length</span>) {        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>;    }    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITableViewCell</span> * cell = [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span> DW_CalculateCellWithIdentifier:identifier];    [cell prepareForReuse];<span class="hljs-comment" style="color: rgb(136, 0, 0);">//放回重用池</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (configuration) {        configuration(cell);<span class="hljs-comment" style="color: rgb(136, 0, 0);">//对cell进行操作</span>   }回重用池    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span> DW_CalculateCellHeightWithCell:cell];}</code>

这个方法就比较简单了,先判断重用标示。如果空,直接返回0。还是只为了安全。

再通过第一个方法取出cell,然后将它放回重用池,以至于下次我们还能取出来这个cell

不要在意我之后还要对cell进行操作,这个重用池只是一个概念,其实并不是什么东西,只是标志着这里面的cell可以用于重用,你完全可以理解成他只是cell的一个标签。所以我之后还是可以继续使用这个cell。不要纠结重用池、取出、放回了,少年,他只是一个概念。

哦对了,另外有一点你要注意,你记不记得老司机说过,进入重用池,是将整个cell存储下来,并没有做其他任何操作

其实你可以重写prepareForReuse这个方法,这里可以做任何你想做的事。比如清除所有子视图。不过有三点你需要注意:

  • 别忘了先写[super prepareForReuse];
  • 真清除所有子视图的时候记得别把contentView也删了
  • 重写之后上面的程序中你要合理的考虑一下[cell prepareForReuse]这句话的位置,反正这么跟你讲,我是没想出来放哪。。

最后就是返回高度了。终于完事了。


留给外界的接口

-(CGFloat)DW_CalculateCellWithIdentifier:(NSString )identifier indexPath:(NSIndexPath )indexPath configuration:(void(^)(id cell))configuration

我只说一个,这是最重要的一个返回高度的接口了,如果每次我们都计算高度,那我们这写法也算是废了,充其量算一个自动返回高度的算法。

所以我们的逻辑应该是先从cache里面中找,如果没有,计算并存储。下次再找这个indexPath的时候就能找到了,正如下面的代码一样。

<code class="objectivec">-(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGFloat</span>)DW_CalculateCellWithIdentifier:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *)identifier                               indexPath:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSIndexPath</span> *)indexPath                           configuration:(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span>(^)(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">id</span> cell))configuration{    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.bounds</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.size</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.width</span> != <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0);">//防止初始宽度为0(如autoLayout初次加载时)</span>    {        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (!identifier<span class="hljs-variable" style="color: rgb(102, 0, 102);">.length</span> || !indexPath) {<span class="hljs-comment" style="color: rgb(136, 0, 0);">//非空判断</span>            <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>;        }        <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> * key = [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.cache</span> makeKeyWithIdentifier:identifier indexPath:indexPath];<span class="hljs-comment" style="color: rgb(136, 0, 0);">//制作key</span>        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> ([<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.cache</span> existInCacheByKey:key]) {<span class="hljs-comment" style="color: rgb(136, 0, 0);">//如果key存在</span>            <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.cache</span> heightFromCacheWithKey:key];<span class="hljs-comment" style="color: rgb(136, 0, 0);">//从字典中取出高</span>        }        <span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGFloat</span> height = [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span> DW_CalCulateCellWithIdentifier:identifier configuration:configuration];<span class="hljs-comment" style="color: rgb(136, 0, 0);">//不存在则计算高度</span>        [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.cache</span> cacheHeight:height byKey:key];<span class="hljs-comment" style="color: rgb(136, 0, 0);">//并缓存</span>        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> height;    }    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>;}</code>

最后关于如何使用

就是在原来返回tableView高度的方法出调用上面那个方法,仅此而已。

特别注意,一定要在方法中先填充数据,一定要在方法中先填充数据,一定要在方法中先填充数据。重要的事情说三遍。否则你永远都是44啊亲们。。


注入数据

我知道今天这个教程看上去很抽象,所以这次我会附上demo的链接。

demo地址

不过老司机还是想说一下自己对demo这件事的看法。

老司机能选择在这里分享一些自己学到的东西,自然就不是一个敝帚自珍的人。然而之所以不爱附上demo链接是因为老司机觉得每次我都已经很详细的在博客中贴出我全部代码而且一句一句讲解真的已经知无不言言无不尽了,我觉得编程这种东西还是得下手敲一遍,看别人的东西看一天也看不懂。所以我更提倡你们自己去敲一遍。如果我把demo链接一放出来,你们直接下载了就去看,就去改,真的没有自己敲一遍学的快。当然有同学实在有需要可以留下邮箱,老司机会给你单独发demo的。


常用套话了,这么贪幕虚荣的老司机不就图你点个喜欢么=。=,觉得好点个喜欢吧。

转载记得附上链接。
http://www.jianshu.com/p/2b192257276f



文/老司机Wicky(简书作者)
原文链接:http://www.jianshu.com/p/2b192257276f
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。


0 0