自訂UIAlertView(UITableView)

來源:互聯網
上載者:User

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

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

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

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

樣本圖:

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.