This repository has been archived by the owner on May 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
DTGridView.h
224 lines (187 loc) · 8.68 KB
/
DTGridView.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//
// DTGridView.h
// GridViewTester
//
// Created by Daniel Tull on 05.12.2008.
// Copyright 2008 Daniel Tull. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "DTGridViewCell.h"
/*!
@enum DTGridViewScrollPosition
@abstract Used to determine how to position a grid view cell on screen when scrolling to it.
@constant DTGridViewScrollPositionNone Aligns the cell such that the shortest distance to display as much of the cell is used.
@constant DTGridViewScrollPositionTopLeft Aligns the cell so that it is in the top left of the grid view.
@constant DTGridViewScrollPositionTopCenter Aligns the cell so that it is in the top center of the grid view.
@constant DTGridViewScrollPositionTopRight Aligns the cell so that it is in the top right of the grid view.
@constant DTGridViewScrollPositionMiddleLeft Aligns the cell so that it is in the middle left of the grid view.
@constant DTGridViewScrollPositionMiddleCenter Aligns the cell so that it is in the middle center of the grid view.
@constant DTGridViewScrollPositionMiddleRight Aligns the cell so that it is in the middle right of the grid view.
@constant DTGridViewScrollPositionBottomLeft Aligns the cell so that it is in the bottom left of the grid view.
@constant DTGridViewScrollPositionBottomCenter Aligns the cell so that it is in the bottom center of the grid view.
@constant DTGridViewScrollPositionBottomRight Aligns the cell so that it is in the bottom right of the grid view.
@discussion In most cases you will want to use DTGridViewScrollPositionNone to just bring the cell to the screen using the quickest route. In the case where the cell is too big to display completely on screen, the position will still be used, in that the center aligned cells will have their middle in the center of the screen, with their edges outside the screen bounds equally as much.
*/
typedef enum {
DTGridViewScrollPositionNone = 0,
DTGridViewScrollPositionTopLeft,
DTGridViewScrollPositionTopCenter,
DTGridViewScrollPositionTopRight,
DTGridViewScrollPositionMiddleLeft,
DTGridViewScrollPositionMiddleCenter,
DTGridViewScrollPositionMiddleRight,
DTGridViewScrollPositionBottomLeft,
DTGridViewScrollPositionBottomCenter,
DTGridViewScrollPositionBottomRight
} DTGridViewScrollPosition;
/*!
@enum DTGridViewEdge
@abstract Categorizes beverages into groups of similar types.
@constant DTGridViewEdgeTop Sweet, carbonated, non-alcoholic beverages.
@constant DTGridViewEdgeBottom Sweet, carbonated, non-alcoholic beverages.
@constant DTGridViewEdgeLeft Sweet, carbonated, non-alcoholic beverages.
@constant DTGridViewEdgeRight Sweet, carbonated, non-alcoholic beverages.
@discussion Extended discussion goes here.
Lorem ipsum....
*/
typedef enum {
DTGridViewEdgeTop,
DTGridViewEdgeBottom,
DTGridViewEdgeLeft,
DTGridViewEdgeRight
} DTGridViewEdge;
struct DTOutset {
CGFloat top;
CGFloat bottom;
CGFloat left;
CGFloat right;
};
@protocol DTGridViewDelegate;
@protocol DTGridViewDataSource;
/*!
@class DTGridView
@abstract
@discussion
*/
@interface DTGridView : UIScrollView <UIScrollViewDelegate, DTGridViewCellDelegate> {
NSObject<DTGridViewDataSource> *dataSource;
CGPoint cellOffset;
UIEdgeInsets outset;
NSMutableArray *gridCells;
NSMutableArray *freeCells;
NSMutableArray *cellInfoForCellsOnScreen;
NSMutableArray *gridRows;
NSMutableArray *rowHeights;
NSMutableArray *rowPositions;
NSMutableArray *cellsOnScreen;
CGPoint oldContentOffset;
BOOL hasResized;
BOOL hasLoadedData;
NSInteger numberOfRows;
NSInteger rowIndexOfSelectedCell;
NSInteger columnIndexOfSelectedCell;
NSTimer *decelerationTimer;
NSTimer *draggingTimer;
}
/*!
@abstract The object that acts as the data source of the receiving grid view.
@discussion The data source must adopt the DTGridViewDataSource protocol. The data source is not retained.
*/
@property (nonatomic, assign) IBOutlet NSObject<DTGridViewDataSource> *dataSource;
/*!
@abstract The object that acts as the delegate of the receiving grid view.
@discussion The delegate must adopt the DTGridViewDelegate protocol. The delegate is not retained.
*/
@property (nonatomic, assign) IBOutlet id<DTGridViewDelegate> delegate;
/*!
@abstract The object that acts as the delegate of the receiving grid view.
@deprecated This property is depricated and you should now use the standard delegate property.
*/
@property (nonatomic, assign) IBOutlet id<DTGridViewDelegate> gridDelegate;
/*!
@abstract The offset for each cell with respect to the cells above and to the right.
@discussion The x and y values can be either positive or negative; Using negative will overlay the cells by that amount, the outcome of this can never be gauranteed what the ordering of cells will be though.
*/
@property (assign) CGPoint cellOffset;
@property (assign) UIEdgeInsets outset;
@property (nonatomic, retain) NSMutableArray *gridCells;
@property (nonatomic) NSInteger numberOfRows;
#pragma mark -
#pragma mark Subclass methods
// These methods can be overridden by subclasses.
// They should never need to be called from outside classes.
- (void)didEndMoving;
- (void)didEndDragging;
- (void)didEndDecelerating;
- (CGFloat)findWidthForRow:(NSInteger)row column:(NSInteger)column;
- (NSInteger)findNumberOfRows;
- (NSInteger)findNumberOfColumnsForRow:(NSInteger)row;
- (CGFloat)findHeightForRow:(NSInteger)row;
- (DTGridViewCell *)findViewForRow:(NSInteger)row column:(NSInteger)column;
#pragma mark -
#pragma mark Regular methods
/*!
@abstract Returns a reusable grid view cell object located by its identifier.
@param identifier A string identifying the cell object to be reused.
@discussion For performance reasons, grid views should always reuse their cells. This works like the table view's reuse policy.
*/
- (DTGridViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier;
/*!
@abstract Returns a grid view cell object located by its row and column positions.
@param rowIndex The index of the row of the wanted cell.
@param columnIndex The index of the column of the wanted cell.
@return The grid view cell of the grid or nil if the cell is not visible or the indexes is out of range.
*/
- (DTGridViewCell *)cellForRow:(NSInteger)rowIndex column:(NSInteger)columnIndex;
/*!
@abstract A constant that identifies a relative position in the receiving table view (top, middle, bottom) for row when scrolling concludes. See “Table View Scroll Position” a descriptions of valid constants.
@param rowIndex The index of the row to scroll to.
@param columnIndex The index of the column to scroll to.
@param position The position the cell should be in once scrolled to.
@param animated If this
*/
- (void)scrollViewToRow:(NSInteger)rowIndex column:(NSInteger)columnIndex scrollPosition:(DTGridViewScrollPosition)position animated:(BOOL)animated;
- (void)selectRow:(NSInteger)rowIndex column:(NSInteger)columnIndex scrollPosition:(DTGridViewScrollPosition)position animated:(BOOL)animated;
/*!
@abstract This method should be used by subclasses to know when the grid did appear on screen.
*/
- (void)didLoad;
/*!
@abstract Call this to reload the grid view's data.
*/
- (void)reloadData;
@end
#pragma mark -
@protocol DTGridViewDelegate <UIScrollViewDelegate>
@optional
/*!
Called when the grid view loads.
*/
- (void)gridViewDidLoad:(DTGridView *)gridView;
- (void)gridView:(DTGridView *)gridView selectionMadeAtRow:(NSInteger)rowIndex column:(NSInteger)columnIndex;
- (void)gridView:(DTGridView *)gridView scrolledToEdge:(DTGridViewEdge)edge;
- (void)pagedGridView:(DTGridView *)gridView didScrollToRow:(NSInteger)rowIndex column:(NSInteger)columnIndex;
- (void)gridView:(DTGridView *)gridView didProgrammaticallyScrollToRow:(NSInteger)rowIndex column:(NSInteger)columnIndex;
@end
#pragma mark -
@protocol DTGridViewDataSource
/*!
Asks the data source to return the number of rows in the grid view.
The grid view object requesting this information.
@return The number of rows in the grid view.
*/
- (NSInteger)numberOfRowsInGridView:(DTGridView *)gridView;
/*!
@abstract Asks the data source to return the number of columns for the given row in the grid view.
@para The grid view object requesting this information.
@para The index of the given row.
@return The number of colums in the row of the grid view.
*/
- (NSInteger)numberOfColumnsInGridView:(DTGridView *)gridView forRowWithIndex:(NSInteger)index;
- (CGFloat)gridView:(DTGridView *)gridView heightForRow:(NSInteger)rowIndex;
- (CGFloat)gridView:(DTGridView *)gridView widthForCellAtRow:(NSInteger)rowIndex column:(NSInteger)columnIndex;
- (DTGridViewCell *)gridView:(DTGridView *)gridView viewForRow:(NSInteger)rowIndex column:(NSInteger)columnIndex;
@optional
- (NSInteger)spacingBetweenRowsInGridView:(DTGridView *)gridView;
- (NSInteger)spacingBetweenColumnsInGridView:(DTGridView *)gridView;
@end