Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: soffes/sstoolkit
...
head fork: baz/sstoolkit
compare: master
  • 15 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
15 SSToolkit/SSCollectionView.h
View
@@ -548,6 +548,21 @@ typedef enum {
@optional
/**
+ Asks the delegate for the amount of inter-item spacing in a row.
+
+ @param aCollectionView The collection view object requesting this information.
+
+ @param section The section the item is located in.
+
+ @param proposedSpacing The spacing that the collection view has calculated is the ideal amount.
+
+ @return The alternate item row inter-item spacing.
+ */
+- (CGFloat)collectionView:(SSCollectionView *)aCollectionView itemRowSpacingForSection:(NSUInteger)section proposedSpacing:(CGFloat)proposedSpacing;
+
+@optional
+
+/**
Tells the delegate the collection view is about to draw an item for a particular index path.
A collection view sends this message to its `delegate` just before it uses item to draw a portion of a row, thereby
11 SSToolkit/SSCollectionView.m
View
@@ -452,6 +452,8 @@ - (void)insertSections:(NSIndexSet *)sections withItemAnimation:(SSCollectionVie
- (void)deleteSections:(NSIndexSet *)sections withItemAnimation:(SSCollectionViewItemAnimation)animation {
+ // Clear cache to avoid an out of bounds crash when inserting new sections in place of the deleted ones
+ [_sectionCache removeAllObjects];
[_tableView deleteSections:sections withRowAnimation:(UITableViewRowAnimation)animation];
}
@@ -636,7 +638,12 @@ - (NSUInteger)_numberOfRowsInSection:(NSUInteger)section {
- (CGFloat)_itemSpacingForSection:(NSUInteger)section {
CGSize itemSize = [self _itemSizeForSection:section];
NSUInteger itemsPerRow = [self _numberOfItemsPerRowForSection:section];
- return roundf((self.frame.size.width - (itemSize.width * (CGFloat)itemsPerRow)) / (itemsPerRow + 1));
+ CGFloat spacing = roundf((self.frame.size.width - (itemSize.width * (CGFloat)itemsPerRow)) / (itemsPerRow + 1));
+ if ([_delegate respondsToSelector:@selector(collectionView:itemRowSpacingForSection:proposedSpacing:)]) {
+ spacing = [_delegate collectionView:self itemRowSpacingForSection:section proposedSpacing:spacing];
+ }
+
+ return spacing;
}
@@ -655,7 +662,7 @@ - (NSArray *)_itemsForRowIndexPath:(NSIndexPath *)rowIndexPath {
NSUInteger startIndex = itemsPerRow * row;
NSUInteger endIndex = (NSUInteger)fmin(totalItems, startIndex + itemsPerRow);
- NSMutableArray *items = [[[NSMutableArray alloc] initWithCapacity:endIndex - startIndex] autorelease];
+ NSMutableArray *items = [[[NSMutableArray alloc] initWithCapacity:abs(endIndex - startIndex)] autorelease];
for (NSUInteger i = startIndex; i < endIndex; i++) {
NSIndexPath *itemIndexPath = [NSIndexPath indexPathForRow:i inSection:rowIndexPath.section];
11 SSToolkit/SSCollectionViewItemTableViewCell.m
View
@@ -52,13 +52,20 @@ - (void)dealloc {
- (void)layoutSubviews {
__block CGFloat x = _itemSpacing;
-
+ // Ensure whole row is centered
+ NSUInteger numItems = (NSUInteger) self.bounds.size.width / _itemSize.width;
+ CGFloat totalWidth = _itemSize.width * numItems + _itemSpacing * (numItems + 1);
+ CGFloat xOrigin = (self.bounds.size.width - totalWidth) / 2;
+
[_items enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop) {
SSCollectionViewItem *item = (SSCollectionViewItem *)object;
- item.frame = CGRectMake(x, 0.0f, _itemSize.width, _itemSize.height);
+ item.frame = CGRectIntegral(CGRectMake(xOrigin + x, 0.0f, _itemSize.width, _itemSize.height));
+
x += _itemSize.width + _itemSpacing;
}];
+
+ self.frame = CGRectIntegral(self.frame);
}

No commit comments for this range

Something went wrong with that request. Please try again.