<code class="objectivec"><strong><span style="font-size:18px;"><span class="hljs-preprocessor">隐藏分割线、</span><span class="hljs-preprocessor">隐藏多余Cell</span></span></strong> <span class="hljs-comment">//隐藏分割线</span> tableView<span class="hljs-variable">.separatorStyle</span> = <span class="hljs-built_in">UITableViewCellSeparatorStyle</span><span class="hljs-variable">.None</span> <span class="hljs-comment">//隐藏多余的cell</span> tableView<span class="hljs-variable">.tableFooterView</span> = <span class="hljs-built_in">UIView</span>(frame: <span class="hljs-built_in">CGRectZero</span>)</code>
<code class="swift"><span style="font-size:18px;"><strong><span class="hljs-comment">分割线头部顶到底、分割线颜色</span></strong></span> <span class="hljs-comment">//启动、旋转、视图大小位置发生改变、增加子视图等都会调用</span> <span class="hljs-keyword">override</span> <span class="hljs-func"><span class="hljs-keyword">func</span> <span class="hljs-title">viewDidLayoutSubviews</span><span class="hljs-params">()</span></span> { tableView.separatorInset = <span class="hljs-type">UIEdgeInsetsZero</span> tableView.layoutMargins = <span class="hljs-type">UIEdgeInsetsZero</span> <span class="hljs-comment">//articleTableView.separatorColor = UIColor.redColor() //分割线颜色</span> } <span class="hljs-comment">//没当cell即将出现屏幕时候都会调用此方法</span> <span class="hljs-func"><span class="hljs-keyword">func</span> <span class="hljs-title">tableView</span><span class="hljs-params">(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)</span></span> { cell.separatorInset = <span class="hljs-type">UIEdgeInsetsZero</span> cell.layoutMargins = <span class="hljs-type">UIEdgeInsetsZero</span> }</code>
<code class="objectivec"><strong><span style="font-size:18px;"><span class="hljs-preprocessor">点击后效果 Cell 背景等更改</span></span></strong> <span class="hljs-comment">//点击Cell时,没有点击效果</span> cell<span class="hljs-variable">.selectionStyle</span> = <span class="hljs-built_in">UITableViewCellSelectionStyle</span><span class="hljs-variable">.None</span> <span class="hljs-comment">//系统默认的颜色 .Blue蓝色-默认 .Grap灰色 .None 无色</span> <span class="hljs-comment">//点击Cell时,自定义选中后的背景视图</span> <span class="hljs-comment">//背景颜色</span> cell<span class="hljs-variable">.selectedBackgroundView</span> = <span class="hljs-built_in">UIView</span>() cell<span class="hljs-variable">.selectedBackgroundView</span>?<span class="hljs-variable">.backgroundColor</span> = <span class="hljs-built_in">UIColor</span><span class="hljs-variable">.clearColor</span>() <span class="hljs-comment">//背景图片</span> cell<span class="hljs-variable">.selectedBackgroundView</span> = <span class="hljs-built_in">UIImageView</span>(image: <span class="hljs-built_in">UIImage</span>(named: article<span class="hljs-variable">.avatarImage</span>)) <span class="hljs-comment">//cell 右边的辅助的提示</span> cell<span class="hljs-variable">.accessoryType</span> = <span class="hljs-variable">.DisclosureIndicator</span> <span class="hljs-comment">//></span> <span class="hljs-comment">//.Checkmark //√ .DetailDisclosureButton // ! > .DetailButton // !</span></code>
<code class="cpp"><strong><span style="font-size:18px;"><span class="hljs-preprocessor">类似button点击效果闪一下</span></span></strong> <span class="hljs-comment">//在 didSelectRowAtIndexPath 方法内使用</span> <span class="hljs-comment">//点击Cell时 一闪而过 适合转场时候交互 - </span> tableView.deselectRowAtIndexPath(indexPath, animated: <span class="hljs-literal">false</span>) <span class="hljs-comment">// - true 动画慢吞吞,适合不转场时</span></code>
<code class="swift"><span class="hljs-number"></span><strong><span style="font-size:18px;"><span class="hljs-type">Tableview</span>视图<span class="hljs-type">Cell</span>进入动画 从底部往上弹</span></strong> <span class="hljs-comment">//加载动画 Cell 往上冲 在 viewWillAppear 中使用</span> <span class="hljs-func"><span class="hljs-keyword">func</span> <span class="hljs-title">animateTable</span><span class="hljs-params">()</span></span> { <span class="hljs-keyword">self</span>.tableView.reloadData() <span class="hljs-keyword">let</span> cells = tableView.visibleCells <span class="hljs-keyword">let</span> tableHeight = tableView.bounds.size.height <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> cells { <span class="hljs-keyword">let</span> cell: <span class="hljs-type">UITableViewCell</span> = i <span class="hljs-keyword">as</span> <span class="hljs-type">UITableViewCell</span> cell.transform = <span class="hljs-type">CGAffineTransformMakeTranslation</span>(<span class="hljs-number">0</span>, tableHeight) } <span class="hljs-keyword">var</span> index = <span class="hljs-number">0</span> <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> cells { <span class="hljs-keyword">let</span> cell: <span class="hljs-type">UITableViewCell</span> = a <span class="hljs-keyword">as</span> <span class="hljs-type">UITableViewCell</span> <span class="hljs-type">UIView</span>.animateWithDuration(<span class="hljs-number">1.0</span>, delay: <span class="hljs-number">0.05</span> * <span class="hljs-type">Double</span>(index), usingSpringWithDamping: <span class="hljs-number">0.8</span>, initialSpringVelocity: <span class="hljs-number">0</span>, options: [], animations: { cell.transform = <span class="hljs-type">CGAffineTransformMakeTranslation</span>(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>); }, completion: <span class="hljs-literal">nil</span>) index += <span class="hljs-number">1</span> } }</code>
<code class="swift"><strong><span style="font-size:18px;">点击cell展开样式</span></strong> <span class="hljs-comment">//比如一个使用了SB约束好的label ,tag = 666 把他 属性 lines = 0 与 1转换 即显示单行或多行</span> <span class="hljs-comment">// -1.记得使用SB设置好约束</span> <span class="hljs-keyword">override</span> <span class="hljs-func"><span class="hljs-keyword">func</span> <span class="hljs-title">viewDidLoad</span><span class="hljs-params">()</span></span> { <span class="hljs-keyword">super</span>.viewDidLoad() <span class="hljs-comment">// 0.启动自动布局计划</span> tableView.estimatedRowHeight = <span class="hljs-number">44</span> tableView.rowHeight = <span class="hljs-type">UITableViewAutomaticDimension</span> } <span class="hljs-comment">// 1.先声明的一个字典 - 记录每个cell展收状态</span> <span class="hljs-keyword">var</span> dict:<span class="hljs-type">Dictionary</span><<span class="hljs-type">Int</span>,<span class="hljs-type">Bool</span>> = [:] <span class="hljs-comment">// 2.根据字典显示cell状态</span> <span class="hljs-keyword">override</span> <span class="hljs-func"><span class="hljs-keyword">func</span> <span class="hljs-title">tableView</span><span class="hljs-params">(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)</span></span> -> <span class="hljs-type">UITableViewCell</span> { <span class="hljs-keyword">let</span> cell = tableView.dequeueReusableCellWithIdentifier(<span class="hljs-string">"Cell"</span>, forIndexPath: indexPath) <span class="hljs-keyword">let</span> label = cell.contentView.viewWithTag(<span class="hljs-number">666</span>) <span class="hljs-keyword">as</span>! <span class="hljs-type">UILabel</span> label.text = <span class="hljs-string">"本文导航 \n 1.隐藏分割线\n 2.隐藏多余Cell\n 3.分割线头部顶到底、分割线颜色\n 4.自定义点击后效果 Cell 背景等更改\n 5.类似button点击效果 Cell - 闪一下\n 6.Tableview视图Cell进入动画 从底部往上弹\n 7.TableviewCell使用SB约束 自动布局 \n 8. cell 点击展开"</span> <span class="hljs-keyword">if</span> dict[indexPath.row] == <span class="hljs-literal">false</span> { label.numberOfLines = <span class="hljs-number">0</span> } <span class="hljs-keyword">else</span> { label.numberOfLines = <span class="hljs-number">1</span> } <span class="hljs-keyword">return</span> cell } <span class="hljs-comment">// 3. 在 beginUpdates() - endUpdates() 放代码 有连续动画效果</span> <span class="hljs-keyword">override</span> <span class="hljs-func"><span class="hljs-keyword">func</span> <span class="hljs-title">tableView</span><span class="hljs-params">(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)</span></span> { tableView.deselectRowAtIndexPath(indexPath, animated: <span class="hljs-literal">true</span>) <span class="hljs-comment">//点击闪动效果</span> <span class="hljs-keyword">let</span> cell = tableView.cellForRowAtIndexPath(indexPath) <span class="hljs-keyword">let</span> label = cell!.contentView.viewWithTag(<span class="hljs-number">666</span>) <span class="hljs-keyword">as</span>! <span class="hljs-type">UILabel</span> tableView.beginUpdates() <span class="hljs-comment">//开始</span> <span class="hljs-keyword">if</span> label.numberOfLines == <span class="hljs-number">0</span> { label.numberOfLines = <span class="hljs-number">1</span> dict[indexPath.row] = <span class="hljs-literal">true</span> } <span class="hljs-keyword">else</span> { label.numberOfLines = <span class="hljs-number">0</span> dict[indexPath.row] = <span class="hljs-literal">false</span> } tableView.endUpdates() }</code>
<code class="objectivec"><strong><span style="font-size:18px;"><code class="objectivec"><span class="hljs-preprocessor">没有数据时候提示 可以自行加入空数据时候显示</span></code> </span></strong> <span class="hljs-comment"> //判断有没有数据显示 提示</span> func showIfNoAnswer() { let imageView = <span class="hljs-built_in">UIImageView</span>(frame: <span class="hljs-built_in">CGRectMake</span>(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">60</span>, <span class="hljs-number">60</span>)) let image = <span class="hljs-built_in">UIImage</span>(named: <span class="hljs-string">"sad"</span>) imageView<span class="hljs-variable">.image</span> = image?<span class="hljs-variable">.imageWithRenderingMode</span>(<span class="hljs-variable">.AlwaysTemplate</span>) imageView<span class="hljs-variable">.tintColor</span> = <span class="hljs-built_in">UIColor</span><span class="hljs-variable">.grayColor</span>() imageView<span class="hljs-variable">.center</span> = <span class="hljs-built_in">CGPointMake</span>(<span class="hljs-keyword">self</span><span class="hljs-variable">.view</span><span class="hljs-variable">.center</span><span class="hljs-variable">.x</span>, <span class="hljs-number">145</span>) imageView<span class="hljs-variable">.tag</span> = <span class="hljs-number">33</span> <span class="hljs-comment">// 方便 remove</span> <span class="hljs-keyword">self</span><span class="hljs-variable">.view</span><span class="hljs-variable">.addSubview</span>(imageView) let label = <span class="hljs-built_in">UILabel</span>(frame: <span class="hljs-variable">.zero</span>) label<span class="hljs-variable">.text</span> = <span class="hljs-string">"加载失败"</span> label<span class="hljs-variable">.font</span> = <span class="hljs-built_in">UIFont</span>(name: <span class="hljs-string">"New Gulim"</span>, size: <span class="hljs-number">20</span>) label<span class="hljs-variable">.textColor</span> = <span class="hljs-built_in">UIColor</span><span class="hljs-variable">.grayColor</span>() label<span class="hljs-variable">.textAlignment</span> = <span class="hljs-variable">.Center</span> label<span class="hljs-variable">.tag</span> = <span class="hljs-number">3</span> label<span class="hljs-variable">.sizeToFit</span>() label<span class="hljs-variable">.backgroundColor</span> = <span class="hljs-built_in">UIColor</span><span class="hljs-variable">.clearColor</span>() label<span class="hljs-variable">.center</span> = <span class="hljs-built_in">CGPointMake</span>(<span class="hljs-keyword">self</span><span class="hljs-variable">.view</span><span class="hljs-variable">.center</span><span class="hljs-variable">.x</span>, <span class="hljs-number">200</span>) view<span class="hljs-variable">.addSubview</span>(label) } }</code>