UIAlertTableView.h
#import <UIKit/UIKit.h>
@class UIAlertView;
@interface UIAlertTableView : UIAlertView {
UIAlertView *alertView;
UITableView *tableView;
int tableHeight;
int tableExtHeight;
id <UITableViewDataSource> dataSource;
id <UITableViewDelegate> tableDelegate;
}
@property (nonatomic, assign) id dataSource;
@property (nonatomic, assign) id tableDelegate;
@property (nonatomic, readonly) UITableView *tableView;
@property (nonatomic, assign) int tableHeight;
- (void)prepare;
@end
@class UIAlertView;
@interface UIAlertTableView : UIAlertView {
UIAlertView *alertView;
UITableView *tableView;
int tableHeight;
int tableExtHeight;
id <UITableViewDataSource> dataSource;
id <UITableViewDelegate> tableDelegate;
}
@property (nonatomic, assign) id dataSource;
@property (nonatomic, assign) id tableDelegate;
@property (nonatomic, readonly) UITableView *tableView;
@property (nonatomic, assign) int tableHeight;
- (void)prepare;
@end
UIAlertTableView.m
#import "UIAlertTableView.h"
#define kTablePadding 8.0f
@interface UIAlertView (private)
- (void)layoutAnimated:(BOOL)fp;
@end
@implementation UIAlertTableView
@synthesize dataSource;
@synthesize tableDelegate;
@synthesize tableHeight;
@synthesize tableView;
- (void)layoutAnimated:(BOOL)fp {
[super layoutAnimated:fp];
[self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y - tableExtHeight / 2, self.frame.size.width, self.frame.size.height + tableExtHeight)];
UIView *lowestView;
int i = 0;
while (![[self.subviews objectAtIndex:i] isKindOfClass:[UIControl class]]) {
lowestView = [self.subviews objectAtIndex:i];
i++;
}
CGFloat tableWidth = 262.0f;
tableView.frame = CGRectMake(11.0f, lowestView.frame.origin.y + lowestView.frame.size.height + 2 * kTablePadding, tableWidth, tableHeight);
for (UIView *v in self.subviews) {
if ([v isKindOfClass:[UIControl class]]) {
v.frame = CGRectMake(v.frame.origin.x, v.frame.origin.y + tableExtHeight, v.frame.size.width, v.frame.size.height);
}
}
}
- (void)show{
[self prepare];
[super show];
}
- (void)prepare {
if (tableHeight == 0) {
tableHeight = 150.0f;
}
tableExtHeight = tableHeight + 2 * kTablePadding;
tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 0.0f, 0.0f) style:UITableViewStylePlain];
tableView.backgroundColor = [UIColor orangeColor];
tableView.delegate = tableDelegate;
tableView.dataSource = dataSource;
[self insertSubview:tableView atIndex:0];
[self setNeedsLayout];
}
- (void)dealloc {
[tableView release];
[super dealloc];
}
@end
#define kTablePadding 8.0f
@interface UIAlertView (private)
- (void)layoutAnimated:(BOOL)fp;
@end
@implementation UIAlertTableView
@synthesize dataSource;
@synthesize tableDelegate;
@synthesize tableHeight;
@synthesize tableView;
- (void)layoutAnimated:(BOOL)fp {
[super layoutAnimated:fp];
[self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y - tableExtHeight / 2, self.frame.size.width, self.frame.size.height + tableExtHeight)];
UIView *lowestView;
int i = 0;
while (![[self.subviews objectAtIndex:i] isKindOfClass:[UIControl class]]) {
lowestView = [self.subviews objectAtIndex:i];
i++;
}
CGFloat tableWidth = 262.0f;
tableView.frame = CGRectMake(11.0f, lowestView.frame.origin.y + lowestView.frame.size.height + 2 * kTablePadding, tableWidth, tableHeight);
for (UIView *v in self.subviews) {
if ([v isKindOfClass:[UIControl class]]) {
v.frame = CGRectMake(v.frame.origin.x, v.frame.origin.y + tableExtHeight, v.frame.size.width, v.frame.size.height);
}
}
}
- (void)show{
[self prepare];
[super show];
}
- (void)prepare {
if (tableHeight == 0) {
tableHeight = 150.0f;
}
tableExtHeight = tableHeight + 2 * kTablePadding;
tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 0.0f, 0.0f) style:UITableViewStylePlain];
tableView.backgroundColor = [UIColor orangeColor];
tableView.delegate = tableDelegate;
tableView.dataSource = dataSource;
[self insertSubview:tableView atIndex:0];
[self setNeedsLayout];
}
- (void)dealloc {
[tableView release];
[super dealloc];
}
@end
AlertTableViewController.h
#import <UIKit/UIKit.h>
@interface AlertTableViewController : UIViewController <UIAlertViewDelegate, UITableViewDelegate, UITableViewDataSource> {
UITableView *myTableView;
NSArray *array;
NSIndexPath *lastIndexPath;
}
@property (nonatomic, retain) NSIndexPath *lastIndexPath;
- (IBAction)btnClick:sender;
@end
@interface AlertTableViewController : UIViewController <UIAlertViewDelegate, UITableViewDelegate, UITableViewDataSource> {
UITableView *myTableView;
NSArray *array;
NSIndexPath *lastIndexPath;
}
@property (nonatomic, retain) NSIndexPath *lastIndexPath;
- (IBAction)btnClick:sender;
@end
AlertTableViewController.m
#import "AlertTableViewController.h"
#import "UIAlertTableView.h"
@implementation AlertTableViewController
@synthesize lastIndexPath;
- (void)viewDidLoad {
array = [[NSArray alloc] initWithObjects:@"test1", @"test2", @"test3", @"test4", nil];
[super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
NSUInteger oldRow = [lastIndexPath row];
cell.textLabel.text = [array objectAtIndex:row];
cell.accessoryType = (row == oldRow && lastIndexPath != nil) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
return indexPath;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 40;
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int newRow = [indexPath row];
int oldRow = [lastIndexPath row];
if ((newRow == 0 && oldRow == 0) || (newRow != oldRow)){
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
lastIndexPath = [indexPath retain];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (IBAction)btnClick:sender
{
UIAlertTableView *alert = [[UIAlertTableView alloc] initWithTitle:@"Select Option"
message:nil
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Do", nil];
alert.tableDelegate = self;
alert.dataSource = self;
alert.tableHeight = 120;
[alert show];
[alert release];
}
- (void)dealloc {
[lastIndexPath release];
[array release];
[super dealloc];
}
@end
#import "UIAlertTableView.h"
@implementation AlertTableViewController
@synthesize lastIndexPath;
- (void)viewDidLoad {
array = [[NSArray alloc] initWithObjects:@"test1", @"test2", @"test3", @"test4", nil];
[super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
NSUInteger oldRow = [lastIndexPath row];
cell.textLabel.text = [array objectAtIndex:row];
cell.accessoryType = (row == oldRow && lastIndexPath != nil) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
return indexPath;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 40;
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int newRow = [indexPath row];
int oldRow = [lastIndexPath row];
if ((newRow == 0 && oldRow == 0) || (newRow != oldRow)){
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
lastIndexPath = [indexPath retain];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (IBAction)btnClick:sender
{
UIAlertTableView *alert = [[UIAlertTableView alloc] initWithTitle:@"Select Option"
message:nil
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Do", nil];
alert.tableDelegate = self;
alert.dataSource = self;
alert.tableHeight = 120;
[alert show];
[alert release];
}
- (void)dealloc {
[lastIndexPath release];
[array release];
[super dealloc];
}
@end
示例图: