#import <UIKit/UIKit.h>
@interface DropDownList : UIView<UITableViewDelegate,UITableViewDataSource> {
UITextField* textField; //文本输入框
NSArray* list; //下拉列表数据
BOOL showList; //是否弹出下拉列表
UITableView* listView; //下拉列表
CGRect oldFrame,newFrame; //整个控件(包括下拉前和下拉后)的矩形
UIColor *lineColor,*listBgColor;//下拉框的边框色、背景色
CGFloat lineWidth; //下拉框边框粗细
UITextBorderStyle borderStyle; //文本框边框style
}
@property (nonatomic,retain)UITextField *textField;
@property (nonatomic,retain)NSArray* list;
@property (nonatomic,retain)UITableView* listView;
@property (nonatomic,retain)UIColor *lineColor,*listBgColor;
@property (nonatomic,assign)UITextBorderStyle borderStyle;
-(void)drawView;
-(void)setShowList:(BOOL)b;
@end
#import "DropDownList.h"
@implementation DropDownList
@synthesize textField,list,listView,lineColor,listBgColor,borderStyle;
- (id)initWithFrame:(CGRect)frame {
if(self=[super initWithFrame:frame]){
//默认的下拉列表中的数据
list=[[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",nil];
borderStyle=UITextBorderStyleRoundedRect;
showList=NO; //默认不显示下拉框
oldFrame=frame; //未下拉时控件初始大小
//当下拉框显示时,计算出控件的大小。
newFrame=CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height*5);
lineColor=[UIColor lightGrayColor];//默认列表边框线为灰色
listBgColor=[UIColor whiteColor];//默认列表框背景色为白色
lineWidth=1; //默认列表边框粗细为1
//把背景色设置为透明色,否则会有一个黑色的边
self.backgroundColor=[UIColor clearColor];
[self drawView];//调用方法,绘制控件
}
return self;
}
-(void)drawView{
//文本框
textField=[[UITextField alloc]
initWithFrame:CGRectMake(0, 0,
oldFrame.size.width,
oldFrame.size.height)];
textField.borderStyle=borderStyle;//设置文本框的边框风格
[self addSubview:textField];
[textField addTarget:self action:@selector(dropdown) forControlEvents:UIControlEventAllTouchEvents];
//下拉列表
listView=[[UITableView alloc]initWithFrame:
CGRectMake(lineWidth,oldFrame.size.height+lineWidth,
oldFrame.size.width-lineWidth*2,
oldFrame.size.height*4-lineWidth*2)];
listView.dataSource=self;
listView.delegate=self;
listView.backgroundColor=listBgColor;
listView.separatorColor=lineColor;
listView.hidden=!showList;//一开始listView是隐藏的,此后根据showList的值显示或隐藏
[self addSubview:listView];
[listView release];
}
-(void)dropdown{
[textField resignFirstResponder];
if (showList) {//如果下拉框已显示,什么都不做
return;
}else {//如果下拉框尚未显示,则进行显示
//把dropdownList放到前面,防止下拉框被别的控件遮住
[self.superview bringSubviewToFront:self];
[self setShowList:YES];//显示下拉框
}
}
#pragma mark listViewdataSource method and delegate method
-(NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section{
return list.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellid=@"listviewid";
UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:cellid];
if(cell==nil){
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellid]autorelease];
}
//文本标签
cell.textLabel.text=(NSString*)[list objectAtIndex:indexPath.row];
cell.textLabel.font=textField.font;
cell.selectionStyle=UITableViewCellSelectionStyleGray;
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return oldFrame.size.height;
}
//当选择下拉列表中的一行时,设置文本框中的值,隐藏下拉列表
-(void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//NSLog(@"select");
textField.text=(NSString*)[list objectAtIndex:indexPath.row];
//NSLog(@"textField.text=%@",textField.text);
[self setShowList:NO];
}
-(BOOL)showList{//setShowList:No为隐藏,setShowList:Yes为显示
return showList;
}
-(void)setShowList:(BOOL)b{
showList=b;
NSLog(@"showlist is set ");
if(showList){
self.frame=newFrame;
}else {
self.frame=oldFrame;
}
listView.hidden=!b;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code.
}
*/
- (void)dealloc {
[super dealloc];
}
@end
# ifndef __UIPREDICATECONTROL1_4F617DD144CA4E969A36095CBF6306D6_H_INCLUDED
# define __UIPREDICATECONTROL1_4F617DD144CA4E969A36095CBF6306D6_H_INCLUDED
WSI_BEGIN_HEADER_OBJC
@interface UIPredicateControl : WSIUIControl <UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>{
WSIUITextField *searchField;
WSIUIButton *contact;
WSIUITableView *listTable;
NSArray *dataArr;
NSArray *filterArr;
BOOL showTable;
BOOL isClick;
CGRect oldFrame, newFrame;
UIColor *lineColor, *listBgColor;
CGFloat lineWidth;
UITextBorderStyle borderStyle;
}
@property (nonatomic, retain) WSIUITextField *searchField;
@property (nonatomic, retain) WSIUIButton *contact;
@property (nonatomic, retain) WSIUITableView *listTable;
@property (nonatomic, retain) UIColor *lineColor, *listColor;
@property (nonatomic, assign) UITextBorderStyle borderStyle;
@property (nonatomic, assign) NSArray *dataArr;
@property (nonatomic, retain) NSArray *filterArr;
- (void)drawView;
- (void)setShowTable:(BOOL)show;
@end
WSI_END_HEADER_OBJC
#endif
# import "WSIFoundation.h"
# import "Layout.h"
# import "UIPredicateControl.h"
WSI_BEGIN_OBJC
@interface UIPredicateControl ()
- (void)__init;
@end
@implementation UIPredicateControl
@synthesize searchField, contact, listTable;
@synthesize dataArr, filterArr;
@synthesize lineColor, listColor,borderStyle;
- (id)init {
self = [super init];
[self __init];
return self;
}
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
[self __init];
return self;
}
- (void)dealloc {
[searchField release];
[contact release];
[listTable release];
[super dealloc];
}
- (void)__init {
CGRect rc = self.frame;
borderStyle = UITextBorderStyleRoundedRect;
showTable = NO;
isClick = NO;
oldFrame = rc;
newFrame = CGRectMake(rc.origin.x, rc.origin.y, rc.size.width, rc.size.height * 5);
lineColor = [UIColor lightGrayColor];
listBgColor = [UIColor whiteColor];
lineWidth = 1;
self.backgroundColor = [UIColor clearColor];
[self drawView];
}
-(void)drawView {
searchField = [[WSIUITextField alloc] initWithZero];
searchField.backgroundColor = [UIColor grayColor];
[searchField connect:kSignalValueChanged sel:@selector(act_value_change:) obj:self];
[self addSubview:searchField];
contact = [[WSIUIButton alloc] initWithType:UIButtonTypeRoundedRect];
contact.backgroundColor = [UIColor redColor];
[contact connect:kSignalButtonClicked sel:@selector(act_btn_click:) obj:self];
[self addSubview:contact];
listTable = [[WSIUITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
listTable.backgroundColor = [UIColor yellowColor];
listTable.dataSource = self;
listTable.delegate = self;
[self addSubview:listTable];
}
- (void)setShowTable:(BOOL)show {
showTable = show;
if (showTable) {
self.frame = newFrame;
} else {
self.frame = oldFrame;
}
listTable.hidden = !show;
}
- (BOOL)showTable {
return showTable;
}
- (void)act_value_change:(WSIEventObj *)evt {
NSString *text = (NSString *)evt.result;
trace_obj(text);
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", text];
self.filterArr = [self.dataArr filteredArrayUsingPredicate:predicate];
if ([self.filterArr count] != 0) {
if (showTable) {
[self.listTable reloadData];
} else {
[self.superview bringSubviewToFront:self];
[self setShowTable:YES];
}
} else {
[self setShowTable:NO];
}
}
- (void)act_btn_click:(WSIEventObj *)evt {
if (!isClick) {
if (showTable) {
[self.listTable reloadData];
} else {
[self.superview bringSubviewToFront:self];
[self setShowTable:YES];
}
} else {
[self setShowTable:NO];
}
isClick = !isClick;
}
#pragma mark listTable dataSource and delegate method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (self.filterArr.count == 0)
return 0;
return self.filterArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
WSIUITableViewCell *cell = [[WSIUITableViewCell alloc] init];
NSString *str;
if (self.filterArr.count == 0)
str = @" ";
str = (NSString *)[self.filterArr objectAtIndex:indexPath.row];
[cell storeSet:@"cell" obj:str];
cell.textLabel.text = str;
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return oldFrame.size.height;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.searchField.text = (NSString *)[self.filterArr objectAtIndex:indexPath.row];
[self setShowTable:NO];
}
- (void)layoutSubviews {
CGRect rc = self.bounds;
if (!showTable) {
wsi::CGRectLayoutHBox lyt(rc);
wsi::CGRectLayoutLinear lnrh(lyt);
lnrh.add_flex(4);
lnrh.add_flex(1);
self.searchField.frame = lyt.add_pixel(lnrh.start());
self.contact.frame = lyt.add_pixel(lnrh.next());
} else {
wsi::CGRectLayoutVBox lyt(rc);
wsi::CGRectLayoutLinear lnrv(lyt);
lnrv.add_flex(1);
lnrv.add_flex(4);
wsi::CGRectLayoutHBox lyth(lyt.add_pixel(lnrv.start()));
self.listTable.frame = lyt.add_pixel(lnrv.next());
wsi::CGRectLayoutLinear lnrh(lyth);
lnrh.add_flex(4);
lnrh.add_flex(1);
self.searchField.frame = lyth.add_pixel(lnrh.start());
self.contact.frame = lyth.add_pixel(lnrh.next());
}
}
@end
WSI_END_OBJC