Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixes keyboard moving in collection view with groups

  • Loading branch information...
commit ffe81d5d3a18ab4a1b4fcdcced43771eec2486ae 1 parent 5247718
Pieter pieteromvlee authored
129 BCCollectionView+Keyboard.m
View
@@ -3,6 +3,7 @@
#import "BCCollectionView+Keyboard.h"
#import "BCCollectionViewLayoutManager.h"
+#import "BCCollectionViewItemLayout.h"
@implementation BCCollectionView (BCCollectionView_Keyboard)
@@ -42,109 +43,99 @@ - (void)insertText:(id)aString
}
}
-- (void)moveLeft:(id)sender
+#pragma mark Helper Methods
+
+- (void)simpleSelectItemAtIndex:(NSUInteger)anIndex
{
- NSUInteger index = lastSelectionIndex;
-
- if (index % [layoutManager numberOfItemsAtRow:index] != 0) {
+ if (anIndex != NSNotFound) {
[self deselectAllItems];
- NSUInteger newIndex = index-1;
- [self selectItemAtIndex:newIndex];
- [self scrollRectToVisible:[layoutManager rectOfItemAtIndex:newIndex]];
+ [self selectItemAtIndex:anIndex];
+ [self scrollRectToVisible:[layoutManager rectOfItemAtIndex:anIndex]];
}
}
-- (void)moveLeftAndModifySelection:(id)sender
+- (void)simpleExtendSelectionRange:(NSRange)range newIndex:(NSUInteger)newIndex
{
- if (lastSelectionIndex % [layoutManager numberOfItemsAtRow:lastSelectionIndex] != 0) {
- NSUInteger index = lastSelectionIndex-1;
- if ([selectionIndexes containsIndex:index])
- [self deselectItemAtIndex:lastSelectionIndex];
+ if (newIndex != NSNotFound) {
+ if ([selectionIndexes containsIndex:newIndex])
+ [self deselectItemsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range]];
else
- [self selectItemAtIndex:index];
- lastSelectionIndex = index;
- [self scrollRectToVisible:[layoutManager rectOfItemAtIndex:index]];
+ [self selectItemsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range]];
+ lastSelectionIndex = newIndex;
+ [self scrollRectToVisible:[layoutManager rectOfItemAtIndex:newIndex]];
}
}
+#pragma mark Arrow Keys
+
+- (void)moveLeft:(id)sender
+{
+ NSPoint position = [layoutManager rowAndColumnPositionOfItemAtIndex:lastSelectionIndex];
+ [self simpleSelectItemAtIndex:[layoutManager indexOfItemAtRow:position.y column:position.x-1]];
+}
+
+- (void)moveLeftAndModifySelection:(id)sender
+{
+ NSPoint position = [layoutManager rowAndColumnPositionOfItemAtIndex:lastSelectionIndex];
+ NSUInteger newIndex = [layoutManager indexOfItemAtRow:position.y column:position.x-1];
+ [self simpleExtendSelectionRange:NSMakeRange(newIndex, lastSelectionIndex-newIndex) newIndex:newIndex];
+}
+
- (void)moveRight:(id)sender
{
- NSUInteger index = lastSelectionIndex;
- if (index % [layoutManager numberOfItemsAtRow:index] != [layoutManager numberOfItemsAtRow:index]-1) {
- [self deselectAllItems];
- NSUInteger newIndex = index+1;
- [self selectItemAtIndex:newIndex];
- [self scrollRectToVisible:[layoutManager rectOfItemAtIndex:newIndex]];
- }
+ NSPoint position = [layoutManager rowAndColumnPositionOfItemAtIndex:lastSelectionIndex];
+ [self simpleSelectItemAtIndex:[layoutManager indexOfItemAtRow:position.y column:position.x+1]];
}
- (void)moveRightAndModifySelection:(id)sender
{
- if (lastSelectionIndex % [layoutManager numberOfItemsAtRow:lastSelectionIndex] != [layoutManager numberOfItemsAtRow:lastSelectionIndex]-1) {
- NSUInteger index = lastSelectionIndex+1;
- if ([selectionIndexes containsIndex:index])
- [self deselectItemAtIndex:lastSelectionIndex];
- else
- [self selectItemAtIndex:index];
- lastSelectionIndex = index;
- [self scrollRectToVisible:[layoutManager rectOfItemAtIndex:index]];
- }
+ NSPoint position = [layoutManager rowAndColumnPositionOfItemAtIndex:lastSelectionIndex];
+ NSUInteger newIndex = [layoutManager indexOfItemAtRow:position.y column:position.x+1];
+ [self simpleExtendSelectionRange:NSMakeRange(lastSelectionIndex, newIndex-lastSelectionIndex+1) newIndex:newIndex];
}
- (void)moveUp:(id)sender
{
- NSUInteger index = lastSelectionIndex;
- if (index > [layoutManager numberOfItemsAtRow:index]-1) {
- [self deselectAllItems];
- NSUInteger newIndex = index-[layoutManager numberOfItemsAtRow:index];
- [self selectItemAtIndex:newIndex];
- [self scrollRectToVisible:[layoutManager rectOfItemAtIndex:newIndex]];
- }
+ NSPoint position = [layoutManager rowAndColumnPositionOfItemAtIndex:lastSelectionIndex];
+ [self simpleSelectItemAtIndex:[layoutManager indexOfItemAtRow:position.y-1 column:position.x]];
}
- (void)moveUpAndModifySelection:(id)sender
{
- if (lastSelectionIndex > [layoutManager numberOfItemsAtRow:lastSelectionIndex]-1) {
- NSUInteger index = lastSelectionIndex - [layoutManager numberOfItemsAtRow:lastSelectionIndex];
- NSRange range = NSMakeRange(index, [layoutManager numberOfItemsAtRow:lastSelectionIndex]);
- if ([selectionIndexes containsIndex:index]) {
- range.location++;
- [self deselectItemsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range]];
- } else
- [self selectItemsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range]];
- lastSelectionIndex = index;
- [self scrollRectToVisible:[layoutManager rectOfItemAtIndex:index]];
- }
+ NSPoint position = [layoutManager rowAndColumnPositionOfItemAtIndex:lastSelectionIndex];
+ NSUInteger newIndex = [layoutManager indexOfItemAtRow:position.y-1 column:position.x];
+ if (newIndex == NSNotFound)
+ newIndex = 0;
+
+ NSRange range = NSMakeRange(newIndex, lastSelectionIndex-newIndex);
+ if ([selectionIndexes containsIndex:newIndex])
+ range.location++;
+
+ [self simpleExtendSelectionRange:range newIndex:newIndex];
}
- (void)moveDown:(id)sender
{
- NSUInteger index = lastSelectionIndex;
- if (index + [layoutManager numberOfItemsAtRow:index] < [contentArray count]) {
- [self deselectAllItems];
- NSUInteger newIndex = index + [layoutManager numberOfItemsAtRow:index];
- [self selectItemAtIndex:newIndex];
- [self scrollRectToVisible:[layoutManager rectOfItemAtIndex:newIndex]];
- }
+ NSPoint position = [layoutManager rowAndColumnPositionOfItemAtIndex:lastSelectionIndex];
+ [self simpleSelectItemAtIndex:[layoutManager indexOfItemAtRow:position.y+1 column:position.x]];
}
- (void)moveDownAndModifySelection:(id)sender
{
- NSUInteger index = lastSelectionIndex;
- if (index + [layoutManager numberOfItemsAtRow:index] < [contentArray count]) {
- NSUInteger index = lastSelectionIndex + [layoutManager numberOfItemsAtRow:index];
- NSRange range = NSMakeRange(lastSelectionIndex, [layoutManager numberOfItemsAtRow:index]);
- if ([selectionIndexes containsIndex:index])
- [self deselectItemsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range]];
- else {
- range.length++;
- [self selectItemsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range]];
- }
- lastSelectionIndex = index;
- [self scrollRectToVisible:[layoutManager rectOfItemAtIndex:index]];
- }
+ NSPoint position = [layoutManager rowAndColumnPositionOfItemAtIndex:lastSelectionIndex];
+ NSUInteger newIndex = [layoutManager indexOfItemAtRow:position.y+1 column:position.x];
+ if (newIndex == NSNotFound)
+ newIndex = [contentArray count]-1;
+
+ NSRange range = NSMakeRange(lastSelectionIndex, newIndex-lastSelectionIndex);
+ if (![selectionIndexes containsIndex:newIndex])
+ range.length++;
+
+ [self simpleExtendSelectionRange:range newIndex:newIndex];
}
+#pragma mark Deleting
+
- (void)deleteBackward:(id)sender
{
if ([delegate respondsToSelector:@selector(collectionView:deleteItemsAtIndexes:)])
6 BCCollectionView.h
View
@@ -11,10 +11,12 @@
BCCollectionViewLayoutManager *layoutManager;
NSArray *contentArray;
+ NSArray *groups;
NSMutableArray *reusableViewControllers;
NSMutableDictionary *visibleViewControllers;
NSMutableIndexSet *selectionIndexes;
+ NSMutableDictionary *visibleGroupViewControllers;
NSColor *backgroundColor;
NSUInteger numberOfPreRenderedRows;
@@ -42,7 +44,7 @@
//private
@property (nonatomic, copy) NSIndexSet *originalSelectionIndexes;
-@property (nonatomic, copy) NSArray *contentArray;
+@property (nonatomic, copy) NSArray *contentArray, *groups;
@property (nonatomic, copy) NSString *zoomValueObserverKey, *accumulatedKeyStrokes;
@property (readonly) NSArray *visibleViewControllerArray;
@@ -50,6 +52,7 @@
//designated way to load BCCollectionView
- (void)reloadDataWithItems:(NSArray *)newContent emptyCaches:(BOOL)shouldEmptyCaches;
+- (void)reloadDataWithItems:(NSArray *)newContent groups:(NSArray *)newGroups emptyCaches:(BOOL)shouldEmptyCaches;
//Managing Selections
- (void)selectItemAtIndex:(NSUInteger)index;
@@ -63,6 +66,7 @@
//Basic Cell Information
- (NSSize)cellSize;
+- (NSUInteger)groupHeaderHeight;
- (NSRange)rangeOfVisibleItems;
- (NSRange)rangeOfVisibleItemsWithOverflow;
90 BCCollectionView.m
View
@@ -4,24 +4,26 @@
#import "BCCollectionView.h"
#import "BCGeometryExtensions.h"
#import "BCCollectionViewLayoutManager.h"
+#import "BCCollectionViewItemLayout.h"
#import "BCCollectionViewGroup.h"
@implementation BCCollectionView
-@synthesize delegate, contentArray, backgroundColor, originalSelectionIndexes, zoomValueObserverKey, accumulatedKeyStrokes, numberOfPreRenderedRows, layoutManager;
+@synthesize delegate, contentArray, groups, backgroundColor, originalSelectionIndexes, zoomValueObserverKey, accumulatedKeyStrokes, numberOfPreRenderedRows, layoutManager;
@dynamic visibleViewControllerArray;
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
- reusableViewControllers = [[NSMutableArray alloc] init];
- visibleViewControllers = [[NSMutableDictionary alloc] init];
- contentArray = [[NSArray alloc] init];
- selectionIndexes = [[NSMutableIndexSet alloc] init];
- dragHoverIndex = NSNotFound;
- accumulatedKeyStrokes = [[NSString alloc] init];
- numberOfPreRenderedRows = 3;
- layoutManager = [[BCCollectionViewLayoutManager alloc] initWithCollectionView:self];
+ reusableViewControllers = [[NSMutableArray alloc] init];
+ visibleViewControllers = [[NSMutableDictionary alloc] init];
+ contentArray = [[NSArray alloc] init];
+ selectionIndexes = [[NSMutableIndexSet alloc] init];
+ dragHoverIndex = NSNotFound;
+ accumulatedKeyStrokes = [[NSString alloc] init];
+ numberOfPreRenderedRows = 3;
+ layoutManager = [[BCCollectionViewLayoutManager alloc] initWithCollectionView:self];
+ visibleGroupViewControllers = [[NSMutableDictionary alloc] init];
[self addObserver:self forKeyPath:@"backgroundColor" options:0 context:NULL];
@@ -57,7 +59,9 @@ - (void)dealloc
[layoutManager release];
[reusableViewControllers release];
[visibleViewControllers release];
+ [visibleGroupViewControllers release];
[contentArray release];
+ [groups release];
[selectionIndexes release];
[originalSelectionIndexes release];
[accumulatedKeyStrokes release];
@@ -113,14 +117,6 @@ - (void)drawRect:(NSRect)dirtyRect
if (dragHoverIndex != NSNotFound && [self shoulDrawHover])
[self drawItemSelectionForInRect:[[[visibleViewControllers objectForKey:[NSNumber numberWithInteger:dragHoverIndex]] view] frame]];
-
- if ([delegate respondsToSelector:@selector(collectionView:drawGroupSeparator:inRect:)]) {
- NSRect groupRect = NSMakeRect(0, 0, NSWidth([self visibleRect]), BCCollectionViewGroupHeight);
- for (BCCollectionViewGroup *group in [layoutManager groups]) {
- groupRect.origin.y = [layoutManager rectOfItemAtIndex:[group itemRange].location].origin.y-BCCollectionViewGroupHeight;
- [delegate collectionView:self drawGroupSeparator:group inRect:groupRect];
- }
- }
}
#pragma mark Delegate Call Wrappers
@@ -176,6 +172,11 @@ - (NSSize)cellSize
return [delegate cellSizeForCollectionView:self];
}
+- (NSUInteger)groupHeaderHeight
+{
+ return [delegate groupHeaderHeightForCollectionView:self];
+}
+
- (NSIndexSet *)indexesOfItemsInRect:(NSRect)aRect
{
NSUInteger firstIndex = [layoutManager indexOfItemAtPoint:NSMakePoint(NSMinX(aRect), NSMinY(aRect))];
@@ -216,32 +217,19 @@ - (NSIndexSet *)indexesOfItemContentRectsInRect:(NSRect)aRect
- (NSRange)rangeOfVisibleItems
{
+ NSArray *itemLayouts = [[layoutManager itemLayouts] copy];
NSRect visibleRect = [self visibleRect];
- NSInteger numberOfRows = [layoutManager numberOfRows];
- NSInteger indexOfFirstItemAtRow = 0;
- NSInteger firstIndex = NSNotFound;
- NSInteger lastIndex = NSNotFound;
-
- for (NSInteger i=0; i<numberOfRows; i++) {
- NSRect r = [layoutManager rectOfItemAtIndex:indexOfFirstItemAtRow];
- BOOL rowIsVisible = NSIntersectionRange(NSMakeRange(r.origin.y, r.size.height), NSMakeRange(visibleRect.origin.y, visibleRect.size.height)).length != 0;
-
- if (firstIndex == NSNotFound && rowIsVisible)
- firstIndex = indexOfFirstItemAtRow;
- if (firstIndex != NSNotFound && !rowIsVisible)
- lastIndex = indexOfFirstItemAtRow-1;
- if (firstIndex != NSNotFound && lastIndex != NSNotFound)
- return NSMakeRange(firstIndex, lastIndex-firstIndex);
-
- indexOfFirstItemAtRow += [layoutManager numberOfItemsAtRow:i];
- }
- return NSMakeRange(firstIndex, lastIndex-firstIndex);
+ NSIndexSet *visibleIndexes = [itemLayouts indexesOfObjectsWithOptions:NSEnumerationConcurrent passingTest:^BOOL(id itemLayout, NSUInteger idx, BOOL *stop) {
+ return NSIntersectsRect([itemLayout itemRect], visibleRect);
+ }];
+ [itemLayouts release];
+ return NSMakeRange([visibleIndexes firstIndex], [visibleIndexes lastIndex]-[visibleIndexes firstIndex]);
}
- (NSRange)rangeOfVisibleItemsWithOverflow
{
NSRange range = [self rangeOfVisibleItems];
- NSInteger extraItems = [layoutManager numberOfItemsAtRow:0] * numberOfPreRenderedRows;
+ NSInteger extraItems = [layoutManager maximumNumberOfItemsPerRow] * numberOfPreRenderedRows;
NSInteger min = range.location;
NSInteger max = range.location + range.length;
@@ -325,6 +313,24 @@ - (void)addMissingViewControllersToView
[self delegateUpdateSelectionForItemAtIndex:idx];
}
}];
+
+ if ([groups count] > 0) {
+ [groups enumerateObjectsUsingBlock:^(id group, NSUInteger idx, BOOL *stop) {
+ NSRect groupRect = NSMakeRect(0, NSMinY([layoutManager rectOfItemAtIndex:[group itemRange].location])-[self groupHeaderHeight],
+ NSWidth([self visibleRect]), [self groupHeaderHeight]);
+ BOOL groupShouldBeVisible = NSIntersectsRect(groupRect, [self visibleRect]);
+ NSViewController *groupViewController = [visibleGroupViewControllers objectForKey:[NSNumber numberWithInteger:idx]];
+ if (groupShouldBeVisible && !groupViewController) {
+ groupViewController = [delegate collectionView:self headerViewControllerForGroup:group];
+ [[groupViewController view] setFrame:groupRect];
+ [self addSubview:[groupViewController view]];
+ [visibleGroupViewControllers setObject:groupViewController forKey:[NSNumber numberWithInteger:idx]];
+ } else if (!groupShouldBeVisible && groupViewController) {
+ [[groupViewController view] removeFromSuperview];
+ [visibleGroupViewControllers removeObjectForKey:[NSNumber numberWithInteger:idx]];
+ }
+ }];
+ }
});
}
@@ -458,11 +464,17 @@ - (void)resizeFrameToFitContents
- (void)reloadDataWithItems:(NSArray *)newContent emptyCaches:(BOOL)shouldEmptyCaches
{
+ [self reloadDataWithItems:newContent groups:nil emptyCaches:shouldEmptyCaches];
+}
+
+- (void)reloadDataWithItems:(NSArray *)newContent groups:(NSArray *)newGroups emptyCaches:(BOOL)shouldEmptyCaches
+{
[self deselectAllItems];
if (!delegate)
return;
+ self.groups = newGroups;
self.contentArray = newContent;
[layoutManager reloadWithCompletionBlock:^{
[self resizeFrameToFitContents];
@@ -474,8 +486,12 @@ - (void)reloadDataWithItems:(NSArray *)newContent emptyCaches:(BOOL)shouldEmptyC
[delegate collectionView:self viewControllerBecameInvisible:viewController];
}
+ for (NSViewController *viewController in [visibleGroupViewControllers allValues])
+ [[viewController view] removeFromSuperview];
+
[reusableViewControllers removeAllObjects];
[visibleViewControllers removeAllObjects];
+ [visibleGroupViewControllers removeAllObjects];
} else
[self softReloadVisibleViewControllers];
4 BCCollectionViewDelegate.h
View
@@ -44,7 +44,9 @@ enum {
- (BOOL)collectionViewShouldDrawSelections:(BCCollectionView *)collectionView;
- (BOOL)collectionViewShouldDrawHover:(BCCollectionView *)collectionView;
-- (void)collectionView:(BCCollectionView *)collectionView drawGroupSeparator:(BCCollectionViewGroup *)group inRect:(NSRect)rect;
+//working with groups
+- (NSUInteger)groupHeaderHeightForCollectionView:(BCCollectionView *)collectionView;
+- (id)collectionView:(BCCollectionView *)collectionView headerViewControllerForGroup:(BCCollectionViewGroup *)group;
//managing Drag & Drop (in order of occurence)
- (BOOL)collectionView:(BCCollectionView *)collectionView canDragItemsAtIndexes:(NSIndexSet *)indexSet;
2  BCCollectionViewGroup.h
View
@@ -3,8 +3,6 @@
#import <Foundation/Foundation.h>
-#define BCCollectionViewGroupHeight 30
-
@interface BCCollectionViewGroup : NSObject
{
NSString *title;
5 BCCollectionViewItemLayout.m
View
@@ -11,4 +11,9 @@ + (id)layoutItem
return [[[self alloc] init] autorelease];
}
+- (NSString *)description
+{
+ return [NSString stringWithFormat:@"i:%i r:%i c:%i", (int)itemIndex, (int)rowIndex, (int)columnIndex];
+}
+
@end
15 BCCollectionViewLayoutManager.h
View
@@ -7,21 +7,24 @@
@interface BCCollectionViewLayoutManager : NSObject
{
BCCollectionView *collectionView;
- NSInteger numberOfRows;
- NSArray *groups;
- NSMutableArray *itemLayouts;
-
NSOperationQueue *queue;
+
+ NSMutableArray *itemLayouts;
+ NSInteger numberOfRows;
}
-@property (copy) NSArray *groups;
+@property (readonly) NSArray *itemLayouts;
- (id)initWithCollectionView:(BCCollectionView *)collectionView; //assigned
- (void)reloadWithCompletionBlock:(dispatch_block_t)completionBlock;
#pragma mark Primitives
- (NSUInteger)numberOfRows;
-- (NSUInteger)numberOfItemsAtRow:(NSInteger)rowIndex;
+- (NSUInteger)maximumNumberOfItemsPerRow;
- (NSSize)cellSize;
+#pragma mark Rows and Columns
+- (NSUInteger)indexOfItemAtRow:(NSUInteger)rowIndex column:(NSUInteger)colIndex;
+- (NSPoint)rowAndColumnPositionOfItemAtIndex:(NSUInteger)anIndex;
+
#pragma mark From Point to Index
- (NSUInteger)indexOfItemAtPoint:(NSPoint)p;
- (NSUInteger)indexOfItemContentRectAtPoint:(NSPoint)p;
48 BCCollectionViewLayoutManager.m
View
@@ -2,12 +2,12 @@
// Copyright 2011 Bohemian Coding. All rights reserved.
#import "BCCollectionViewLayoutManager.h"
-#import "BCCollectionViewItemLayout.h"
-#import "BCCollectionViewGroup.h"
#import "BCCollectionView.h"
+#import "BCCollectionViewGroup.h"
+#import "BCCollectionViewItemLayout.h"
@implementation BCCollectionViewLayoutManager
-@synthesize groups;
+@synthesize itemLayouts;
- (id)initWithCollectionView:(BCCollectionView *)aCollectionView
{
@@ -25,14 +25,15 @@ - (id)initWithCollectionView:(BCCollectionView *)aCollectionView
- (void)reloadWithCompletionBlock:(dispatch_block_t)completionBlock
{
- numberOfRows = -1;
+ numberOfRows = 0;
[queue cancelAllOperations];
[queue addOperationWithBlock:^{
[itemLayouts removeAllObjects];
NSInteger x = 0;
NSInteger y = 0;
+ NSUInteger colIndex = 0;
- NSEnumerator *groupEnum = [groups objectEnumerator];
+ NSEnumerator *groupEnum = [[collectionView groups] objectEnumerator];
BCCollectionViewGroup *group = [groupEnum nextObject];
NSSize cellSize = [self cellSize];
NSUInteger count = [[collectionView contentArray] count];
@@ -40,30 +41,37 @@ - (void)reloadWithCompletionBlock:(dispatch_block_t)completionBlock
if (group && [group itemRange].location == i) {
if (x != 0) {
numberOfRows++;
+ colIndex = 0;
y += cellSize.height;
}
- y += BCCollectionViewGroupHeight;
+ y += [collectionView groupHeaderHeight];
x = 0;
}
BCCollectionViewItemLayout *item = [BCCollectionViewItemLayout layoutItem];
[item setItemIndex:i];
- [item setRowIndex:numberOfRows];
if (![group isCollapsed]) {
if (x + cellSize.width > NSMaxX([collectionView visibleRect])) {
numberOfRows++;
+ colIndex = 0;
y += cellSize.height;
x = 0;
}
+ [item setColumnIndex:colIndex];
[item setItemRect:NSMakeRect(x, y, cellSize.width, cellSize.height)];
x += cellSize.width;
+ colIndex++;
} else {
[item setItemRect:NSMakeRect(x, y, 0, 0)];
}
+ [item setRowIndex:numberOfRows];
[itemLayouts addObject:item];
- if ([group itemRange].location + [group itemRange].length == i)
+ if ([group itemRange].location + [group itemRange].length-1 == i)
group = [groupEnum nextObject];
}
+ numberOfRows = MAX(numberOfRows, [[collectionView groups] count]);
+ if ([[collectionView contentArray] count] > 0 && numberOfRows == -1)
+ numberOfRows = 1;
dispatch_async(dispatch_get_main_queue(), completionBlock);
}];
}
@@ -71,7 +79,6 @@ - (void)reloadWithCompletionBlock:(dispatch_block_t)completionBlock
- (void)dealloc
{
[itemLayouts release];
- [groups release];
[queue release];
[super dealloc];
}
@@ -84,7 +91,7 @@ - (NSUInteger)numberOfRows
return numberOfRows;
}
-- (NSUInteger)numberOfItemsAtRow:(NSInteger)rowIndex
+- (NSUInteger)maximumNumberOfItemsPerRow
{
return MAX(1, [collectionView frame].size.width/[self cellSize].width);
}
@@ -95,6 +102,27 @@ - (NSSize)cellSize
}
#pragma mark -
+#pragma mark Rows and Columns
+
+- (NSPoint)rowAndColumnPositionOfItemAtIndex:(NSUInteger)anIndex
+{
+ BCCollectionViewItemLayout *itemLayout = [itemLayouts objectAtIndex:anIndex];
+ return NSMakePoint(itemLayout.columnIndex, itemLayout.rowIndex);
+}
+
+- (NSUInteger)indexOfItemAtRow:(NSUInteger)rowIndex column:(NSUInteger)colIndex
+{
+ __block NSUInteger index = NSNotFound;
+ [itemLayouts enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id item, NSUInteger idx, BOOL *stop) {
+ if ([item rowIndex] == rowIndex && [item columnIndex] == colIndex) {
+ index = [item itemIndex];
+ *stop = YES;
+ }
+ }];
+ return index;
+}
+
+#pragma mark -
#pragma mark From Point to Index
- (NSUInteger)indexOfItemAtPoint:(NSPoint)p
Please sign in to comment.
Something went wrong with that request. Please try again.