-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatTableView.m
executable file
·457 lines (318 loc) · 12 KB
/
ChatTableView.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//
// ChatTableView.m
// ChatDemo
//
// Created by Charlene Jiang on 10/10/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "ChatTableView.h"
#import "ChatTableViewCell.h"
@implementation UserProfile
@synthesize userPhoto, userName;
- (id)initWithUserName:(NSString *)name photo:(UIImage *)photo
{
if (self = [super init]) {
self.userPhoto = photo;
self.userName = name;
}
return self;
}
@end
@implementation MessageItem
@synthesize isMe, message, messageDate;
- (id)initWithMessage:(NSString *)msg date:(NSDate *)msgDate isMe:(BOOL)value
{
if (self = [super init]) {
self.isMe = value;
self.message = msg;
// self.messageDate = msgDate;
}
return self;
}
@end
@implementation ChatTableView
@synthesize me, myFriend, messageItems;
@synthesize chatDelegate;
const NSInteger kViewTag = 1;
- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
self = [super initWithFrame:frame style:style];
if (self) {
// Initialization code here.
self.delegate = self;
self.dataSource = self;
self.separatorColor = [UIColor clearColor];
self.messageItems = [[NSMutableArray alloc] init];
EGORefreshTableHeaderView *view = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - self.bounds.size.height, self.frame.size.width, self.bounds.size.height)];
view.delegate = self;
[self addSubview:view];
_refreshHeaderView = view;
[view release];
// update the last update date
[_refreshHeaderView refreshLastUpdatedDate];
}
return self;
}
#pragma mark -
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) {
return 1;
}
if (section == 1) {
if (messageItems) {
return [messageItems count];
}
}
return 0;
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
tableView.backgroundColor = [UIColor clearColor];
// tableView.userInteractionEnabled = NO;
UITableViewCell *cell = nil;
NSUInteger section = [indexPath section];
if (section == 0)
{
static NSString *kCellHistory_ID = @"CellHistory_ID";
cell = [tableView dequeueReusableCellWithIdentifier:kCellHistory_ID];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellHistory_ID] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
}
if (section == 1) {
MessageItem *msgItem = [self.messageItems objectAtIndex:indexPath.row];
static NSString *kCellHistory_ID = @"CellHistory_ID";
if (msgItem.isMe) {
kCellHistory_ID = @"CellMyMessage_ID";
} else {
kCellHistory_ID = @"CellMyFriendMessage_ID";
}
cell = [tableView dequeueReusableCellWithIdentifier:kCellHistory_ID];
if (cell == nil) {
cell = [[[ChatTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellHistory_ID] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (msgItem.isMe) {
((ChatTableViewCell *)cell).profile = me;
} else {
((ChatTableViewCell *)cell).profile = myFriend;
}
}
((ChatTableViewCell *)cell).msg = msgItem;
[(ChatTableViewCell *)cell drawCell];
}
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
// Editing
// Individual rows can opt out of having the -editing property set for them. If not implemented, all rows are assumed to be editable.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 1) {
return YES;
}
return NO;
}
// Moving/reordering
// Allows the reorder accessory view to optionally be shown for a particular row. By default, the reorder control will be shown only if the datasource implements -tableView:moveRowAtIndexPath:toIndexPath:
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 1) {
return YES;
}
return NO;
}
// Data manipulation - insert and delete support
// After a row has the minus or plus button invoked (based on the UITableViewCellEditingStyle for the cell), the dataSource must commit the change
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
}
// Data manipulation - reorder / moving support
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
}
#pragma mark -
#pragma mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
}
// Variable height support
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
return 50;
}
if (indexPath.section == 2) {
return 50;
}
if (indexPath.row >= [messageItems count]) {
return 0;
}
NSString *theStringToDisplay = ((MessageItem *)[messageItems objectAtIndex:indexPath.row]).message;
float height = [ChatTableViewCell heightOfCellWithMessage:theStringToDisplay];
if (height < 66) {
height = 66;
}
return height;
}
// Accessories (disclosures).
//
//- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
//{
// return UITableViewCellAccessoryNone;
//}
//
//- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
//{
//
//}
//
// Selection
// Called before the user changes the selection. Return a new indexPath, or nil, to change the proposed selection.
//- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
// return nil;
//}
//
//- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath
//// Called after the user changes the selection.
//{
// return nil;
//}
//
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//}
//
//- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//
//}
// Editing
// Allows customization of the editingStyle for a particular cell located at 'indexPath'. If not implemented, all editable cells will have UITableViewCellEditingStyleDelete set for them when the table has editing property set to YES.
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 1) {
return UITableViewCellEditingStyleInsert;
}
return UITableViewCellEditingStyleNone;
}
// Controls whether the background is indented while editing. If not implemented, the default is YES. This is unrelated to the indentation level below. This method only applies to grouped style table views.
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
// The willBegin/didEnd methods are called whenever the 'editing' property is automatically changed by the table (allowing insert/delete/move). This is done by a swipe activating a single row
- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
}
- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
}
// Moving/reordering
// Allows customization of the target row for a particular row as it is being moved/reordered
//- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath;
//{
//
//}
// Indentation
//
//- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
//// return 'depth' of row for hierarchies
//{
//
//}
#pragma mark -
- (void)historyButtonAction:(id)sender
{
[self.messageItems removeAllObjects];
[self reloadData];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[chatDelegate shouldDismissKeyboard];
}
#pragma -
- (void)messageRecieved:(MessageItem *)msg
{
if (msg) {
[messageItems addObject:msg];
[self reloadData];
}
if ([messageItems count] == 0) {
return;
}
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[messageItems count] - 1 inSection:1];
[self scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
- (void)scrollToBottom
{
if ([messageItems count] == 0) {
return;
}
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[messageItems count] - 1 inSection:1];
[self scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
#define DefaultContentLabelFont 13
#define DefaultContentLabelHeight
#define DefaultCellSize CGSizeMake(300,40)
- (CGFloat) heightOfContent: (NSString *)content
{
}
#pragma mark -
#pragma mark UIScrollViewDelegate Methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
[_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
[_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
}
#pragma mark -
#pragma mark EGORefreshTableHeaderDelegate Methods
- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view{
[self reloadTableViewDataSource];
[self performSelector:@selector(doneLoadingTableViewData) withObject:nil afterDelay:2.0];
}
- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view{
return _reloading; // should return if data source model is reloading
}
- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view{
return [NSDate date]; // should return date data source was last changed
}
#pragma mark -
#pragma mark Data Source Loading / Reloading Methods
- (void)reloadTableViewDataSource{
// should be calling your tableviews data source model to reload
// put here just for demo
_reloading = YES;
}
- (void)doneLoadingTableViewData
{
// model should call this when its done loading
_reloading = NO;
for (int i = 0; i < 2; i ++)
{
NSDate *oldestDate = [NSDate date];
if ([messageItems count] > 0)
{
MessageItem *oldestItem = [messageItems objectAtIndex:0];
oldestDate = oldestItem.messageDate;
}
/*
MessageItem *item = [[MessageItem alloc] initWithMessage:@"Xixi ^_^" date:[oldestDate dateByAddingTimeInterval:-10] isMe:YES];
[messageItems insertObject:item atIndex:0];
item = [[MessageItem alloc] initWithMessage:@"Haha :)" date:[oldestDate dateByAddingTimeInterval:-20] isMe:NO];
[messageItems insertObject:item atIndex:0];
*/
}
[self reloadData];
[_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self];
}
@end