防止 UITableView 滚动到某个点以下 [英] Prevent UITableView scrolling below a certain point

查看:73
本文介绍了防止 UITableView 滚动到某个点以下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让 UITableView 允许在某个索引行上方滚动,但在低于某个点时阻止它?例如,如果我有第 1 到 100 行,其中在给定时间只有 5 行出现在视图中,我希望允许用户在第 1-50 行之间滚动,但在第 50 行可见时阻止任何进一步向下滚动.

How can I have a UITableView that permits scrolling above a certain index row, but prevents it when it below a certain point? For example, if I have rows 1 through 100, where only 5 appear in the view at a given time, I want to allow a user to scroll among rows 1-50, but prevent any further scrolling down when row 50 is visible.

推荐答案

你可以使用 UITableViewcontentInset 属性来解决这个问题.请记住将它们设置为负值,因为我们正在缩小内容大小:

You can use the property contentInset of UITableView just for that. Just remember to set them with minus values, cause we are shrinking the content size:

CGFloat insetTop = topAllowedRow * c_CELL_HEIGHT * -1;
CGFloat insetBottom = bottomAllowedRow * c_CELL_HEIGHT * -1;
[self.tableView setContentInset:UIEdgeInsetsMake(insetTop, 0, insetBottom, 0)];

另一种解决方案是实现UIScrollViewDelegate方法scrollViewDidScroll:,当scrollView.contentOffset太大时,将其设置回最大值该用户可以滚动到:

The other solution is to implement UIScrollViewDelegate method scrollViewDidScroll: and when scrollView.contentOffset is too huge, set it back to the max value that user can scroll to:

- (void)scrollViewDidScroll:(UIScrollView*)scrollView {
    CGPoint scrollViewOffset = scrollView.contentOffset;
    if (scrollViewOffset.x > MAX_VALUE) {
        scrollViewOffset.x = MAX_VALUE;
    }
    [scrollView setContentOffset:scrollViewOffset];
}

第一个解决方案既有优点也有缺点,因为 UIScrollView 将管理弹跳(就像拉动刷新一样).它更自然和 HIG 兼容,但如果您确实需要用户不要看到特定行下方,请使用委托方法.

First solution has both the advantage and disadvantage since then UIScrollView will manage the bouncing (just like in pull to refresh). It's more natural and HIG-compatible, but if you really need user not to see below the certain row, use delegate method.

这篇关于防止 UITableView 滚动到某个点以下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