-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUTEditableTableView.m
243 lines (194 loc) · 5.6 KB
/
UTEditableTableView.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//
// UTEditableTableView.m
// RingFinder
//
// Created by Danny Morrow on 10/29/10.
// Copyright 2010 unitytheory.com. All rights reserved.
//
#import "UTEditableTableView.h"
#import "UTTableViewCell.h"
#import "UTTableViewPhotoPickerCell.h"
#import "UTTableViewPListDataSource.h"
@implementation UTEditableTableView
@synthesize maxHeight = _maxHeight;
@synthesize dataModel = _dataModel;
- (id) initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
if (self = [super initWithFrame:frame style:style])
{
_dataModel = [NSMutableDictionary dictionary];
self.delegate = self;
[self addObservers];
}
return self;
}
#pragma mark -
#pragma mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[((UTTableViewCell *)[tableView cellForRowAtIndexPath:indexPath]) select];
//[self reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
}
/*
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//DLog(@"kind of %@", [tableView cellForRowAtIndexPath:indexPath]);
NSDictionary* data = [(UTTableViewPListDataSource*)tableView.dataSource dataAtIndexPath:indexPath];
if ([[data valueForKey:@"Type"] isEqualToString:kPhotoPicker])
{
if ([[self.dataModel valueForKey:[data valueForKey:@"Key"]] isKindOfClass:[NSDictionary class]])
{
return 55;
}
}
return 38;//tableView.rowHeight;
}
*/
#pragma mark -
#pragma mark tableviewcell delegates
- (void) scrollBack
{
if (!_keyboardVisible)
{
_firstResponder = nil;
_selectedCell = nil;
}
}
-(void)cellDidBeginEditting:(UTTableViewCell *)cell
{
_originalFrame = self.frame;
[_firstResponder resignFirstResponder];
_firstResponder = cell.responder;
_selectedCell = cell;
DLog(@"begin editting");
if ( _keyboardVisible)
{
[self scrollToRowAtIndexPath:[self indexPathForCell:_selectedCell] atScrollPosition:UITableViewScrollPositionTop animated:TRUE];
}
}
- (void)cellDidEndEditting:(UTTableViewCell *)cell
{
[self scrollBack];
}
#pragma mark -
#pragma mark keyboard
- (void)addObservers
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:@"UIKeyboardWillShowNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:@"UIKeyboardWillHideNotification" object:nil];
}
- (void)removeObservers
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIKeyboardWillShowNotification" object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIKeyboardWillHideNotification" object:nil];
}
- (void)keyboardWillHide:(NSNotification*)notification
{
//[self setContentSize:CGSizeMake(320, self.maxHeight+50) animated:TRUE];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:.3];
[UIView setAnimationBeginsFromCurrentState:TRUE];
UIView* footer = self.tableFooterView;
CGRect f = footer.frame;
f.size.height -= 200;
footer.frame = f;
self.tableFooterView = footer;
[UIView commitAnimations];
_keyboardVisible = FALSE;
}
- (void)keyboardWillShow:(NSNotification *)notification
{
//[self setContentSize:CGSizeMake(320, self.maxHeight+50+240) animated:FALSE];
UIView* footer = self.tableFooterView;
CGRect f = footer.frame;
f.size.height += 200;
footer.frame = f;
self.tableFooterView = footer;
_keyboardVisible = TRUE;
[self scrollToRowAtIndexPath:[self indexPathForCell:_selectedCell] atScrollPosition:UITableViewScrollPositionTop animated:TRUE];
_selectedCell = nil;
}
- (void) scrollTo:(CGPoint)offset animated:(BOOL) animated
{
if (animated)
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:.3];
[UIView setAnimationBeginsFromCurrentState:TRUE];
}
self.contentOffset = offset;
if (animated)
{
[UIView commitAnimations];
}
}
- (void) setContentSize:(CGSize)size animated:(BOOL)animated
{
if (animated)
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:.3];
[UIView setAnimationBeginsFromCurrentState:TRUE];
}
self.contentSize = size;
if (animated)
{
[UIView commitAnimations];
}
}
- (void) setFrame:(CGRect)frame animated:(BOOL)animated
{
if (animated)
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:.3];
[UIView setAnimationBeginsFromCurrentState:TRUE];
}
self.frame = frame;
if (animated)
{
[UIView commitAnimations];
}
}
- (void) closeKeyboard
{
if ([_firstResponder respondsToSelector:@selector(resignFirstResponder)])
{
[_firstResponder resignFirstResponder];
}
}
- (void) setMaxHeight:(float)h
{
_maxHeight = h;//CGRectGetMaxY(f);
//DLog(@"max height %f", _maxHeight);
//self.contentSize = CGSizeMake(320, _maxHeight+300);
}
- (void) valueDidChange:(UTTableViewCell*)cell value:(id)val key:(NSString*)key
{
if ([self.dataModel valueForKey:key] != val)
{
[self.dataModel setValue:val forKey:key];
/*
if ([cell isKindOfClass:[UTTableViewPhotoPickerCell class]])
{
[self reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[self indexPathForCell:cell], nil] withRowAnimation:UITableViewRowAnimationNone];
}
*/
//DLog(@"%@ %@ %@", val, key, self.dataModel);
}
}
- (void) setValue:(id)value forKey:(NSString *)key
{
[self.dataModel setValue:value forKey:key];
[self reloadData];
}
- (void) dealloc
{
//self.dataModel = nil;
[self removeObservers];
}
@end