/
TiUITableViewRowProxy.m
988 lines (893 loc) Β· 28.8 KB
/
TiUITableViewRowProxy.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
#ifdef USE_TI_UITABLEVIEW
#import "TiUITableViewRowProxy.h"
#import "TiUITableViewAction.h"
#import "TiUITableViewSectionProxy.h"
#import "TiUITableView.h"
#import "TiViewProxy.h"
#import "TiUtils.h"
#import "Webcolor.h"
#import "ImageLoader.h"
#import "TiSelectedCellbackgroundView.h"
#import "TiLayoutQueue.h"
#import <libkern/OSAtomic.h>
NSString * const defaultRowTableClass = @"_default_";
#define CHILD_ACCESSORY_WIDTH 20.0
#define CHECK_ACCESSORY_WIDTH 20.0
#define DETAIL_ACCESSORY_WIDTH 33.0
#define IOS7_ACCESSORY_EXTRA_OFFSET 15.0
// TODO: Clean this up a bit
#define NEEDS_UPDATE_ROW 1
#ifdef TI_USE_AUTOLAYOUT
@interface TiUITableViewRowContainer : TiLayoutView
#else
@interface TiUITableViewRowContainer : UIView
#endif
{
TiProxy * hitTarget;
CGPoint hitPoint;
#ifdef TI_USE_AUTOLAYOUT
CGFloat m_height;
CGFloat m_width;
#endif
}
@property(nonatomic,retain,readwrite) TiProxy * hitTarget;
@property(nonatomic,assign,readwrite) CGPoint hitPoint;
-(void)clearHitTarget;
@end
TiProxy * DeepScanForProxyOfViewContainingPoint(UIView * targetView, CGPoint point)
{
if (!CGRectContainsPoint([targetView bounds], point))
{
return nil;
}
for (UIView * subView in [targetView subviews])
{
TiProxy * subProxy = DeepScanForProxyOfViewContainingPoint(subView,[targetView convertPoint:point toView:subView]);
if (subProxy != nil)
{
return subProxy;
}
}
//By now, no subviews have claimed ownership.
if ([targetView respondsToSelector:@selector(proxy)])
{
return [(TiUIView *)targetView proxy];
}
return nil;
}
@implementation TiUITableViewRowContainer
@synthesize hitTarget, hitPoint;
-(id)init
{
if (self = [super init]) {
hitPoint = CGPointZero;
}
return self;
}
-(void)clearHitTarget
{
[hitTarget autorelease];
hitTarget = nil;
}
-(NSString*)apiName
{
return @"Ti.UI.TableViewRow";
}
-(TiProxy *)hitTarget
{
TiProxy * result = hitTarget;
[self clearHitTarget];
return result;
}
- (UIView *)hitTest:(CGPoint) point withEvent:(UIEvent *)event
{
UIView * result = [super hitTest:point withEvent:event];
[self setHitPoint:point];
if (result==nil)
{
[self setHitTarget:DeepScanForProxyOfViewContainingPoint(self,point)];
return nil;
}
if ([result respondsToSelector:@selector(proxy)])
{
[self setHitTarget:[(TiUIView *)result proxy]];
}
else
{
[self clearHitTarget];
}
return result;
}
- (void) dealloc
{
[self clearHitTarget];
[super dealloc];
}
#ifdef TI_USE_AUTOLAYOUT
-(void)initializeTiLayoutView
{
[super initializeTiLayoutView];
[self setDefaultWidth:TiDimensionAutoSize];
[self setDefaultHeight:TiDimensionAutoSize];
[self setHeight_:@"SIZE"];
}
-(CGFloat)heightIfWidthWere:(CGFloat)width
{
if (m_width != width) {
m_width = width;
m_height = [super heightIfWidthWere:width];
}
if (m_height == 0) {
m_height = [super heightIfWidthWere:width];
}
return m_height;
}
#endif
@end
@implementation TiUITableViewRowProxy
@synthesize tableClass, table, section, row, callbackCell;
@synthesize reusable = reusable_;
-(void)_destroy
{
RELEASE_TO_NIL(tableClass);
#ifdef TI_USE_KROLL_THREAD
TiThreadRemoveFromSuperviewOnMainThread(rowContainerView, NO);
TiThreadReleaseOnMainThread(rowContainerView, NO);
#else
TiThreadPerformOnMainThread(^{
[rowContainerView removeFromSuperview];
RELEASE_TO_NIL(rowContainerView);
}, YES);
#endif
rowContainerView = nil;
[callbackCell setProxy:nil];
callbackCell = nil;
[super _destroy];
}
-(void)_initWithProperties:(NSDictionary *)properties
{
[super _initWithProperties:properties];
self.modelDelegate = self;
}
-(NSString*)tableClass
{
if (tableClass==nil)
{
// must use undefined key since class is a special
// property on the NSObject class
id value = [self valueForUndefinedKey:@"className"];
if (value==nil)
{
value = defaultRowTableClass;
}
// tableClass must always be a string so we coerce it
tableClass = [[TiUtils stringValue:value] retain];
}
return tableClass;
}
-(id)height
{
return [self valueForUndefinedKey:@"height"];
}
-(void)setHeight:(id)value
{
height = [TiUtils dimensionValue:value];
[self replaceValue:value forKey:@"height" notification:YES];
}
-(id) backgroundLeftCap
{
return [self valueForUndefinedKey:@"backgroundLeftCap"];
}
-(void)setBackgroundLeftCap:(id)value
{
leftCap = TiDimensionFromObject(value);
[self replaceValue:value forKey:@"backgroundLeftCap" notification:NO];
if (callbackCell != nil) {
[self configureBackground:callbackCell];
}
}
-(id) backgroundTopCap
{
return [self valueForUndefinedKey:@"backgroundTopCap"];
}
-(void)setBackgroundTopCap:(id)value
{
topCap = TiDimensionFromObject(value);
[self replaceValue:value forKey:@"backgroundTopCap" notification:NO];
if (callbackCell != nil) {
[self configureBackground:callbackCell];
}
}
// Special handling to try and avoid Apple's detection of private API 'layout'
-(void)setValue:(id)value forUndefinedKey:(NSString *)key
{
if ([key isEqualToString:[@"lay" stringByAppendingString:@"out"]]) {
//CAN NOT USE THE MACRO
if (ENFORCE_BATCH_UPDATE) {
if (updateStarted) {
[self setTempProperty:value forKey:key]; \
return;
}
else if(!allowLayoutUpdate){
return;
}
}
#ifndef TI_USE_AUTOLAYOUT
layoutProperties.layoutStyle = TiLayoutRuleFromObject(value);
#else
[[self currentRowContainerView] setLayout_:value];
#endif
[self replaceValue:value forKey:[@"lay" stringByAppendingString:@"out"] notification:YES];
return;
}
[super setValue:value forUndefinedKey:key];
}
-(CGFloat)sizeWidthForDecorations:(CGFloat)oldWidth forceResizing:(BOOL)force
{
CGFloat width = oldWidth;
BOOL updateForiOS7 = NO;
if (force || !configuredChildren) {
if ([TiUtils boolValue:[self valueForKey:@"hasChild"] def:NO]) {
width -= CHILD_ACCESSORY_WIDTH;
updateForiOS7 = YES;
}
else if ([TiUtils boolValue:[self valueForKey:@"hasDetail"] def:NO]) {
width -= DETAIL_ACCESSORY_WIDTH;
updateForiOS7 = YES;
}
else if ([TiUtils boolValue:[self valueForKey:@"hasCheck"] def:NO]) {
width -= CHECK_ACCESSORY_WIDTH;
updateForiOS7 = YES;
}
id rightImage = [self valueForKey:@"rightImage"];
if (rightImage != nil) {
NSURL *url = [TiUtils toURL:rightImage proxy:self];
UIImage *image = [[ImageLoader sharedLoader] loadImmediateImage:url];
width -= [image size].width;
}
id leftImage = [self valueForKey:@"leftImage"];
if (leftImage != nil) {
NSURL *url = [TiUtils toURL:leftImage proxy:self];
UIImage *image = [[ImageLoader sharedLoader] loadImmediateImage:url];
width -= [image size].width;
}
}
if (updateForiOS7) {
width -= IOS7_ACCESSORY_EXTRA_OFFSET;
}
return width;
}
-(CGFloat)rowHeight:(CGFloat)width
{
if (TiDimensionIsDip(height))
{
return height.value;
}
CGFloat result = 0;
#ifndef TI_USE_AUTOLAYOUT
if (TiDimensionIsAuto(height) || TiDimensionIsAutoSize(height) || TiDimensionIsUndefined(height))
{
result = [self minimumParentHeightForSize:CGSizeMake(width, [self table].bounds.size.height)];
}
if (TiDimensionIsPercent(height) && [self table] != nil) {
result = TiDimensionCalculateValue(height, [self table].bounds.size.height);
}
#else
result = [(TiLayoutView*)[self currentRowContainerView] heightIfWidthWere:width];
result = result == 0 ? 0 : result + 1;
#endif
return (result == 0) ? [table tableRowHeight:0] : result;
}
-(void)updateRow:(NSDictionary *)data withObject:(NSDictionary *)properties
{
modifyingRow = YES;
[super _initWithProperties:data];
// check to see if we have a section header change, too...
if ([data objectForKey:@"header"])
{
[section setValue:[data objectForKey:@"header"] forUndefinedKey:@"headerTitle"];
// we can return since we're reloading the section, will cause the
// row to be repainted at the same time
}
if ([data objectForKey:@"footer"])
{
[section setValue:[data objectForKey:@"footer"] forUndefinedKey:@"footerTitle"];
// we can return since we're reloading the section, will cause the
// row to be repainted at the same time
}
modifyingRow = NO;
}
-(void)configureTitle:(UITableViewCell*)cell
{
UILabel * textLabel = [cell textLabel];
NSString *title = [self valueForKey:@"title"];
if (title!=nil)
{
[textLabel setText:title]; //UILabel already checks to see if it hasn't changed.
UIColor * textColor = [[TiUtils colorValue:[self valueForKey:@"color"]] _color];
[textLabel setTextColor:(textColor==nil)?[UIColor blackColor]:textColor];
UIColor * selectedTextColor = [[TiUtils colorValue:[self valueForKey:@"selectedColor"]] _color];
[textLabel setHighlightedTextColor:(selectedTextColor==nil)?[UIColor whiteColor]:selectedTextColor];
id fontValue = [self valueForKey:@"font"];
UIFont * font;
if (fontValue!=nil)
{
font = [[TiUtils fontValue:fontValue] font];
}
else
{
font = [UIFont systemFontOfSize:0];
}
[textLabel setFont:font];
}
else
{
[textLabel setText:nil];
}
}
-(void)configureRightSide:(UITableViewCell*)cell
{
BOOL hasChild = [TiUtils boolValue:[self valueForKey:@"hasChild"] def:NO];
if (hasChild)
{
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
else
{
BOOL hasDetail = [TiUtils boolValue:[self valueForKey:@"hasDetail"] def:NO];
if (hasDetail)
{
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
else
{
BOOL hasCheck = [TiUtils boolValue:[self valueForKey:@"hasCheck"] def:NO];
if (hasCheck)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
}
id rightImage = [self valueForKey:@"rightImage"];
if (rightImage!=nil)
{
NSURL *url = [TiUtils toURL:rightImage proxy:self];
UIImage *image = [[ImageLoader sharedLoader] loadImmediateImage:url];
cell.accessoryView = [[[UIImageView alloc] initWithImage:image] autorelease];
}
else {
cell.accessoryView = nil;
}
}
-(void)configureBackground:(UITableViewCell*)cell
{
[(TiUITableViewCell *)cell setBackgroundGradient_:[self valueForKey:@"backgroundGradient"]];
[(TiUITableViewCell *)cell setSelectedBackgroundGradient_:[self valueForKey:@"selectedBackgroundGradient"]];
id bgImage = [self valueForKey:@"backgroundImage"];
id selBgColor = [self valueForKey:@"selectedBackgroundColor"];
if (bgImage!=nil)
{
NSURL *url = [TiUtils toURL:bgImage proxy:(TiProxy*)table.proxy];
UIImage *image = [[ImageLoader sharedLoader] loadImmediateStretchableImage:url withLeftCap:leftCap topCap:topCap];
if ([cell.backgroundView isKindOfClass:[UIImageView class]]==NO)
{
UIImageView *view_ = [[[UIImageView alloc] initWithFrame:CGRectZero] autorelease];
cell.backgroundView = view_;
}
if (image!=((UIImageView*)cell.backgroundView).image)
{
((UIImageView*)cell.backgroundView).image = image;
}
}
else if (cell.backgroundView!=nil && [cell.backgroundView isKindOfClass:[UIImageView class]] && ((UIImageView*)cell.backgroundView).image!=nil)
{
cell.backgroundView = nil;
}
id selBgImage = [self valueForKey:@"selectedBackgroundImage"];
if (selBgImage!=nil) {
NSURL *url = [TiUtils toURL:selBgImage proxy:(TiProxy*)table.proxy];
UIImage *image = [[ImageLoader sharedLoader] loadImmediateStretchableImage:url withLeftCap:leftCap topCap:topCap];
if ([cell.selectedBackgroundView isKindOfClass:[UIImageView class]]==NO) {
UIImageView *view_ = [[[UIImageView alloc] initWithFrame:CGRectZero] autorelease];
cell.selectedBackgroundView = view_;
}
if (image!=((UIImageView*)cell.selectedBackgroundView).image) {
((UIImageView*)cell.selectedBackgroundView).image = image;
}
UIColor* theColor = [Webcolor webColorNamed:selBgColor];
cell.selectedBackgroundView.backgroundColor = ((theColor == nil)?[UIColor clearColor]:theColor);
} else {
if (![cell.selectedBackgroundView isKindOfClass:[TiSelectedCellBackgroundView class]]) {
cell.selectedBackgroundView = [[[TiSelectedCellBackgroundView alloc] initWithFrame:CGRectZero] autorelease];
}
TiSelectedCellBackgroundView *selectedBGView = (TiSelectedCellBackgroundView*)cell.selectedBackgroundView;
selectedBGView.grouped = [[table tableView] style]==UITableViewStyleGrouped;
UIColor* theColor = [Webcolor webColorNamed:selBgColor];
if (theColor == nil) {
switch (cell.selectionStyle) {
case UITableViewCellSelectionStyleGray:theColor = [Webcolor webColorNamed:@"#bbb"];break;
case UITableViewCellSelectionStyleNone:theColor = [UIColor clearColor];break;
case UITableViewCellSelectionStyleBlue:theColor = [Webcolor webColorNamed:@"#0272ed"];break;
default:theColor = [Webcolor webColorNamed:@"#e0e0e0"];break;
}
}
selectedBGView.fillColor = theColor;
NSInteger count = [section rowCount];
if (count == 1) {
selectedBGView.position = TiCellBackgroundViewPositionSingleLine;
}
else {
if (row == 0) {
selectedBGView.position = TiCellBackgroundViewPositionTop;
}
else if (row == count-1) {
selectedBGView.position = TiCellBackgroundViewPositionBottom;
}
else {
selectedBGView.position = TiCellBackgroundViewPositionMiddle;
}
}
}
}
-(void)configureLeftSide:(UITableViewCell*)cell
{
id image = [self valueForKey:@"leftImage"];
if (image!=nil)
{
NSURL *url = [TiUtils toURL:image proxy:(TiProxy*)table.proxy];
UIImage *image = [[ImageLoader sharedLoader] loadImmediateImage:url];
if (cell.imageView.image!=image)
{
cell.imageView.image = image;
}
}
else if (cell.imageView!=nil && cell.imageView.image!=nil)
{
cell.imageView.image = nil;
}
}
-(void)configureIndentionLevel:(UITableViewCell*)cell
{
cell.indentationLevel = [TiUtils intValue:[self valueForKey:@"indentionLevel"] def:0];
}
-(void)configureSelectionStyle:(UITableViewCell*)cell
{
id value = [self valueForKey:@"selectionStyle"];
if (value == nil)
{
if (table!=nil)
{
// look at the tableview if not on the row
value = [[table proxy] valueForUndefinedKey:@"selectionStyle"];
}
}
if (value!=nil)
{
cell.selectionStyle = [TiUtils intValue:value];
}
else
{
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
}
}
-(UIView *)parentViewForChild:(TiViewProxy *)child
{
return rowContainerView;
}
-(BOOL)viewAttached
{
return (callbackCell != nil) && (callbackCell.proxy == self);
}
-(BOOL)canHaveControllerParent
{
return NO;
}
-(void)redelegateViews:(TiViewProxy *)proxy toView:(UIView *)touchDelegate;
{
[[proxy view] setTouchDelegate:touchDelegate];
NSArray* subproxies = [proxy children];
for (TiViewProxy * childProxy in subproxies)
{
[self redelegateViews:childProxy toView:touchDelegate];
}
}
-(TiProxy*)parentForBubbling
{
return section;
}
-(UIView*)view
{
return nil;
}
//Private method : For internal use only
-(TiUITableViewRowContainer*) currentRowContainerView
{
if (rowContainerView == nil) {
rowContainerView = [[TiUITableViewRowContainer alloc] init];
}
return (TiUITableViewRowContainer*)rowContainerView;
}
//Private method :For internal use only. Called from layoutSubviews of the cell.
-(void)triggerLayout
{
if (modifyingRow) {
return;
}
modifyingRow = YES;
#ifndef TI_USE_AUTOLAYOUT
[TiLayoutQueue layoutProxy:self];
#endif
modifyingRow = NO;
}
- (void)prepareTableRowForReuse
{
if (!self.reusable) {
[rowContainerView removeFromSuperview];
return;
}
if (![self.tableClass isEqualToString:defaultRowTableClass]) {
return;
}
RELEASE_TO_NIL(rowContainerView);
// ... But that's not enough. We need to detatch the views
// for all children of the row, to clean up memory.
for (TiViewProxy* child in [self children]) {
[child detachView];
}
}
- (void)didReceiveMemoryWarning:(NSNotification *)notification
{
if (self.viewAttached) {
return;
}
RELEASE_TO_NIL(rowContainerView);
for (TiViewProxy* child in [self children]) {
[child detachView];
}
}
-(void)configureChildren:(UITableViewCell*)cell
{
// this method is called when the cell is initially created
// to be initialized. on subsequent repaints of a re-used
// table cell, the updateChildren below will be called instead
configuredChildren = NO;
if ([[self children] count] > 0)
{
UIView *contentView = cell.contentView;
CGRect rect = [contentView bounds];
rect.origin = CGPointZero;
if (self.reusable || (rowContainerView == nil)) {
if (self.reusable) {
RELEASE_TO_NIL(rowContainerView);
for (UIView* subview in [[cell contentView] subviews]) {
if ([subview isKindOfClass:[TiUITableViewRowContainer class]]) {
rowContainerView = [subview retain];
break;
}
}
}
NSArray *rowChildren = [self children];
if (self.reusable && (rowContainerView != nil)) {
__block BOOL canReproxy = YES;
NSArray *existingSubviews = [rowContainerView subviews];
if ([rowChildren count] != [existingSubviews count]) {
canReproxy = NO;
} else {
[rowChildren enumerateObjectsUsingBlock:^(TiViewProxy *proxy, NSUInteger idx, BOOL *stop) {
TiUIView *uiview = [existingSubviews objectAtIndex:idx];
if (![uiview validateTransferToProxy:proxy deep:YES]) {
canReproxy = NO;
*stop = YES;
}
}];
}
if (!canReproxy && ([existingSubviews count] > 0)) {
DebugLog(@"[ERROR] TableViewRow structures for className %@ does not match", self.tableClass);
[existingSubviews enumerateObjectsUsingBlock:^(TiUIView *child, NSUInteger idx, BOOL *stop) {
[(TiViewProxy *)child.proxy detachView];
}];
}
}
if (rowContainerView == nil) {
rowContainerView = [[TiUITableViewRowContainer alloc] initWithFrame:rect];
} else {
[rowContainerView setFrame:rect];
}
if ([rowContainerView superview] == nil) {
[contentView addSubview:rowContainerView];
}
[rowContainerView setBackgroundColor:[UIColor clearColor]];
[rowContainerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
NSArray *existingSubviews = [rowContainerView subviews];
[rowChildren enumerateObjectsUsingBlock:^(TiViewProxy *proxy, NSUInteger idx, BOOL *stop) {
TiUIView *uiview = idx < [existingSubviews count] ? [existingSubviews objectAtIndex:idx] : nil;
if (!CGRectEqualToRect([proxy sandboxBounds], rect)) {
[proxy setSandboxBounds:rect];
}
[proxy windowWillOpen];
[proxy setReproxying:YES];
[uiview transferProxy:proxy deep:YES];
[self redelegateViews:proxy toView:contentView];
if (uiview == nil) {
[rowContainerView addSubview:[proxy view]];
}
[proxy setReproxying:NO];
}];
} else {
[[self children] enumerateObjectsUsingBlock:^(TiViewProxy *proxy, NSUInteger idx, BOOL *stop) {
[self redelegateViews:proxy toView:contentView];
}];
[rowContainerView setFrame:rect];
[contentView addSubview:rowContainerView];
}
#ifdef TI_USE_AUTOLAYOUT
[rowContainerView performSelector:@selector(updateWidthAndHeight)];
#endif
}
configuredChildren = YES;
}
-(void)configureTintColor:(UITableViewCell*)cell
{
UIColor* theTint = nil;
id theColor = [self valueForUndefinedKey:@"tintColor"];
if (theColor != nil) {
theTint = [[TiUtils colorValue:theColor] color];
}
if (theTint == nil) {
theTint = [[table tableView] tintColor];
}
[cell setTintColor:theTint];
}
-(void)initializeTableViewCell:(UITableViewCell*)cell
{
modifyingRow = YES;
[self configureTintColor:cell];
[self configureTitle:cell];
[self configureSelectionStyle:cell];
[self configureLeftSide:cell];
[self configureRightSide:cell];
[self configureBackground:cell];
[self configureIndentionLevel:cell];
[self configureChildren:cell];
cell.accessibilityLabel = [TiUtils stringValue:[self valueForUndefinedKey:@"accessibilityLabel"]];
cell.accessibilityValue = [TiUtils stringValue:[self valueForUndefinedKey:@"accessibilityValue"]];
cell.accessibilityHint = [TiUtils stringValue:[self valueForUndefinedKey:@"accessibilityHint"]];
modifyingRow = NO;
}
-(BOOL)isAttached
{
return (table!=nil) && ([self parent]!=nil);
}
// TODO: SUPER MEGA UGLY but it's the only workaround for now. zindex does NOT work with table rows.
// TODO: Add child locking methods for whenever we have to touch children outside TiViewProxy
-(void)willShow
{
NSArray* subproxies = [self children];
for (TiViewProxy* child in subproxies) {
[child setParentVisible:YES];
}
}
-(void)triggerAttach
{
if (!attaching && ![self viewAttached]) {
attaching = YES;
[self windowWillOpen];
[self willShow];
attaching = NO;
}
}
-(void)updateRow:(TiUITableViewAction*)action
{
OSAtomicTestAndClearBarrier(NEEDS_UPDATE_ROW, &dirtyRowFlags);
[table dispatchAction:action];
}
-(void)triggerRowUpdate
{
if ([self isAttached] && self.viewAttached && !modifyingRow && !attaching)
{
if (OSAtomicTestAndSetBarrier(NEEDS_UPDATE_ROW, &dirtyRowFlags)) {
return;
}
TiUITableViewAction *action = [[[TiUITableViewAction alloc] initWithObject:self
animation:nil
type:TiUITableViewActionRowReload] autorelease];
TiThreadPerformOnMainThread(^{[self updateRow:action];}, NO);
}
}
-(void)windowWillOpen
{
attaching = YES;
[super windowWillOpen];
[self setParentVisible:YES];
attaching = NO;
}
-(void)triggerUpdateIfHeightChanged
{
TiThreadPerformOnMainThread(^{
if ([self viewAttached] && rowContainerView != nil) {
CGFloat curHeight = rowContainerView.bounds.size.height;
CGSize newSize = [callbackCell computeCellSize];
if (newSize.height != curHeight) {
DeveloperLog(@"Height changing from %.1f to %.1f. Triggering update.",curHeight,newSize.height);
[self triggerRowUpdate];
} else {
DeveloperLog(@"Height does not change. Just laying out children. Height %.1f",curHeight);
//TIMOB-13121. Ensure touchdelegate is set if we are not going to reconstruct the row.
if ([rowContainerView superview] != nil) {
UIView* contentView = [rowContainerView superview];
[[self children] enumerateObjectsUsingBlock:^(TiViewProxy *proxy, NSUInteger idx, BOOL *stop) {
[self redelegateViews:proxy toView:contentView];
}];
}
[callbackCell setNeedsDisplay];
}
} else {
[callbackCell setNeedsDisplay];
}
}, NO);
}
-(void)contentsWillChange
{
if (attaching==NO)
{
[self triggerUpdateIfHeightChanged];
}
}
-(void)childWillResize:(TiViewProxy *)child
{
[self triggerUpdateIfHeightChanged];
}
-(TiProxy *)touchedViewProxyInCell:(UITableViewCell *)targetCell atPoint:(CGPoint*)point
{
for (TiUITableViewRowContainer * thisContainer in [[targetCell contentView] subviews])
{
if ([thisContainer isKindOfClass:[TiUITableViewRowContainer class]])
{
TiProxy * result = [thisContainer hitTarget];
*point = [thisContainer hitPoint];
if (result != nil)
{
return result;
}
}
}
return self;
}
-(id)createEventObject:(id)initialObject
{
NSMutableDictionary *dict = nil;
if (initialObject == nil)
{
dict = [NSMutableDictionary dictionary];
}
else
{
dict = [NSMutableDictionary dictionaryWithDictionary:initialObject];
}
NSInteger index = [table indexForRow:self];
[dict setObject:NUMINTEGER(index) forKey:@"index"];
// TODO: We really need to ensure that a row's section is set upon creation - even if this means changing how tables work.
if (section != nil) {
[dict setObject:section forKey:@"section"];
}
[dict setObject:self forKey:@"row"];
[dict setObject:self forKey:@"rowData"];
[dict setObject:NUMBOOL(NO) forKey:@"detail"];
[dict setObject:NUMBOOL(NO) forKey:@"searchMode"];
return dict;
}
//TODO: Remove when deprication is done.
-(void)fireEvent:(NSString*)type withObject:(id)obj withSource:(id)source propagate:(BOOL)propagate reportSuccess:(BOOL)report errorCode:(int)code message:(NSString*)message;
{
// merge in any row level properties for the event
if (source!=self)
{
obj = [self createEventObject:obj];
}
[callbackCell handleEvent:type];
[super fireEvent:type withObject:obj withSource:source propagate:propagate reportSuccess:report errorCode:code message:message];
}
-(void)fireEvent:(NSString*)type withObject:(id)obj propagate:(BOOL)propagate reportSuccess:(BOOL)report errorCode:(NSInteger)code message:(NSString*)message;
{
[callbackCell handleEvent:type];
[super fireEvent:type withObject:obj propagate:propagate reportSuccess:report errorCode:code message:message];
}
-(void)setSelectedBackgroundColor:(id)arg
{
[self replaceValue:arg forKey:@"selectedBackgroundColor" notification:NO];
TiThreadPerformOnMainThread(^{
if ([self viewAttached]) {
[self configureBackground:callbackCell];
}
}, NO);
}
-(void)setBackgroundImage:(id)arg
{
[self replaceValue:arg forKey:@"backgroundImage" notification:NO];
TiThreadPerformOnMainThread(^{
if ([self viewAttached]) {
[self configureBackground:callbackCell];
}
}, NO);
}
-(void)setSelectedBackgroundImage:(id)arg
{
[self replaceValue:arg forKey:@"selectedBackgroundImage" notification:NO];
TiThreadPerformOnMainThread(^{
if ([self viewAttached]) {
[self configureBackground:callbackCell];
}
}, NO);
}
-(void)setBackgroundGradient:(id)arg
{
TiGradient * newGradient = [TiGradient gradientFromObject:arg proxy:self];
[self replaceValue:newGradient forKey:@"backgroundGradient" notification:NO];
TiThreadPerformOnMainThread(^{[callbackCell setBackgroundGradient_:newGradient];}, NO);
}
-(void)setSelectedBackgroundGradient:(id)arg
{
TiGradient * newGradient = [TiGradient gradientFromObject:arg proxy:self];
[self replaceValue:newGradient forKey:@"selectedBackgroundGradient" notification:NO];
TiThreadPerformOnMainThread(^{[callbackCell setSelectedBackgroundGradient_:newGradient];}, NO);
}
-(void)propertyChanged:(NSString*)key oldValue:(id)oldValue newValue:(id)newValue proxy:(TiProxy*)proxy
{
// these properties should trigger a re-paint for the row
static NSSet * TableViewRowProperties = nil;
if (TableViewRowProperties==nil)
{
TableViewRowProperties = [[NSSet alloc] initWithObjects:
@"title", @"accessibilityLabel", @"backgroundImage",
@"leftImage",@"hasDetail",@"hasCheck",@"hasChild",
@"indentionLevel",@"selectionStyle",@"color",@"selectedColor",
@"height",@"width",@"backgroundColor",@"rightImage",@"tintColor",
nil];
}
if ([TableViewRowProperties member:key]!=nil)
{
TiThreadPerformOnMainThread(^{
if (![self viewAttached]) {
return;
}
if ([key isEqualToString:@"height"] || [key isEqualToString:@"width"] || [key isEqualToString:@"indentionLevel"]) {
[self triggerRowUpdate];
} else if ([key isEqualToString:@"title"] || [key isEqualToString:@"color"] || [key isEqualToString:@"font"] || [key isEqualToString:@"selectedColor"]) {
[self configureTitle:callbackCell];
[callbackCell setNeedsDisplay];
} else if ([key isEqualToString:@"hasCheck"] || [key isEqualToString:@"hasChild"] || [key isEqualToString:@"hasDetail"] || [key isEqualToString:@"rightImage"]) {
[self configureRightSide:callbackCell];
[self triggerUpdateIfHeightChanged];
} else if ([key isEqualToString:@"leftImage"]) {
[self configureLeftSide:callbackCell];
[self triggerUpdateIfHeightChanged];
} else if ([key isEqualToString:@"backgroundImage"]) {
[self configureBackground:callbackCell];
[callbackCell setNeedsDisplay];
} else if ([key isEqualToString:@"backgroundColor"]) {
[callbackCell setBackgroundColor:[[TiUtils colorValue:newValue] color]];
[self triggerRowUpdate];
} else if ([key isEqualToString:@"accessibilityLabel"]){
callbackCell.accessibilityLabel = [TiUtils stringValue:newValue];
} else if ([key isEqualToString:@"tintColor"]){
[self configureTintColor:callbackCell];
}
}, NO);
}
}
-(TiDimension)defaultAutoHeightBehavior:(id)unused
{
return TiDimensionAutoSize;
}
@end
#endif