-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpandableCell.m
183 lines (147 loc) · 6.44 KB
/
ExpandableCell.m
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
//
// ExpandableCell.m
// AvocadoTest1
//
// Created by Jake on 12-03-30.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "ExpandableCell.h"
#import "ExpandableCellData.h"
#import "ListRenderer.h"
@implementation ExpandableCell
-(id)init
{
self = [super init];
if (self) {
colapsedImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ColapsedMenuDropdown.png"]];
expandedImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ExpandedMenuDropdown.png"]];
nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(93, 24, 120, 21)];
[nameLabel setFont:[Utilities fravicHeadingFont]];
[nameLabel setTextAlignment:UITextAlignmentCenter];
[nameLabel setTextColor:[UIColor blackColor]];
[nameLabel setBackgroundColor:[UIColor clearColor]];
[nameLabel setAdjustsFontSizeToFitWidth:YES];
numberOfItemsLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 50, 100, 21)];
[numberOfItemsLabel setFont:[Utilities fravicTextFont]];
[numberOfItemsLabel setTextColor:[Utilities fravicDarkRedColor]];
[numberOfItemsLabel setBackgroundColor:[UIColor clearColor]];
[numberOfItemsLabel setTextAlignment:UITextAlignmentRight];
[self addSubview:colapsedImage];
[self addSubview:expandedImage];
[self addSubview:nameLabel];
[self addSubview:numberOfItemsLabel];
}
return self;
}
+(BOOL) canDisplayData:(id)data
{
//This class is meant to be subclassed. If this were ever true, the subclasses
//wouldn't be instantiated by CustomViewCell.
return NO;
}
+(NSString *) cellIdentifier
{
@throw [NSException exceptionWithName:NSInternalInconsistencyException
reason:[NSString stringWithFormat:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)]
userInfo:nil];
//Must return a unique string identifier for this type of cell.
return @"ExpandableCell";
}
-(void) setData:(id) data
{
//In theory this will never actually get called.
if (![[self class] canDisplayData:data])
{
CLLog(LOG_LEVEL_ERROR, @"An unsupported data type was sent to ExpandableCell's setData:");
return;
}
expandableData = data;
if ([data isOpen]) {
[self initializeOpen];
}
else {
[self initializeClosed];
}
}
+(CGFloat)cellHeightForData:(id)data
{
if ([data isOpen]) {
return [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ExpandedMenuDropdown.png"]] frame].size.height;
}
return [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ColapsedMenuDropdown.png"]] frame].size.height;
}
-(void)transitionToOpen:(NSIndexPath *) selfLocation :(UITableView *)table
{
if (![expandableData isOpen]) {
[expandableData setIsOpen:YES];
//Replace this cell with one that's open
[[[[expandableData renderer] listData] objectAtIndex:[selfLocation section]] replaceObjectAtIndex:[selfLocation row] withObject:expandableData];
//Reload the cell so that the new open cell gets displayed
[table reloadRowsAtIndexPaths:[NSArray arrayWithObject:selfLocation] withRowAnimation:UITableViewRowAnimationFade];
//Now we need to insert all of the expansion contents into the renderer:
NSMutableArray *helperArray = [[NSMutableArray alloc] init];
for (int i=1; i<([[expandableData expansionContents] count] + 1); i++)
{
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:(selfLocation.row + i) inSection:selfLocation.section];
[helperArray addObject:tmpIndexPath];
}
int i = [selfLocation row] + 1;
for (id eachItem in [expandableData expansionContents]) {
[[[[expandableData renderer] listData] objectAtIndex:[selfLocation section]] insertObject:eachItem atIndex:i];
i++;
}
[table insertRowsAtIndexPaths:helperArray withRowAnimation:UITableViewRowAnimationTop];
[table scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[selfLocation row] inSection:[selfLocation section]] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
else {
CLLog(LOG_LEVEL_ERROR, @"A transition to open was attempted on an expandable cell that is already open.");
}
}
-(void)transitionToClosed:(NSIndexPath *) selfLocation:(UITableView *)table
{
if ([expandableData isOpen]) {
[expandableData setIsOpen:NO];
//Replace this cell with one that's closed
[[[[expandableData renderer] listData] objectAtIndex:[selfLocation section]] replaceObjectAtIndex:[selfLocation row] withObject:expandableData];
//Reload the cell so that the new open cell gets displayed
[table reloadRowsAtIndexPaths:[NSArray arrayWithObject:selfLocation] withRowAnimation:UITableViewRowAnimationFade];
//Now we need to remove all of the cells that are associated with this expandable cell
NSMutableArray *helperArray = [[NSMutableArray alloc] init];
for (int i=1; i<([[expandableData expansionContents] count] + 1); i++)
{
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:(selfLocation.row + i) inSection:selfLocation.section];
[helperArray addObject:tmpIndexPath];
}
for (id eachItem in [expandableData expansionContents]) {
[[[[expandableData renderer] listData] objectAtIndex:[selfLocation section]] removeObjectAtIndex:([selfLocation row] + 1)];
}
[table deleteRowsAtIndexPaths:helperArray withRowAnimation:UITableViewRowAnimationTop];
}
else {
CLLog(LOG_LEVEL_ERROR, @"A transition to closed was attempted on an expandable cell that is already closed.");
}
}
-(void)toggleOpen:(NSIndexPath *)selfLocation :(UITableView *)table
{
if ([expandableData isOpen]) {
[self transitionToClosed:selfLocation :table];
}
else {
[self transitionToOpen:selfLocation :table];
}
}
-(void)initializeOpen
{
[nameLabel setAlpha:1.0f];
[numberOfItemsLabel setAlpha:0.0f];
[colapsedImage setAlpha:0.0f];
[expandedImage setAlpha:1.0f];
}
-(void)initializeClosed
{
[nameLabel setAlpha:1.0f];
[numberOfItemsLabel setAlpha:1.0f];
[colapsedImage setAlpha:1.0f];
[expandedImage setAlpha:0.0f];
}
@end