Prevent UITableView scrolling below a certain point - ios

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.

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)];
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];
}
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 is a subclass of UIScrollView. This means you can use all methods from UIScrollViewDelegate. Try adding the scrollView.contentOffset logic in the following delegate method:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;

Related

How to know if UICollectionView will actually scroll after scrollToItemAtIndexPath: is called?

I use scrollToItemAtIndexPath:atScrollPosition:animated to scroll UICollectionView. I have to disable some functionality while this automatic scrolling is happening and then re-enable it, when scrolling ends. The problem occurs, when I try to scroll to some cell which is already visible and there is no scroll necessary (for example, it's the last cell and collection view is already scrolled until the very end). In this case scrollViewDidEndDragging nor scrollViewDidEndDecelerating are called.
Is there a way to know if scrollToItemAtIndexPath will actually scroll UICollectionView?
I Think you can save the latest scroll view offset & track changes in it every time the scrollView scrolls & apply actions after significant changes only.
func scrollViewDidScroll(scrollView: UIScrollView) {
let scrollOffset = scrollView.contentOffset.y;
if scrollOffset < (previousValue-100) || scrollOffset > (previousValue+100)
{
// Scrolled up or down with 100 points which you can change
previousValue = scrollOffset
}
}
You can determine whether scrollToItemAtIndexPath will scroll based on the cell's frame before calling scrollToItemAtIndexPath. For example, if you just want the cell to be visible (UICollectionViewScrollPositionNone):
UICollectionViewLayoutAttributes* attrs = [collectionView layoutAttributesForItemAtIndexPath:indexPath];
CGRect frame = [collectionView convertRect:attrs.frame toView:self.view];
BOOL willScroll = !CGRectContainsRect(collectionView.frame, frame);
It's unfortunate that scrollToItemAtIndexPath doesn't simply tell you whether it will scroll.

ScrollviewDidScroll not called at each point

There is a button at the bottom of my view controller. When the user scrolls down the button has to be attached to the scrollview at certain height.
I need to attach a button to the scrollview, immediately when the contentOffset.y reaches a particular value. -(void) scrollviewDidScroll doesn't help me as there might be a jump in contentOffset when the user is scrolling fast. Any leads on this are helpful.
Also, whenever I add a subview to the scrollview, -(void) viewDidLayoutSubviews is called. Which in turn sets the contentOffset to {0,0}. How can I achieve the functionality I need?
I needed to do the same thing with a UITableView and for me using scrollViewDidScroll worked.
I created a view called staticBar and added it as a subview of the tableView, but I had to rearrange the tableview subviews for it to appear in the right place. I don't have my code in front of me, but in -scrollViewDidScroll: it looked something like this:
- (void)scrollViewDidScroll:(UIScrollView*)scrollView
{
CGFloat staticBarAdjustedY = _staticBarY - scrollView.contentOffset.y;
CGFloat scrollViewYFloor = scrollView.frame.size.height - _staticBar.frame.size.height;
// This way maximum Y the view can have is at the base of the scrollView
CGFloat newY = MIN( staticBarAdjustedY, scrollViewYFloor);
_staticBar.frame = (CGRect){ { _staticBar.frame.origin.x, newY}, _staticBar.frame.size}
}
I will check my code later today and add more details here.
Also, you said the scrollviewDidScroll has jumps in contentOffset, but it's worth mentioning that these jumps are the same that the scrollView uses to scroll its own view. So it's not like you are "losing" frames on this delegate method.
Hope it helps.
PS: So, here is the rest of my code.
//I place my custom view as a subview of the tableView below it's last subview
//The last subview is for scroll indicators.
WTButtonsBar *buttonBar = [[WTButtonsBar alloc] init];
[self.tableView insertSubview:buttonBar belowSubview:self.tableView.subviews.lastObject];
In scrollViewDidScroll:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//In my app I needed my view to stick to the top of the screen
//thats why I use MAX here
//self.buttonsBarOriginalY is the view's position in the scrollView when it isn't attached to the top.
CGFloat newY = MAX(scrollView.contentOffset.y, self.buttonsBarOriginalY)
[_buttonsBar setFrame:(CGRect){{0, newY}, _buttonsBar.frame.size}];
}

