菜鸟教程小白 发表于 2022-12-11 20:45:55

objective-c - UITableView 中心单元格详细信息


                                            <p><p>我正在尝试将 <code>UITableView</code> 与 <code>UILabel</code> 同步,以确保它们显示相同的数据;当然,最终情况会有所不同,但为了测试,这是我需要做的。</p>

<p> <img src="/image/GWT2c.png" alt="enter image description here"/> </p>

<p>看到箭头了吗?我希望那个中间单元格(来自 px44-88)在 <code>UILabel</code> 中显示 <code>cell.textLabel.text</code> 当它是“中间单元格”时。 </p>

<p>我尝试使用 <code>- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath</code> 但我遇到了很多问题,我想我会来这里询问是否有人有更好的方法来做到这一点。我不确定它是否会有所作为,但我正在使用 <code>NSFetchedResultsController</code> 来填充我的 <code>UITableView</code>。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>UITableView 是 UIScrollView 的子类,所以可能你可以使用 UIScrollViewDelegate </p>

<p><code>- (void)scrollViewDidScroll:(UIScrollView *)scrollView</code> </p>

<p>成员来检测当前哪个单元格在中间。</p>

<p>即假设您有一个没有部分的普通表格,并且 tableView 是表格的导出,标签是标签的导出,这将在您的 Controller 中起作用:</p>

<pre><code>    - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
      int row = (scrollView.contentOffset.y + tableView.frame.size.height / 2) / 44;

      NSIndexPath *indexPath = ;

      UITableViewCell *cell = ;
      label.text = cell.textLabel.text;
}
</code></pre>

<p>当然,你需要做一些滚动才能让它工作;) </p>

<p>您可以使用 visibleCells 方法获取非普通表格的可见单元格布局,并使用它来检测表格中间的单元格。</p></p>
                                   
                                                <p style="font-size: 20px;">关于objective-c - UITableView 中心单元格详细信息,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/8557596/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/8557596/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: objective-c - UITableView 中心单元格详细信息