树鬼 發表於 2024-11-13 09:46:00

iOS开发- tableView的协议

<p>在使用 <code>UITableView</code> 时,必须实现的协议主要包括以下几个</p>
<h3 id="1-uitableviewdatasource-协议">1. <strong><code>UITableViewDataSource</code> 协议</strong></h3>
<p>这是最重要的协议,用于提供数据给 <code>UITableView</code>。没有这个协议,<code>UITableView</code> 是无法显示任何内容的。</p>
<h4 id="必须实现的方法">必须实现的方法:</h4>
<ul>
<li>
<p><strong><code>tableView:numberOfRowsInSection:</code></strong>:返回给定 section 中的行数。</p>
<pre><code class="language-objc">- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
</code></pre>
</li>
<li>
<p><strong><code>tableView:cellForRowAtIndexPath:</code></strong>:返回对应 <code>indexPath</code> 的单元格(<code>UITableViewCell</code>)。</p>
<pre><code class="language-objc">- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
</code></pre>
</li>
</ul>
<p>这两个方法是 <code>UITableViewDataSource</code> 协议中最核心的必须实现的方法。</p>
<h4 id="可选的方法">可选的方法:</h4>
<ul>
<li>
<p><strong><code>tableView:titleForHeaderInSection:</code></strong>:返回指定 section 的标题(用于表头)。</p>
<pre><code class="language-objc">- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
</code></pre>
</li>
<li>
<p><strong><code>tableView:titleForFooterInSection:</code></strong>:返回指定 section 的标题(用于表尾)。</p>
<pre><code class="language-objc">- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
</code></pre>
</li>
<li>
<p><strong><code>tableView:canEditRowAtIndexPath:</code></strong>:指示是否允许编辑某一行。</p>
<pre><code class="language-objc">- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
</code></pre>
</li>
<li>
<p><strong><code>tableView:canMoveRowAtIndexPath:</code></strong>:指示是否允许移动某一行。</p>
<pre><code class="language-objc">- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
</code></pre>
</li>
</ul>
<h3 id="2-uitableviewdelegate-协议">2. <strong><code>UITableViewDelegate</code> 协议</strong></h3>
<p><code>UITableViewDelegate</code> 协议用于处理表视图的交互,例如行选择、行删除、行移动等。这个协议的实现通常是为了增强用户体验。</p>
<h4 id="必须实现的方法-1">必须实现的方法:</h4>
<p>实际上,<code>UITableViewDelegate</code> 中并没有严格“必须”实现的方法,但是通常会实现以下几种常见方法:</p>
<ul>
<li>
<p><strong><code>tableView:didSelectRowAtIndexPath:</code></strong>:当用户点击某一行时调用。</p>
<pre><code class="language-objc">- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
</code></pre>
</li>
</ul>
<h4 id="可选的方法-1">可选的方法:</h4>
<ul>
<li>
<p><strong><code>tableView:heightForRowAtIndexPath:</code></strong>:设置行高。</p>
<pre><code class="language-objc">- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
</code></pre>
</li>
<li>
<p><strong><code>tableView:heightForHeaderInSection:</code></strong>:设置表头的高度。</p>
<pre><code class="language-objc">- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
</code></pre>
</li>
<li>
<p><strong><code>tableView:heightForFooterInSection:</code></strong>:设置表尾的高度。</p>
<pre><code class="language-objc">- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
</code></pre>
</li>
<li>
<p><strong><code>tableView:viewForHeaderInSection:</code></strong>:自定义表头视图。</p>
<pre><code class="language-objc">- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
</code></pre>
</li>
<li>
<p><strong><code>tableView:viewForFooterInSection:</code></strong>:自定义表尾视图。</p>
<pre><code class="language-objc">- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
</code></pre>
</li>
<li>
<p><strong><code>tableView:didDeselectRowAtIndexPath:</code></strong>:当用户取消选择某一行时调用。</p>
<pre><code class="language-objc">- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath;
</code></pre>
</li>
</ul>
<h3 id="3-uitableviewdragdelegate-和-uitableviewdropdelegateios-11-及以上">3. <strong><code>UITableViewDragDelegate</code> 和 <code>UITableViewDropDelegate</code>(iOS 11 及以上)</strong></h3>
<p>这些协议主要用于拖放操作(<code>drag and drop</code>)功能,适用于需要支持拖动排序或拖拽添加数据的表格。</p>
<ul>
<li><strong><code>UITableViewDragDelegate</code></strong>:用于处理行拖拽操作。</li>
<li><strong><code>UITableViewDropDelegate</code></strong>:用于处理行的接收(drop)操作。</li>
</ul>
<p>这些协议方法在使用拖放功能时非常有用,但它们是可选的,只在支持拖放操作时才需要实现。</p>
<h3 id="4-uitableviewdatasourceprefetchingios-10-及以上">4. <strong><code>UITableViewDataSourcePrefetching</code>(iOS 10 及以上)</strong></h3>
<p>如果表格需要进行数据预加载,<code>UITableViewDataSourcePrefetching</code> 协议非常有用。这个协议允许提前加载即将显示的行的数据(例如,提前加载图片或远程数据)。</p>
<ul>
<li>
<p><strong><code>tableView:prefetchRowsAtIndexPaths:</code></strong>:预加载数据的方法。</p>
<pre><code class="language-objc">- (void)tableView:(UITableView *)tableView prefetchRowsAtIndexPaths:(NSArray&lt;NSIndexPath *&gt; *)indexPaths;
</code></pre>
</li>
<li>
<p><strong><code>tableView:cancelPrefetchingForRowsAtIndexPaths:</code></strong>:取消预加载的数据的方法。</p>
<pre><code class="language-objc">- (void)tableView:(UITableView *)tableView cancelPrefetchingForRowsAtIndexPaths:(NSArray&lt;NSIndexPath *&gt; *)indexPaths;
</code></pre>
</li>
</ul>
<h3 id="总结">总结</h3>
<ul>
<li>
<p><strong>必需的协议</strong>:</p>
<ul>
<li><code>UITableViewDataSource</code>:主要负责提供数据。</li>
<li><code>UITableViewDelegate</code>:主要负责处理交互(例如行的选择、编辑、行高等)。</li>
</ul>
</li>
<li>
<p><strong>可选的协议</strong>:</p>
<ul>
<li><code>UITableViewDragDelegate</code> 和 <code>UITableViewDropDelegate</code>(用于拖放操作)。</li>
<li><code>UITableViewDataSourcePrefetching</code>(用于数据预加载)。</li>
</ul>
</li>
</ul>
<p>大部分时候,只需要实现 <code>UITableViewDataSource</code> 和 <code>UITableViewDelegate</code> 中的几个关键方法。如果还需要自定义其他功能(例如拖放、数据预加载),可以根据需求再实现其他协议的方法。</p>
<p>而使用<code>UIcollectionView</code>也是相同的。</p><br><br>
来源:https://www.cnblogs.com/jianqiu/p/18543166
頁: [1]
查看完整版本: iOS开发- tableView的协议