-
Notifications
You must be signed in to change notification settings - Fork 43
/
UIAlertView+UITableView.m
184 lines (155 loc) · 5.67 KB
/
UIAlertView+UITableView.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
//
// UIAlertView+UITableView.m
// IguanaGet
//
// Created by Sergio on 03/06/11.
// Copyright 2011 Sergio. All rights reserved.
//
#import "UIAlertView+UITableView.h"
@implementation AlertTableView
@synthesize caller, context, data;
@synthesize itemValue;
@synthesize sectionSelected;
@synthesize selectedRow;
- (id)initWithCaller:(id<AlertTableViewDelegate>)_caller
data:(NSArray*)_data
title:(NSString*)_title
context:(id)_context
dictionary:(NSMutableDictionary *)item
section:(int)section
row:(int)row;
{
NSMutableString *messageString = [NSMutableString stringWithString:@"\n"];
tableHeight = 0;
if([_data count] < 6)
{
for(int i = 0; i < [_data count]; i++)
{
[messageString appendString:@"\n\n"];
tableHeight += 53;
}
}
else
{
messageString = [NSMutableString stringWithString:@"\n\n\n\n\n\n\n\n\n\n"];
tableHeight = 207;
}
if ((self = [super initWithTitle:_title
message:messageString
delegate:self
cancelButtonTitle:nil
otherButtonTitles:NSLocalizedString(@"cancel", @""), NSLocalizedString(@"uncheckItem", @""), nil]))
{
self.caller = _caller;
self.context = _context;
self.data = _data;
self.itemValue = item;
self.sectionSelected = section;
self.selectedRow = row;
[self prepare];
}
return self;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
if ([self.caller respondsToSelector:@selector(didSelectRowAtIndex:section:withContext:text:andItem:row:)])
{
[self.caller didSelectRowAtIndex:self.selectedRow
section:self.sectionSelected
withContext:self.context
text:@"-1"
andItem:nil
row:self.selectedRow];
}
}
if (buttonIndex == 1)
{
if ([self.caller respondsToSelector:@selector(didSelectRowAtIndex:section:withContext:text:andItem:row:)])
{
[self.caller didSelectRowAtIndex:self.selectedRow
section:self.sectionSelected
withContext:nil
text:@"-2"
andItem:self.itemValue
row:self.selectedRow];
}
}
}
- (void)show
{
self.hidden = YES;
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(myTimer:) userInfo:nil repeats:NO];
[super show];
}
- (void)myTimer:(NSTimer *)_timer
{
self.hidden = NO;
[myTableView flashScrollIndicators];
}
- (void)prepare
{
myTableView = [[UITableView alloc] initWithFrame:CGRectMake(11, 50, 261, tableHeight) style:UITableViewStylePlain];
if([data count] < 5)
{
myTableView.scrollEnabled = NO;
}
myTableView.delegate = self;
myTableView.dataSource = self;
[self addSubview:myTableView];
UIImageView *imgView = [[[UIImageView alloc] initWithFrame:CGRectMake(11, 50, 261, 4)] autorelease];
imgView.image = [UIImage imageNamed:@"top.png"];
[self addSubview:imgView];
imgView = [[[UIImageView alloc] initWithFrame:CGRectMake(11, tableHeight+46, 261, 4)] autorelease];
imgView.image = [UIImage imageNamed:@"bottom.png"];
[self addSubview:imgView];
CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, 10);
[self setTransform:myTransform];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = (UITableViewCell*) [tableView dequeueReusableCellWithIdentifier:@"ABC"];
if (cell == nil)
{
//cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"ABC"] autorelease];
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
}
cell.textLabel.text = [[data objectAtIndex:indexPath.row] description];
int rowSelected = [[self.itemValue valueForKey:@"rowSelected"] intValue];
if ((rowSelected != -1) && (indexPath.row == rowSelected))
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self dismissWithClickedButtonIndex:0 animated:YES];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[self.itemValue setValue:[NSString stringWithFormat:@"%d", indexPath.row] forKey:@"rowSelected"];
if ([self.caller respondsToSelector:@selector(didSelectRowAtIndex:section:withContext:text:andItem:row:)])
{
[self.caller didSelectRowAtIndex:indexPath.row
section:self.sectionSelected
withContext:self.context
text:cell.textLabel.text
andItem:itemValue
row:self.selectedRow];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [data count];
}
- (void)dealloc
{
self.data = nil;
self.caller = nil;
self.context = nil;
[myTableView release];
[super dealloc];
}
@end