UiTableView 标题没有消失 [英] UiTableView header not disappearing
问题描述
我有一个 UITableView 配置为普通样式,所以我可以将标题视图卡在表格顶部,直到另一个标题将其拉走.
I have a UITableView configured as plain style so I can have the header views stuck on the top of the table until another header pulls it away.
问题是:如果我有一个标题卡在屏幕顶部,并且我以编程方式滚动到表格的另一部分(该标题根本不应该出现在那里),则 UIView 不会被关闭.IE.如果我再次滚动到表格的那部分,表格的那部分上就会看到那个标题的影子.
The problem is: If I have a header stuck on the top of the screen, and I programmatically scroll to another part of the table (where that header should not appear at all), that UIView will not be dismissed. I.e. if I scroll again to that part of the table, a ghost of that header will be visible on that part of the table.
我已经实现了方法 - (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(nonnull UIView *)view forSection:(NSInteger)section
以了解发生了什么.我发现如果我手动滚动直到标题被拉离屏幕,这个委托就会被调用.但是如果我以编程方式滚动,则不会调用委托.
I've implemented the method - (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(nonnull UIView *)view forSection:(NSInteger)section
to understand what is happening. I found that if I manually scroll until a header is pull away of the screen, this delegate is called. But if I scroll programmatically, the delegate is not called.
顺便说一下,我尝试使用两种不同的方法以编程方式滚动,问题是一样的.
By the way, I tried scrolling programmatically using two different methods, and the problem is the same.
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated;
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
我可以想象的一种解决方法是实现 - (void)scrollViewDidScroll:(UIScrollView *)scrollView;
,过滤可见屏幕之外的所有标题视图,并将它们从超级视图中删除.我可能可以让它工作,但我想知道是否有其他更好的解决方案.
One workaround that I can imagine is implementing - (void)scrollViewDidScroll:(UIScrollView *)scrollView;
, filtering all the header views that are outside the visible screen, and removing them from superview. I can probably make it work, but I would like to know if there is any other better solution.
如果我调用 - (void)setContentOffset:(CGPoint)contentOffset animation:(BOOL)animated;
和 animation = YES,错误不会发生.我可以使用这个解决方案,但在某些情况下我真的希望在没有动画的情况下滚动.
If I call - (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated;
with animated = YES, the bug does not happen. I can go with this solution, but I really would like in some cases to scroll without animation.
推荐答案
TL;DR
不要在beginUpdates
和endUpdates
之间显式滚动表格视图.
Do NOT explicitly scroll the table view between beginUpdates
and endUpdates
.
说明
我使用 NSFetchedResultsController 来填充表格.这些是我对 NSFetchedResultsControllerDelegate 的一些方法的实现.
I'm using NSFetchedResultsController to populate the table. These are my implementations for some of the methods of NSFetchedResultsControllerDelegate.
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[_conversationTableView beginUpdates];
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[_conversationTableView endUpdates];
}
问题是 endUpdates 进行了一系列调用,最终调用了我的方法 [self scrollToBottom]
(实际上这是一个非常丑陋的代码).顾名思义,这个方法调用 - (void)setContentOffset:(CGPoint)contentOffset animation:(BOOL)animated;
将表格视图滚动到表格底部.
The problem is that endUpdates was making a chain of calls that ended calling my method [self scrollToBottom]
(which was a very ugly code actually). This method, as the name says, calls - (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated;
to scroll the table view to the bottom of the table.
在 beginUpdates
- endUpdates
期间显式滚动表格是我整个问题的罪魁祸首.
The explicit scrolling of the table during a beginUpdates
- endUpdates
was the culprit of my whole problem.
解决方案
仅在完成 endUpdates 后滚动表格视图.
Scrolling the table view only after finishing endUpdates.
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[_conversationTableView beginUpdates];
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[_conversationTableView endUpdates];
[self scrollToBottom];
}
旁注
这也解决了滚动时表格视图有时闪烁的问题.
This also fixed a problem where the table view was sometimes flickering when scrolling.
这篇关于UiTableView 标题没有消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!