自定义iOS下拉框


 

#import <UIKit/UIKit.h>


@protocol PushBankCardDelegate <NSObject>


- (void)PushBankCardBindingView;

- (void)showbankcardInfo:(NSInteger) index;

@end



@interface ComBox :UIView<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>

{

    UITableView *tv;//下拉列表

   NSMutableArray *tableArray;//下拉列表数据

   UITextField *textField;//文本输入框

   CGFloat tabheight;//table下拉列表的高度

   CGFloat frameHeight;//frame的高度

}

@property (nonatomic,retain) id <PushBankCardDelegate> delegate;

@property (nonatomic,retain)UITableView *tv;


@property (nonatomic,retain)NSMutableArray *tableArray;


@property (nonatomic,assignBOOL showList;


@property (nonatomic,retain)UITextField *textField;

标记

//@property BOOL editeState;

@end




#import "ComBox.h"

#import "BankCardBindingViewController.h"

@implementation ComBox


@synthesize tv,tableArray,textField;



- (id)initWithFrame:(CGRect)frame

{

   if (frame.size.height<200) {

       frameHeight = 200;

    }else{

        frameHeight = frame.size.height;

    }

    tabheight = frameHeight-30;

    

    frame.size.height =30.0f;

    

   self = [superinitWithFrame:frame];

   if(self){

       _showList = NO;//默认不显示下拉框

        

        tv = [[UITableViewalloc] initWithFrame:CGRectMake(0,30, frame.size.width +80 , 0)];

       tv.delegate =self;

       tv.dataSource =self;

        tv.backgroundColor = [UIColorclearColor];

        tv.separatorColor = [UIColorlightGrayColor];

       tv.hidden =YES;

        

//        //初始化状态编辑

//        self.editeState = YES;

        

        [selfaddSubview:tv];

        

        textField = [[UITextFieldalloc] initWithFrame:CGRectMake(0,0, frame.size.width +80, 30)];

        textField.font = [UIFontsystemFontOfSize:13.0f];

        //textField.userInteractionEnabled = NO;

        textField.borderStyle=UITextBorderStyleRoundedRect;//设置文本框的边框风格

        [textFieldaddTarget:selfaction:@selector(dropdown)forControlEvents:UIControlEventAllTouchEvents];

        [selfaddSubview:textField];

        

    }

    return self;

}


-(void)dropdown{

    [textField resignFirstResponder];

    if (_showList) {//如果下拉框已显示,什么都不做

       tv.hidden =YES;

       _showList = NO;

       return;

    }else {//如果下拉框尚未显示,则进行显示

        //dropdownList放到前面,防止下拉框被别的控件遮住

        [self.superviewbringSubviewToFront:self];

       tv.hidden =NO;

       _showList = YES;//显示下拉框

        

       CGRect frame = tv.frame;

        tabheight =tableArray.count*50;

        

        frameHeight =tabheight;

       CGRect sf = self.frame;

        sf.size.height =frameHeight+40;

        

        frame.size.height =tabheight;

        [UIViewbeginAnimations:@"ResizeForKeyBoard"context:nil];

        [UIViewsetAnimationCurve:UIViewAnimationCurveLinear];

       self.frame = sf;

       tv.frame = frame;

        [UIViewcommitAnimations];

    }

    //tabview分割线左对齐

   if ([self.tvrespondsToSelector:@selector(setSeparatorInset:)])

    {

        [self.tvsetSeparatorInset:UIEdgeInsetsZero];

    }

   if ([self.tvrespondsToSelector:@selector(setLayoutMargins:)])

    {

        [self.tvsetLayoutMargins:UIEdgeInsetsZero];

    }

    

    [tv reloadData];

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 1;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return [tableArraycount];

}

//tabview分割线左对齐

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{

   if ([cell respondsToSelector:@selector(setSeparatorInset:)])

    {

        [cell setSeparatorInset:UIEdgeInsetsZero];

    }

   if ([cell respondsToSelector:@selector(setLayoutMargins:)])

    {

        [cell setLayoutMargins:UIEdgeInsetsZero];

    }

}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

   static NSString *CellIdentifier =@"Cell";

    

   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

   if (cell == nil) {

        cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier] ;

    }

    cell.textLabel.text = [tableArrayobjectAtIndex:[indexPath row]];

    cell.textLabel.font = [UIFontsystemFontOfSize:13.0f];

    cell.accessoryType  =UITableViewCellAccessoryNone;

    cell.selectionStyle =UITableViewCellSelectionStyleGray;

    

   return cell;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 50;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSInteger index = tableArray.count-1;

   if(index == [indexPath row])

    {

        [self.delegate PushBankCardBindingView];

    }

   else

    {

        textField.text = [tableArray objectAtIndex:[indexPath row]];

        [self.delegate showbankcardInfo:[indexPath row]];

    }

    _showList =NO;

    tv.hidden =YES;

    

    CGRect sf =self.frame;

    sf.size.height =30;

   self.frame = sf;

    CGRect frame = tv.frame;

    frame.size.height =0;

    tv.frame = frame;

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    // Return YES for supported orientations

   return (interfaceOrientation == UIInterfaceOrientationPortrait);

}



设置单元格能否被移动

//-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

//{

//    

//    if (self.editeState)

//    {

//        return YES;//编辑状态可移动

//    }

//    else

//    {

//        return NO;

//    }

//}

移动的方法

//-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

//{

//    //移动的起始行

//    NSInteger firstRow=[sourceIndexPath row];

//    //移动的目标航

//    NSInteger targetRow=[destinationIndexPath row];

//    //开始移动

//    [self.tableArray exchangeObjectAtIndex:firstRow withObjectAtIndex:targetRow];//数据的数组必须是可变的

//}

//

设置单元格能否被编辑

//-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

//{

//    NSLog(@"%ld , %lu ",(long)indexPath.row,(unsigned long)tableArray.count);

//    if (!indexPath.row - tableArray.count == 0)

//    {

//        return YES;

//        

//    }

//    return NO;

//}

//

设置单元格被编辑的具体方法

//-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

//{

//    //开始跟新

//    [tableView beginUpdates];

//    switch (editingStyle)

//    {

        case UITableViewCellEditingStyleInsert://添加

            //往数组里添加

            [self.data insertObject:@"太阳" atIndex:indexPath.row];

            //添加到表格

            [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

            break;

//        case UITableViewCellEditingStyleDelete://删除

//            [self.tableArray removeObjectAtIndex:indexPath.row];

//            //从表格删除

//            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];

//            break;

//            

//        default:

//            break;

//    }

//    //结束跟新

//    [tableView endUpdates];

//}

//#pragma mark - UITableViewDelegate

//

单元格编辑样式

//-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

//{

//    if (self.editeState)

//    {

//        return UITableViewCellEditingStyleDelete;

//    }

//    else

//        return UITableViewCellEditingStyleInsert;

//    

//}



@end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值