下拉選單在表單類需求裡很常見。這裡示範一個把 UITextField 與 UITableView 組合成 DropDownList 的做法,點一下輸入框就展開清單,選完自動收起並回填文字。
元件需要記住幾件事:文字框、清單資料、目前是否展開、清單視圖,以及展開前後的 frame(展開後高度變成五倍,用來放四列選項)。另外把邊框色、背景色、線寬、文字框邊框樣式都開成可設定的屬性。
#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;
}
@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
初始化時先給一組預設資料,記錄展開前的大小 oldFrame,並推算展開後的 newFrame(高度乘五)。背景必須設成 clearColor,否則外框會出現一條黑邊。
- (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;
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;
[self addSubview:listView];
[listView release];
}
點擊文字框時先收起鍵盤;若清單已展開就什麼都不做,否則把自己移到最上層(避免被兄弟視圖遮住)再展開。
- (void)dropdown {
[textField resignFirstResponder];
if (showList) return;
[self.superview bringSubviewToFront:self];
[self setShowList:YES];
}
- (void)setShowList:(BOOL)b {
showList = b;
self.frame = b ? newFrame : oldFrame;
listView.hidden = !b;
}
資料源部分很直觀:列數取 list.count,高度沿用 oldFrame 的高度,重用 cell 時把字型對齊文字框。選中某列後,把文字寫回 textField 並收起清單。
- (NSInteger)tableView:(UITableView *)tableView
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;
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
textField.text = (NSString *)[list objectAtIndex:indexPath.row];
[self setShowList:NO];
}
這裡有個實務提醒:清單展開時元件高度會變,若父視圖沒有預留空間,可能會壓到下面的其他控制項。真實專案裡通常會改用 popover 或浮層呈現,或是以 popup 的方式覆蓋在其他內容之上,比撐開自身高度更好維護。