【问题标题】:disable tableHeaderView (Not to be confused with section header) scrolling禁用 tableHeaderView(不要与节标题混淆)滚动
【发布时间】:2013-09-01 21:33:20
【问题描述】:

是否可以禁用 tableHeaderView 的滚动(不要与节标题混淆)。现在每当我滚动表格时,tableHeaderView 中的视图也会滚动。

我在做什么:

  1. 我有一个 UITableViewController 的子类。
  2. 在情节提要中,我使用的是静态表格视图。
  3. 表格样式是分组的,我添加了 8 个部分,每个部分都有一行。
  4. 在第 1 部分的顶部,添加了一个视图,即 tableHeaderView。

我想在滚动表格时禁用标题为“Profile”的视图滚动。

PS: 我知道如果我从 UIViewController 而不是 UITableViewController 子类化我的类,这是可以实现的。 但我不想 UIViewController 因为我使用情节提要设计静态单元格,如果我使用 UIViewController 而不是 UITableViewController 则编译器会抛出警告“静态表视图仅在嵌入 UITableViewController 实例时才有效”

请让我知道哪种方法是实现此目的的最佳方法。是否可以使用我当前的方法禁用 tableHeader 的滚动,或者我需要改用 UIViewController。

【问题讨论】:

  • 您可能会发现这个答案很有用...stackoverflow.com/a/6961973/1757581
  • @hacker2007 这是其中一种方法,但在我的情况下它不起作用,因为我使用情节提要设计静态单元格,如果我使用 UIViewController 而不是 UITableViewController 则编译器会抛出警告“静态表视图仅在嵌入 UITableViewController 实例时有效"
  • 在故事板编辑器中,您可以尝试将视图从对象库拖动到黑色状态栏和第一个静态表格视图单元格之间的小空间。 (首先双击您的 tableview 控制器进行缩放)

标签: iphone ios uitableview


【解决方案1】:

只需使用带有父 UIViewController 的嵌入 segue,由标题视图和容器视图组成。将您的 UITableViewController 嵌入到容器视图中。更具体的步骤在this answer

如果您想要 UITableViewController 中的所有内容,您可以插入自己的子视图,执行以下操作:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.header = [[UIView alloc] init];
    self.header.frame = CGRectMake(0, 0, self.tableView.bounds.size.width, 44);
    self.header.backgroundColor = [UIColor greenColor];
    [self.tableView addSubview:self.header];
    self.tableView.contentInset = UIEdgeInsetsMake(44, 0, 0, 0);
}

然后在scrollViewDidScroll和好友中操纵view的位置:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.header.transform = CGAffineTransformMakeTranslation(0, self.tableView.contentOffset.y);
}

我说“和朋友”是因为您需要处理像 scrollViewDidScrollToTop: 这样的极端情况。 scrollViewDidScroll 在滚动期间的每个显示周期中都会被调用,所以这样做看起来完美无缺。

【讨论】:

  • 是的,这是实现相同结果的好方法之一,我仅通过 UITableViewController 尝试。等待答案,通过它我们可以禁用 tableHeaderView 的滚动。如果我没有找到,那么将接受你的回答。
  • 这种方式可行,但是如果表格视图有一些节标题,当您滚动时,您可以看到节标题将覆盖您的表格标题
  • @ZuzooVn 更改布局,使标题和表格视图不重叠。请记住,它们可能不会在情节提要中重叠,但如果您在具有不同屏幕尺寸的设备上运行,则布局可能会发生变化。你必须设置你的支柱和弹簧或约束来处理这个问题。
  • iO8 - 一些小改动,以防止 tableview 部分标题浮动在 headerView 上
  • @Isuru 为您的标题视图提供非零宽度
【解决方案2】:

蒂莫西·穆斯 (Timothy Moose) 很准。这是 iOS8 的必要更改。

MonoTouch (C#)

// create the fixed header view 
headerView = new UIView() {
                    Frame = new RectangleF(0,0,this.View.Frame.Width,44),
                    AutoresizingMask = UIViewAutoresizing.FlexibleWidth,
                    BackgroundColor = UIColor.DarkGray 
                };
// make it the top most layer
headerView.Layer.ZPosition = 1.0f;

// add directly to tableview, do not use TableViewHeader
TableView.AddSubview(headerView);

// TableView will start at the bottom of the nav bar
this.EdgesForExtendedLayout = UIRectEdge.None;

// move the content down the size of the header view
TableView.ContentInset = new UIEdgeInsets(headerView.Bounds.Height,0,0,0);

.....

[Export("scrollViewDidScroll:")]
public virtual void Scrolled(UIScrollView scrollView)
{
            // Keeps header fixed, this is called in the displayLink layer so it wont skip.
           if(headerView!=null) headerView.Transform = CGAffineTransform.MakeTranslation(0, TableView.ContentOffset.Y);


}

[Export ("scrollViewDidScrollToTop:")]
public virtual void ScrolledToTop (UIScrollView scrollView)
{
            // Keeps header fixed, this is called in the displayLink layer so it wont skip.
            if(headerView!=null) headerView.Transform = CGAffineTransform.MakeTranslation(0, TableView.ContentOffset.Y);


}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-24
    • 1970-01-01
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    • 2013-02-09
    • 2016-07-17
    • 2018-06-14
    相关资源
    最近更新 更多