ios UITableView contentOffSet 无法正常工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15222186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:34:31  来源:igfitidea点击:

UITableView contentOffSet is not working properly

iphoneiosuitableviewcontentoffset

提问by JiteshW

In viewWillAppear, I have added UISearchBaras my headerview of UITableView. When view loads, I hides UISearchbarunder UINavigationBarusing contentOffSet of UITableView. When user pull down the tableview the searchbar gets displayed.

viewWillAppear,我加UISearchBar为我的headerview UITableView。当视图加载,我隐藏UISearchbarUINavigationBar使用的contentOffSet UITableView。当用户下拉 tableview 时,搜索栏就会显示出来。

After adding headerview I hides it using below code.

添加 headerview 后,我使用下面的代码隐藏它。

self.tableView.contentOffset = CGPointMake(0, 40); //My searhbar height is 40

But at times contentOffSetis not hiding headerview. What can be the mistake.

但有时contentOffSet并没有隐藏headerview。可能是什么错误。

回答by maz

This worked for me:

这对我有用:

// contentOffset will not change before the main runloop ends without queueing it, for iPad that is
dispatch_async(dispatch_get_main_queue(), ^{
    // The search bar is hidden when the view becomes visible the first time
    self.tableView.contentOffset = CGPointMake(0, CGRectGetHeight(self.searchBar.bounds));
});

Put it in your -viewDidLoad or -viewWillAppear

把它放在你的 -viewDidLoad 或 -viewWillAppear

回答by Matthew Cawley

In earlier versions of the iOS SDK, methods such as viewWillAppearran on the main thread, they now run on a background thread which is why the issue now occurs.

在较早版本的 iOS SDK 中,诸如viewWillAppear在主线程上运行的方法现在在后台线程上运行,这就是现在出现问题的原因。

Most callbacks tend to fire on a background thread so always check for thread safety when doing UI calls.

大多数回调倾向于在后台线程上触发,因此在执行 UI 调用时始终检查线程安全性。

Dispatch the change to the content offset on the main thread:

在主线程上分发对内容偏移量的更改:

Objective C

目标 C

dispatch_async(dispatch_get_main_queue(), ^{
    CGPoint offset = CGPointMake(0, self.searchBar.bounds.height)
    [self.tableView setContentOffset:offset animated:NO];
});

Swift 3

斯威夫特 3

DispatchQueue.main.async {
            let offset = CGPoint.init(x: 0, y: self.searchBar.bounds.height)
            self.tableView.setContentOffset(offset, animated: false)
        }

回答by deRonbrown

Not sure what the reason is (don't have time to research it right now), but I solved this issue by using performSelectorafter a delay of 0. Example:

不知道是什么原因(现在没时间研究),但我通过performSelector延迟0后使用解决了这个问题。例如:

- (void)viewWillAppear:(BOOL)animated {
    ...
    [self performSelector:@selector(hideSearchBar) withObject:nil afterDelay:0.0f];
}

- (void)hideSearchBar {
    self.tableView.contentOffset = CGPointMake(0, 44);
}

回答by Kishor

This worked for me.

这对我有用。

self.tableView.beginUpdates()
self.tableView.setContentOffset( CGPoint(x: 0.0, y: 0.0), animated: false)
self.tableView.endUpdates()

Objective-C :

目标-C:

[_tableView beginUpdates];
[_tableView setContentOffset:CGPointMake(0, 0)];
[_tableView endUpdates];

回答by Blank

I fixed it with layoutIfNeeded()

我用layoutIfNeeded()修复了它

    self.articlesCollectionView.reloadData()
    self.articlesCollectionView.layoutIfNeeded()
    self.articlesCollectionView.setContentOffset(CGPoint(x: 0, y: 40), animated: false)

回答by Mexx

In my case the problem was that I called

就我而言,问题是我打电话

 [_myGreatTableView reloadData];

before getting the _tableView.contentOffset.y. This always returned '0'.

在获得_tableView.contentOffset.y. 这总是返回“0”。

So I switched the order of these methodsand now it works.

所以我改变了这些方法的顺序,现在它可以工作了。

This is quite a strange behavior in my opinion, because offset returned 0 but the UI still kept the scroll position of the table.

在我看来,这是一种很奇怪的行为,因为 offset 返回 0 但 UI 仍然保持表格的滚动位置。

回答by techniao

It may be because of the "Adjust Scroll View Insets" attribute on the View Controller. See this: https://stackoverflow.com/a/22022366

这可能是因为视图控制器上的“调整滚动视图插入”属性。看到这个:https: //stackoverflow.com/a/22022366

EDIT: Be sure to check the value of 'contentInset' to see what's happening, since this also has an effect on the scroll view. This value is changed after viewWillAppear: when "Adjust Scroll View Insets" is set, which seems to be what others are trying to avoid by using dispatch queues and the like.

编辑:请务必检查“contentInset”的值以查看发生了什么,因为这也会对滚动视图产生影响。此值在 viewWillAppear: 设置“调整滚动视图插入”后更改,这似乎是其他人试图通过使用调度队列等来避免的。

回答by Kappe

After one hour of tests the only way that works 100% is this one:

经过一小时的测试,100% 工作的唯一方法是:

-(void)hideSearchBar
{
    if([self.tableSearchBar.text length]<=0 && !self.tableSearchBar.isFirstResponder)
    {
        self.tableView.contentOffset = CGPointMake(0, self.tableSearchBar.bounds.size.height);
        self.edgesForExtendedLayout = UIRectEdgeBottom;
    }
}

-(void)viewDidLayoutSubviews
{
    [self hideSearchBar];
}

with this approach you can always hide the search bar if is empty

使用这种方法,如果搜索栏为空,您可以随时隐藏搜索栏

回答by Abdurrahman Mubeen Ali

The line of code is working as it should.

这行代码正常工作。

Explaination of contentOffsetof a UITableView:

的阐释contentOffsetUITableView

For example, if you have a table view of height 100. Its content may be more than it's view, say 150. contentOffsetwill tell the table from where in the content to start from. Say contentOffset = (0, 40), the content will be shown after 40 of it's height.

例如,如果你有一个高度为 100 的表格视图。它的内容可能比它的视图多,比如 150。contentOffset将告诉表格从内容中的哪里开始。说contentOffset = (0, 40),内容将在其高度的 40 之后显示。

Note: Once the content is scrolled, there is no affect of the previously set contentOffset.

注意:一旦内容滚动,之前设置的contentOffset.

回答by parilogic

You need to hide the search bar by yourself when you scroll the table. So don't put it as a UITableViewheader. You could hide it by setting its height to zero. That way if your table view is set to auto resize, it will expand.

滚动表格时需要自己隐藏搜索栏。所以不要把它作为UITableView标题。您可以通过将其高度设置为零来隐藏它。这样,如果您的表视图设置为自动调整大小,它将扩展。