【问题标题】:Iphone : How to scroll to the 1st cell of the 2nd section, letting the header of the 1st section visibleIphone:如何滚动到第二部分的第一个单元格,让第一部分的标题可见
【发布时间】:2011-06-03 15:03:07
【问题描述】:

我有一个带有行和部分的 UITableView。 我想滚动到第二部分的第一项,让第一部分的标题可见。就像我手动滚动列表直到达到那个状态一样。

---- TOP OF SCREEN ----
Header of first section
Header of the second section
cell 1
cell 2
cell 3
Header of the third section
cell 1
cell 2
...

scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1] 不做这项工作,它隐藏了第一部分的标题。

【问题讨论】:

    标签: iphone uitableview uiscrollview scroll sectionheader


    【解决方案1】:

    我们继续前进。我根据凯文的想法找到了这个方法。为了能够将动画设置为 YES,我使用 UIScrollView 的委托方法来捕捉动画的结束。有用。但是任何有助于不做 2 个动画的解决方案将不胜感激。 知道如何做到这一点吗?

    - (IBAction) scrollToToday:(BOOL)animate {
        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1] atScrollPosition:UITableViewScrollPositionTop animated:animate];
        if (animate == NO) [self showFirstHeaderLine:NO];
    }
    
    - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
        [self showFirstHeaderLine:YES];
    }
    
    - (void) showFirstHeaderLine:(BOOL)animate {
        CGRect headerRect = [self.tableView rectForHeaderInSection:1];
        CGPoint scrollPoint = headerRect.origin;
        scrollPoint.y -= headerRect.size.height;
        [self.tableView setContentOffset:scrollPoint animated:animate];
    }
    

    对于这段代码,当动画设置为 YES 时,进程应该在 scrollViewDidEndScrollingAnimation 和 showFirstHeaderLine 之间无限循环......它循环,是的,但只有一次...... 知道为什么吗?

    【讨论】:

    • 这种事情最好作为对原始问题的编辑。
    • 但是......如果我用解决方案编辑问题,这将不再是一个问题:-)
    【解决方案2】:

    您可以获取所需行的矩形,然后减去上一节标题的高度并滚动到该点。类似以下(未经测试)的东西应该可以工作:

    CGRect rowRect = [table rectForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
    CGRect headerRect = [table rectForHeaderInSection:0];
    rowRect.origin.y -= headerRect.size.height;
    rowRect.size.height += headerRect.size.height;
    [table scrollRectToVisible:rowRect animated:YES]; // UITableView is a subclass of UIScrollView
    

    【讨论】:

    • 这是个好主意,但这行不通。实际上它什么也没做,可能是因为该行已经可见。但是保持这个想法,是否可以让事情发生,然后向下滚动以显示隐藏的标题?
    • 啊,是的,如果它已经可见,这将无济于事。您可以做的只是计算原点 Y 坐标 (rowRect.origin.y - headerRect.size.height) 并使用它来设置 contentOffset 属性(您需要使用 -setContentOffset:animated: 使其具有动画效果)。
    【解决方案3】:

    我试过你的代码,它可以工作!!

    对于循环问题,由于您设置了一个偏移量(SetContentOffset),它与滚动无关。它不会调用 scrollView 委托。所以 scrollViewDidEndScrollingAnimation 只会被调用一次,它是从 scrollToRowAtIndexPath 调用的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 2021-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-02
      相关资源
      最近更新 更多