自己做的一个下拉框控件(二级关联)

.h文件:

#import <UIKit/UIKit.h>


@interface DropdownListControllere :UIView<UITableViewDataSource,UITableViewDelegate>{

    

}

@property(nonatomic,retain)NSMutableArray *allarray;

@property(nonatomic,retain)UIButton *chooseValueText;

@property(nonatomic,retain)UITableView *firstTable;

@property(nonatomic,retain)UITableView *secondTable;

@property(nonatomic,retain)NSMutableArray *firstArr;

@property(nonatomic,retain)NSMutableArray *secondArr;

@property(nonatomic,retain)NSMutableArray *Arr;

@property(nonatomic,retain)NSString *chooleValue;


@end


.m文件:

#import "DropdownListControllere.h"


@implementation DropdownListControllere

@synthesize chooseValueText;

@synthesize firstArr;

@synthesize secondArr;

@synthesize firstTable;

@synthesize secondTable;

@synthesize chooleValue;

@synthesize Arr;

@synthesize allarray;

//@synthesize views;


- (id)initWithFrame:(CGRect)frame

{

   self = [superinitWithFrame:frame];

   if (self) {

       double textWidth = self.frame.size.width/2;

        chooseValueText = [UIButtonbuttonWithType:UIButtonTypeCustom];

       chooseValueText.frame=CGRectMake(0,0, 160, 44);

        

        chooseValueText.backgroundColor = [UIColorredColor];

        //chooseValueText.titleLabel.frame= CGRectMake(0, 0, textWidth, 44);

        

        [chooseValueTextaddTarget:selfaction:@selector(dropdown:)forControlEvents:UIControlEventTouchUpInside];

        UITableView *firstTableview = [[UITableViewalloc]initWithFrame:CGRectMake(0,chooseValueText.frame.size.height,chooseValueText.frame.size.width,0style:UITableViewStylePlain];

        firstTableview.backgroundColor = [UIColorredColor];

        [firstTableviewsetSeparatorInset:UIEdgeInsetsZero];

        

        UITableView *secondTableview = [[UITableViewalloc]initWithFrame:CGRectMake(firstTableview.frame.origin.x+chooseValueText.frame.size.width,chooseValueText.frame.size.width,self.frame.size.width,0)style:UITableViewStylePlain];

        secondTableview.backgroundColor = [UIColorredColor];

        // [secondTableview setSeparatorInset:UIEdgeInsetsZero];

        

        firstTableview.tag =1;

        secondTableview.tag =2;

        firstTableview.delegate =self;

        firstTableview.dataSource =self;

        secondTableview.delegate =self;

        secondTableview.dataSource =self;

        

       firstTable = firstTableview;

       secondTable = secondTableview;

        

        [selfaddSubview:chooseValueText];

    }

    return self;

}


-(void)dropdown:(id)sender{

    

    self.frame =CGRectMake(self.frame.origin.x,self.frame.origin.y,320, 400);

    

    //CGRect frame = CGRectMake( 0,chooseValueText.frame.size.height-[firstArr count]*44.0,chooseValueText.frame.size.width,[firstArr count]*44.0);

   CGRect frame;

   if ([firstArrcount]<6) {

        frame = CGRectMake(0, chooseValueText.frame.size.height,chooseValueText.frame.size.width,[firstArrcount]*44.0);

    }

   else{

        frame =CGRectMake(0,chooseValueText.frame.size.height,chooseValueText.frame.size.width,6*44.0+10);

    }

   firstTable.frame = frame;

    [firstTable reloadData];

    [selfaddSubview:firstTable];

    // [self addSubview:views];

    [selfbringSubviewToFront:chooseValueText];

}


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

   return 44.0;

}

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

   if (tableView.tag ==1) {

       return [firstArrcount];

    }else{

       return [secondArrcount];

    }

}

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

   if (tableView.tag ==1) {

       Arr = [NSMutableArrayarrayWithArray:firstArr];

    }

   else{

       Arr = [NSMutableArrayarrayWithArray:secondArr];

    }

   static NSString *CellIdentifier =@"Cell";

   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

   if (cell == nil) {

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

        

    }

    cell.textLabel.text =Arr[indexPath.row];

   return cell;

}

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

    

   if (tableView.tag ==1) {

        NSLog(@"allarray:%@",allarray);

        

       if (![allarray[indexPath.row]isEqual: @""]&&[allarraycount]!=0) {

           secondArr = allarray[indexPath.row ];

            secondTable.frame =CGRectMake(chooseValueText.frame.size.width,chooseValueText.frame.size.height,chooseValueText.frame.size.width,[secondArrcount]*44.0) ;

            

            [selfaddSubview:secondTable];

            [secondTablereloadData];

        }

       else{

            [chooseValueTextsetTitle:firstArr[indexPath.row]forState:UIControlStateNormal];

//            chooseValueText.titleLabel.text = firstArr[indexPath.row];

            [firstTableremoveFromSuperview];

            [secondTableremoveFromSuperview];

           self.frame =CGRectMake(self.frame.origin.x,self.frame.origin.y,320, 44);

        }

    }

   else{

        [chooseValueTextsetTitle:secondArr[indexPath.row]forState:UIControlStateNormal];

//        chooseValueText.titleLabel.text = secondArr[indexPath.row];

        [firstTableremoveFromSuperview];

        [secondTableremoveFromSuperview];

        // [views removeFromSuperview];

        self.frame =CGRectMake(self.frame.origin.x,self.frame.origin.y,320, 44);

    }

}

@end


在要使用的地方这样初始化:

dropdown1 = [[DropdownListControllerealloc]initWithFrame:CGRectMake(0,110, 320, 25)];

    [dropdown1.chooseValueTextsetTitle:@"商品分类" forState:UIControlStateNormal];

    dropdown2 = [[DropdownListControllerealloc]initWithFrame:CGRectMake(160,110, 320, 25)];

    [dropdown2.chooseValueTextsetTitle:@"价格排序" forState:UIControlStateNormal];

    dropdown2.firstArr = [NSMutableArrayarrayWithObjects:@"由高到低",@"由低到高",nil];

    dropdown2.chooseValueText.titleLabel.textColor = [UIColor whiteColor];



给下拉框付值:

dropdown1.firstArr = [[NSMutableArrayalloc]initWithArray:firstcategArr];

dropdown1.allarray = [[NSMutableArrayalloc]initWithArray:secondcategArr];


dropdown2.firstArr = [NSMutableArrayarrayWithObjects:@"由高到低",@"由低到高",nil];


至于原理自己研究吧,不做细说


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值