UITableView header view animation

I'd like to replicate the effect found in the Kickstarter app where, when the user scrolls up and the tableview is already at the beginning, the header view increases in size.
To see the effect, just open a random project and scroll up.
Do you know how to achieve such animation?
You can set delegate for you table view and use method scrollViewDidScroll:, UITableViewDelegate is sub-protocol of UIScrollViewDelegate, so you can just use:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView.contentOffset.y < 0) {
//you have to store image you want to scale somewhere (in ivar for example - _image)
//k for scaling
CGFloat k = fabs(scrollView.contentOffset.y)/10;
_image.transform = CGAffineTransformMakeScale(k, k);
}
}

Can I draw UITableViewCells that are outside the UITableView's frame?

I have a horizontal UITableView where I would like to set the paging distance. I tried this approach,
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
_tableView.decelerationRate = UIScrollViewDecelerationRateFast;
CGFloat pageSize = 320.f / 3.f;
CGPoint contentOffset = _tableView.contentOffset;
contentOffset.y = roundf(contentOffset.y / pageSize) * pageSize;
[_tableView setContentOffset:contentOffset animated:YES];
}
This works when you scroll slowly and let go but if you scroll fast, there's a lot of popping. So after wrestling with it for a bit, I'm trying a different approach...
I'm simply enabling paging on the tableView and then setting the width of the table to my desired paging size (a 3rd of the screen's width). I also set clipsToBounds = NO. When I use this approach, the scrolling works as expected but now cells outside of my smaller table width do not draw. What I would like is to force the cell on the left and right of my current cell to draw, even though they are outside of the UITableView's frame. I know cellForRowAtIndexPath get's called from a deeper level but is there some way I can trigger it myself for the cell's I selected?
I've tried using,
[[_tableView cellForRowAtIndexPath:indexPath] setNeedsDisplay];
but it does nothinggggggg!

UITableView content height

I have a UITableView that is set to not enable scrolling, and it exists in a UIScrollView. I'm doing it this way as the design specs call for something that looks like a table view, (actually there are two of them side by side), and it would be much easier to implement tableviews rather than adding a whole bunch of buttons, (grouped table views).
Question is, I need to know how big to make the container view for the scrollview, so it scrolls the whole height of the table views. Once loaded, is there any way to find the height of a tableview? There is no contentView property like a scroll view, frame seems to be static, etc...
Any thoughts?
Use
CGRect lastRowRect= [tableView rectForRowAtIndexPath:index_path_for_your_last_row];
CGFloat contentHeight = lastRowRect.origin.y + lastRowRect.size.height;
You can then use the contentHeight variable to set the contentSize for the scrollView.
A more general solution that works for me:
CGFloat tableViewHeight(UITableView *tableView) {
NSInteger lastSection = tableView.numberOfSections - 1;
while (lastSection >= 0 && [tableView numberOfRowsInSection:lastSection] <= 0)
lastSection--;
if (lastSection < 0)
return 0;
CGRect lastFooterRect = [tableView rectForFooterInSection:lastSection];
return lastFooterRect.origin.y + lastFooterRect.size.height;
}
In addition to Andrei's solution, it accounts for empty sections and section footers.
UITableView is a subclass of UIScrollView, so it has a contentSize property that you should be able to use no problem:
CGFloat tableViewContentHeight = tableView.contentSize.height;
scrollView.contentSize = CGSizeMake(scrollView.contentSize.width, tableViewContentHeight);
However, as several other SO questions have pointed out, when you make an update to a table view (like inserting a row), its contentSize doesn't appear to be updated immediately like it is for most other animated resizing in UIKit. In this case, you may need to resort to something like Michael Manner's answer. (Although I think it makes better sense implemented as a category on UITableView)
You can run over the sections and use the rectForSection to calculate the total height (this included footer and header as well!). In swift I use the following extension on UITableView
extension UITableView {
/**
Calculates the total height of the tableView that is required if you ware to display all the sections, rows, footers, headers...
*/
func contentHeight() -> CGFloat {
var height = CGFloat(0)
for sectionIndex in 0..<numberOfSections {
height += rectForSection(sectionIndex).size.height
}
return height
}
}

Resources