Skip to content
This repository has been archived by the owner on Aug 14, 2019. It is now read-only.

Commit

Permalink
Changed scrollToRow: to scrollToIndexPath:
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianludwig committed Sep 13, 2015
1 parent 562efd0 commit 3f7d9f7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
Expand Up @@ -271,9 +271,9 @@
/**
* Scrolls the collection view such that the cell in the given row is completely visible, above the `inputToolbar`.
*
* @param row The index of the row that will be visible
* @param indexPath The indexPath that will be visible
* @param animated Pass `YES` if you want to animate scrolling, `NO` if it should be immediate.
*/
- (void)scrollToRow:(NSInteger)row animated:(BOOL)animated;
- (void)scrollToIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;

@end
Expand Up @@ -370,16 +370,17 @@ - (void)scrollToBottomAnimated:(BOOL)animated
return;
}

[self scrollToRow:([self.collectionView numberOfItemsInSection:0] - 1) animated:animated];
NSIndexPath *lastCell = [NSIndexPath indexPathForItem:([self.collectionView numberOfItemsInSection:0] - 1) inSection:0];
[self scrollToIndexPath:lastCell animated:animated];
}

- (void)scrollToRow:(NSInteger)row animated:(BOOL)animated
- (void)scrollToIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated
{
if ([self.collectionView numberOfSections] == 0) {
if ([self.collectionView numberOfSections] <= indexPath.section) {
return;
}

NSInteger numberOfItems = [self.collectionView numberOfItemsInSection:0];
NSInteger numberOfItems = [self.collectionView numberOfItemsInSection:indexPath.section];

if (numberOfItems == 0) {
return;
Expand All @@ -398,15 +399,14 @@ - (void)scrollToRow:(NSInteger)row animated:(BOOL)animated
}


row = MAX(MIN(row, numberOfItems - 1), 0);
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:row inSection:0];
CGSize cellSize = [self.collectionView.collectionViewLayout sizeForItemAtIndexPath:indexPath];

CGFloat maxHeightForVisibleMessage = CGRectGetHeight(self.collectionView.bounds) - self.collectionView.contentInset.top - CGRectGetHeight(self.inputToolbar.bounds);
NSInteger item = MAX(MIN(indexPath.item, numberOfItems - 1), 0);
indexPath = [NSIndexPath indexPathForItem:item inSection:0];

// workaround for really long messages not scrolling
// if last message is too long, use scroll position bottom for better appearance, else use top
// possibly a UIKit bug, see #480 on GitHub
CGSize cellSize = [self.collectionView.collectionViewLayout sizeForItemAtIndexPath:indexPath];
CGFloat maxHeightForVisibleMessage = CGRectGetHeight(self.collectionView.bounds) - self.collectionView.contentInset.top - CGRectGetHeight(self.inputToolbar.bounds);
UICollectionViewScrollPosition scrollPosition = (cellSize.height > maxHeightForVisibleMessage) ? UICollectionViewScrollPositionBottom : UICollectionViewScrollPositionTop;

[self.collectionView scrollToItemAtIndexPath:indexPath
Expand Down

0 comments on commit 3f7d9f7

Please sign in to comment.