您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 訊息 >> iOS 相關資訊

iPhone 下拉列表(DropDownList)元件實作教學

編輯:iOS 相關資訊

下拉列表是許多 App 常見的互動元件,特別是在表單或篩選场景中。以下透過一個自訂的 DropDownList 類別,示範如何以 UITextField 搭配 UITableView 實作出可展開、可收合的下拉選單。

首先在標頭檔中宣告元件所需的屬性與方法:

#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

接著是實作檔,重點在於初始化時計算展開後的尺寸、繪製元件,以及處理選取後回填文字並收合列表:

#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;             // 未展開時的初始大小
        // 展開時,高度放大為原本的 5 倍
        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];
}

- (void)dropdown {
    [textField resignFirstResponder];
    if (showList) {
        return; // 已展開則不做處理
    } else {
        // 置於最上層,避免被其他元件遮擋
        [self.superview bringSubviewToFront:self];
        [self setShowList:YES];
    }
}

#pragma mark - UITableView DataSource & Delegate
- (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];
    }
    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 *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    textField.text = (NSString *)[list objectAtIndex:indexPath.row];
    [self setShowList:NO];
}

- (BOOL)showList {
    return showList;
}

- (void)setShowList:(BOOL)b {
    showList = b;
    if (showList) {
        self.frame = newFrame;
    } else {
        self.frame = oldFrame;
    }
    listView.hidden = !b;
}
@end

整體思路是:點擊文字框時切換 showList 狀態,藉此調整元件 frame 並顯示或隱藏列表;使用者選取某一列後,將該列文字寫回文字框並自動收合。這是一個能直接複用於專案的下拉選單基礎範例。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved