//设置默认选中
<code class="hljs objectivec has-numbering"> <span class="hljs-built_in">NSIndexPath</span>*defaultIndexPath = [<span class="hljs-built_in">NSIndexPath</span> indexPathForRow:<span class="hljs-number">0</span> inSection:<span class="hljs-number">0</span>];
[<span class="hljs-keyword">self</span> tableView:<span class="hljs-keyword">self</span><span class="hljs-variable">.tableView</span> didSelectRowAtIndexPath:defaultIndexPath];</code>
<code class="hljs objectivec has-numbering"></code>
<code class="hljs objectivec has-numbering">- (<span class="hljs-keyword">void</span>)tableView:(<span class="hljs-built_in">UITableView</span> *)tableView didSelectRowAtIndexPath:(<span class="hljs-built_in">NSIndexPath</span> *)indexPath{ <span class="hljs-comment">//当前cell row </span> <span class="hljs-built_in">NSInteger</span> newRow = [indexPath row]; <span class="hljs-comment">//记录上一次cell row</span> <span class="hljs-built_in">NSInteger</span> oldRow = (<span class="hljs-keyword">self</span><span class="hljs-variable">.lastIndexPath</span> != <span class="hljs-literal">nil</span>) ? [<span class="hljs-keyword">self</span><span class="hljs-variable">.lastIndexPath</span> row] : -<span class="hljs-number">1</span>; <span class="hljs-keyword">if</span> (newRow != oldRow) { <span class="hljs-comment">//选中cell</span> CustomTableViewCell *newcell = [tableView cellForRowAtIndexPath:indexPath]; newcell<span class="hljs-variable">.selectImage</span><span class="hljs-variable">.hidden</span> = <span class="hljs-literal">NO</span>; <span class="hljs-comment">//取消上一次选中cell</span> CustomTableViewCell *oldCell = [tableView cellForRowAtIndexPath:<span class="hljs-keyword">self</span><span class="hljs-variable">.lastIndexPath</span>]; oldCell<span class="hljs-variable">.selectImage</span><span class="hljs-variable">.hidden</span> = <span class="hljs-literal">YES</span>; } <span class="hljs-keyword">self</span><span class="hljs-variable">.lastIndexPath</span> = indexPath;}</code>