-
Notifications
You must be signed in to change notification settings - Fork 88
/
SBTableAlert.m
169 lines (130 loc) · 5.31 KB
/
SBTableAlert.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
//
// SBTableAlert.m
// SBTableAlert
//
// Created by Simon Blommegård on 2011-04-08.
// Copyright 2011 Simon Blommegård. All rights reserved.
//
#import "SBTableAlert.h"
#import <QuartzCore/QuartzCore.h>
#import <dispatch/dispatch.h>
@interface SBTableAlert ()
- (id)initWithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelTitle messageFormat:(NSString *)format args:(va_list)args;
- (void)increaseHeightBy:(CGFloat)delta;
- (void)layout;
@end
@implementation SBTableAlert
@synthesize view=_alertView;
@synthesize tableView=_tableView;
@synthesize type=_type;
@synthesize delegate=_delegate;
@synthesize dataSource=_dataSource;
- (id)initWithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelTitle messageFormat:(NSString *)format args:(va_list)args {
if ((self = [super init])) {
NSString *message = format ? [[[NSString alloc] initWithFormat:format arguments:args] autorelease] : nil;
_alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancelTitle otherButtonTitles:nil];
_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;
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidChangeStatusBarOrientationNotification object:nil queue:nil usingBlock:^(NSNotification *n) {
dispatch_after(DISPATCH_TIME_NOW, dispatch_get_main_queue(), ^{[self layout];});
}];
}
return self;
}
- (id)initWithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelTitle messageFormat:(NSString *)message, ... {
va_list list;
va_start(list, message);
self = [self initWithTitle:title cancelButtonTitle:cancelTitle messageFormat:message args:list];
va_end(list);
return self;
}
- (void)dealloc {
[self setTableView:nil];
[self setView:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
#pragma mark -
- (void)show {
[_tableView reloadData];
[_alertView show];
}
#pragma mark - Private
- (void)increaseHeightBy:(CGFloat)delta {
CGPoint c = _alertView.center;
CGRect r = _alertView.frame;
r.size.height += delta;
_alertView.frame = r;
_alertView.center = c;
_alertView.frame = CGRectIntegral(_alertView.frame);
for(UIView *subview in [_alertView subviews]) {
if([subview isKindOfClass:[UIControl class]]) {
CGRect frame = subview.frame;
frame.origin.y += delta;
subview.frame = frame;
}
}
}
- (void)layout {
NSInteger visibleRows = [_tableView numberOfRowsInSection:0];
if (visibleRows > kNumMaximumVisibleRowsInTableView)
visibleRows = kNumMaximumVisibleRowsInTableView;
[self increaseHeightBy:(_tableView.rowHeight * visibleRows)];
if ([_alertView message])
[_tableView setFrame:CGRectMake(12, 75, _alertView.frame.size.width - 24, (_tableView.rowHeight * visibleRows))];
else
[_tableView setFrame:CGRectMake(12, 50, _alertView.frame.size.width - 24, (_tableView.rowHeight * visibleRows))];
[_alertView addSubview:_tableView];
}
#pragma mark -
#pragma mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (_type == SBTableAlertTypeSingleSelect)
[_alertView dismissWithClickedButtonIndex:-1 animated:YES];
if ([_delegate respondsToSelector:@selector(tableAlert:didSelectRow:)])
[_delegate tableAlert:self didSelectRow:[indexPath row]];
}
#pragma mark -
#pragma mark UITableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
return [_dataSource tableAlert:self cellForRow:[indexPath row]];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_dataSource numberOfRowsInTableAlert:self];
}
#pragma mark -
#pragma mark UIAlertViewDelegate
- (void)alertViewCancel:(UIAlertView *)alertView {
if ([_delegate respondsToSelector:@selector(tableAlertCancel:)])
[_delegate tableAlertCancel:self];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ([_delegate respondsToSelector:@selector(tableAlert:clickedButtonAtIndex:)])
[_delegate tableAlert:self clickedButtonAtIndex:buttonIndex];
}
- (void)willPresentAlertView:(UIAlertView *)alertView {
[self layout];
if ([_delegate respondsToSelector:@selector(willPresentTableAlert:)])
[_delegate willPresentTableAlert:self];
}
- (void)didPresentAlertView:(UIAlertView *)alertView {
if ([_delegate respondsToSelector:@selector(didPresentTableAlert:)])
[_delegate didPresentTableAlert:self];
}
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
if ([_delegate respondsToSelector:@selector(tableAlert:willDismissWithButtonIndex:)])
[_delegate tableAlert:self willDismissWithButtonIndex:buttonIndex];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if ([_delegate respondsToSelector:@selector(tableAlert:didDismissWithButtonIndex:)])
[_delegate tableAlert:self didDismissWithButtonIndex:buttonIndex];
}
@end