summaryrefslogtreecommitdiff
path: root/Pods/BlockAlertsAnd-ActionSheets/BlockAlertsDemo/ToAddToYourProjects/BlockTableAlertView.m
blob: a309219eec2cbc2d3a0b31beaddcb084bba0556e (plain)
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
//
//  BlockTableAlertView.m
//  BlockAlertsDemo
//
//  Created by Barrett Jacobsen on 2/14/12.
//  Copyright (c) 2012 CodeCrop Software. All rights reserved.
//

#define SUPPORTS_MULTIPLE_SELECTION [self.tableView respondsToSelector:@selector(setAllowsMultipleSelectionDuringEditing:)]

#define kVerticalSpacing     5
#define kHorizontalMargin   12

#define IS_IPAD UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad

#define IS_LANDSCAPE UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])

#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )

#define kNumMaximumVisibleRowsInTableView (IS_IPAD ? 15 : (IS_LANDSCAPE ? 4 : (IS_IPHONE_5 ? 6 : 5)))

#import "BlockTableAlertView.h"
#import <QuartzCore/QuartzCore.h>
#import <dispatch/dispatch.h>

@implementation BlockTableAlertView
@synthesize tableView=_tableView;
@synthesize type=_type;
@synthesize	didSelectRow;
@synthesize willDismissWithButtonIndex;
@synthesize maxSelection;
@synthesize cellForRow;
@synthesize willPresent;
@synthesize numberOfRowsInTableAlert;

@dynamic indexPathsForSelectedRows;

+ (BlockTableAlertView *)tableAlertWithTitle:(NSString *)title message:(NSString *)message
{
    return [[[BlockTableAlertView alloc] initWithTitle:title message:message] autorelease];
}


- (id)initWithTitle:(NSString *)title message:(NSString *)message {
    self = [super initWithTitle:title message:message];
    
    if (self) {
        if (!SUPPORTS_MULTIPLE_SELECTION)
            selectedItems = [[NSMutableArray alloc] init];
    }
    
    return self;
}

- (void)addComponents:(CGRect)frame {
    [super addComponents:frame];
   
    if (!_tableView) {
		_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
		[_tableView setDelegate:self];
		[_tableView setDataSource:self];
		[_tableView setBackgroundColor:[UIColor colorWithWhite:0.90 alpha:1.0]];
		[_tableView setRowHeight:kDefaultRowHeight];
		[_tableView setSeparatorColor:[UIColor lightGrayColor]];
		_tableView.layer.cornerRadius = kTableCornerRadius;
    }

    CGFloat tableHeight = kDefaultRowHeight * MIN(self.numberOfRowsInTableAlert(self), kNumMaximumVisibleRowsInTableView);
   
    _tableView.frame = CGRectMake(kHorizontalMargin, _height, frame.size.width - kHorizontalMargin * 2, tableHeight);
  
    [_view addSubview:_tableView];
    _height += tableHeight + kVerticalSpacing;
}


- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated {
    if (self.willDismissWithButtonIndex)
        self.willDismissWithButtonIndex(self, buttonIndex);
    
    [super dismissWithClickedButtonIndex:buttonIndex animated:animated];
}

- (void)updateAlertHeight:(CGFloat)oldTableHeight {
    CGFloat newTableHeight = kDefaultRowHeight * MIN(self.numberOfRowsInTableAlert(self), kNumMaximumVisibleRowsInTableView);
    
    if (newTableHeight != oldTableHeight) {
        CGFloat diff = newTableHeight - oldTableHeight;
        [UIView animateWithDuration:0.3 animations:^{
            [self.view.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                UIView *v = obj;
                if (v.frame.origin.y >= _tableView.frame.origin.y + _tableView.frame.size.height)
                    v.frame = CGRectMake(v.frame.origin.x, v.frame.origin.y + diff, v.frame.size.width, v.frame.size.height);
            }];
            
            _tableView.frame = CGRectMake(_tableView.frame.origin.x, _tableView.frame.origin.y, _tableView.frame.size.width, newTableHeight);
            self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height + diff);
        }];
    }
}

- (void)reloadData {
    CGFloat oldTableHeight = _tableView.frame.size.height;

    [self.tableView reloadData];
    
    [self updateAlertHeight:oldTableHeight];
}

- (void)insertRowsAtIndexPaths:(NSArray*)rows {
    CGFloat oldTableHeight = _tableView.frame.size.height;
    
    [self.tableView insertRowsAtIndexPaths:rows withRowAnimation:UITableViewRowAnimationAutomatic];
    
    [self updateAlertHeight:oldTableHeight];
}

- (void)deleteRowsAtIndexPaths:(NSArray*)rows {
    CGFloat oldTableHeight = _tableView.frame.size.height;
    
    [self.tableView deleteRowsAtIndexPaths:rows withRowAnimation:UITableViewRowAnimationAutomatic];
    
    [self updateAlertHeight:oldTableHeight];
}


- (void)show {
    if (!_shown)
        [self setupDisplay];
    
    if (self.willPresent)
        self.willPresent(self);
    
    if (self.type == BlockTableAlertTypeMultipleSelct && SUPPORTS_MULTIPLE_SELECTION) {
        self.tableView.allowsMultipleSelectionDuringEditing = YES;
        self.tableView.editing = YES;
    }

    [super show];
}

#pragma mark - iOS 4 Compatibility

- (NSArray*)indexPathsForSelectedRows {
    if (SUPPORTS_MULTIPLE_SELECTION) {
        return self.tableView.indexPathsForSelectedRows;
    }
    else {
        return selectedItems;
    }
}

- (void)selectRowAtIndexPath:(NSIndexPath*)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scroll {
    if (SUPPORTS_MULTIPLE_SELECTION) {
        [self.tableView selectRowAtIndexPath:indexPath animated:animated scrollPosition:scroll];
    }
    else {
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        
        if (cell.accessoryType == UITableViewCellAccessoryNone) {
            [selectedItems addObject:indexPath];
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
    }
}


#pragma mark - UITableViewDelegate

- (NSIndexPath*)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.type == BlockTableAlertTypeSingleSelect || self.maxSelection == 0)
        return indexPath;
    
    NSIndexPath *toSelect = indexPath;
    
    if ([self.indexPathsForSelectedRows count] + 1 > self.maxSelection)
        toSelect = nil;
    
    return toSelect;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	if (_type == BlockTableAlertTypeSingleSelect)
		[self dismissWithClickedButtonIndex:-1 animated:YES];
	
    if (!SUPPORTS_MULTIPLE_SELECTION) {
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        
        if (cell.accessoryType == UITableViewCellAccessoryNone) {
            [selectedItems addObject:indexPath];
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            
            if (self.didSelectRow)
                self.didSelectRow(self, [indexPath row]);
        }
        else {
            [selectedItems removeObject:indexPath];
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        
        [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
        
        return;
    }
    
	if (self.didSelectRow)
		self.didSelectRow(self, [indexPath row]);
}

#pragma mark - UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {	
	return self.cellForRow(self,[indexPath row]);
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return self.numberOfRowsInTableAlert(self);
}


@end