ios - UITableView headerView 滚动偏移量

标签 ios objective-c uitableview cocoa-touch uisearchbar

我遇到了 UITableView 及其属性 tableHeaderView 的问题。

我想让 tableHeaderView 表现得像 UISearchBar,即内容偏移量应该是 tableHeaderView 的偏移量。 当表格 View 无法填充 View 的框架时,设置 contentOffset 等没有帮助。

这是当前行为的屏幕截图:

current behavior
(来源:tubtub.de)

我希望它如何:

desired behavior
(来源:tubtub.de)

我在 viewDidLoad 中插入 headerView,如下所示:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    headerView.backgroundColor = [UIColor redColor];
    self.tableView.tableHeaderView = headerView;
}

非常感谢任何帮助或提示。谢谢!

编辑:我通过使用 UIScrollViewDelegate 和子类化 UITableView 使其工作。在 github repo 上查看提供。

最佳答案

您可以使用 setContentOffset: by headerView 高度。 这个 contentOffset 只会在你有足够数量的数据被你的 headerView 高度关闭时发生。即)如果 tableView 不可滚动,因为您的屏幕截图中只有很少的数据,则 headerView 仍然可见。但是,如果您有大量无法在屏幕上显示的数据,它将具有 contentOffset。

用 20 行试试这个。你会明白我的意思。

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
headerView.backgroundColor = [UIColor redColor];
self.tableView.tableHeaderView = headerView;
[self.tableView setContentOffset:CGPointMake(0, 44)];

只有 3 行时,此 contentOffset 将不起作用,并且在您的情况下这可能是您希望的行为,因为您不想在有额外空间显示时隐藏 searchBar。

关于ios - UITableView headerView 滚动偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23667518/

相关文章:

ios - 在 iPhone 上使用 OAuth2 访问 Google Calendar API

ios - UICollectionViewCell 是否有 viewWillAppear 或我可以将数据传递给的东西

ios - 更好的可点击表格 View 单元格的可访问性标签

ios - 将 subview Controller View 添加到动态 UITableViewCell

ios - 当 NSAttributedString 属性的范围嵌套/重叠时,它们会堆叠吗?

iphone - 我怎样才能从标签中获取对象?

ios - UITableView 单元格重复

ios - 快速向后滑动时,UITableViewCell 不会被取消选择

ios - UICollectionView ScrollView 从顶部和底部有边距

iOS通过ASP上传图片到服务器