.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
#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,0) style: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];
至于原理自己研究吧,不做细说