菜鸟教程小白 发表于 2022-12-12 18:16:42

ios - UITableView contentOffset 行为异常


                                            <p><p>我有一个 UIViewController,它有多个从屏幕外输入的 UIView。我没有在 UIViewController 中初始化和设计所有这些 UIView,而是创建了单独的 nib 文件以及相应的 .h/.m 文件,这些文件刚刚被预加载。 </p>

<p>在其中一个自定义 UIView 中,我有一个带有标题 UIView 的 UITableView。我试图将这个标题隐藏在我在原始 UIViewController 中的另一个组件下,这个 UIView 将滑入。我最初使用带有 <code>;</code> 的示例项目解决了这个问题,但这只是在 UITableViewController 中。 </p>

<p>由于某种原因,这不适用于子类 UIView。我已经尝试在 <code>- (id)initWithCoder:(NSCoder *)aDecoder</code> 和 <code>- (void)layoutSubviews</code> 中设置 tableView 的内容偏移量,并且我一直在检查是否偏移量实际上是由 </p> 在 <code>tableView:cellForRowAtIndexPath:</code> 中设置的

<pre><code>NSLog(@&#34;tableView Content Offset: %@&#34;, NSStringFromCGPoint(self.tableView.contentOffset));
</code></pre>

<p>但它只是返回</p>

<p><code>tableView 内容偏移量:{0, 0}</code></p>

<p>有趣的是,如果我在 <code>tableView:cellForRowAtIndexPath:</code> 中设置 contentOffset,它会起作用!但我知道我不应该把它放在那里。有人可以向我解释从自定义 nib 加载 UITableView 时我应该在哪里以及如何实现这个,它只是一个子类 UIView?如果您认为有帮助,我可以发布所有代码。</p>

<p><strong>其他信息:</strong><br/>
问题:<em>你是如何从 nib 加载 View 的? – MAB</em> </p>

<pre><code>UIView *newView = [[ loadNibNamed:@&#34;ProfileCostTableViewController&#34; owner:self options:nil] objectAtIndex:0];
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我将 <code>UITableView</code> 子类化以查看 contentOffset 何时恢复到 {0,0}。它似乎是从私有(private)方法调用 ios7 :<code>_adjustContentOffsetIfNecessary</code>。由于没有记录此方法,我看不到如何防止它更改偏移量,因此我认为解决方法是在延迟后或在 <code>- (int) tableView:(UITableView *)tableView numberOfRowsInSection 中更改偏移量:(NSInteger)部分</code>.</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UITableView contentOffset 行为异常,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/21358882/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/21358882/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UITableView contentOffset 行为异常