zoukankan      html  css  js  c++  java
  • 自定义UIAlertView(UITableView)

    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.0f0.0f0.0f0.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

    示例图:

     

  • 相关阅读:
    linux(fedora) 下dvwa 建筑环境
    【ThinkingInC++】2、输入和输出流
    Caused by: java.lang.ClassNotFoundException: javax.transaction.TransactionManager
    SpringMVC注释启用
    XML wsdl soap xslt xsl ide
    一个解析RTSP 的URL函数
    PHP:header()函数
    jquery实现鼠标焦点十字效果
    拼出漂亮的表格
    Oracle中如何插入特殊字符:& 和 ' (多种解决方案)
  • 原文地址:https://www.cnblogs.com/yaoliang11/p/2579003.html
Copyright © 2011-2022 走看看