因为最近有需要用到下拉列表的地方,所以上网查了一些资料,其中向这两位博主学习了很多:
https://blog.csdn.net/qq_40596811/article/details/81559548
https://blog.csdn.net/u012960049/article/details/50962647
在此向他们表示感谢~~
首先展示一下效果
-
最开始只显示button
-
点击后出现下拉选择列表
-
选择之后,button中会显示相应的内容,同时下拉列表收起
程序
//
// ViewController.m
// 下拉列表的实现20190108
//
// Created by lilongyu on 2019/1/8.
// Copyright © 2019 lilongyu. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
BOOL showList;//是否弹出下拉列表
}
@property(nonatomic,retain) UITableView *tableView;
@property(nonatomic,copy) NSArray *dataArray;
@property(nonatomic,strong) UIButton *button;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_dataArray=@[@"奇迹男孩",@"珍珠港",@"绿里奇迹"];//下拉列表的数据
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(100, 140, 130, 120) style:UITableViewStylePlain];
showList = NO;//默认不弹出下拉框
_tableView.delegate=self;
_tableView.dataSource=self;
_tableView.hidden = YES;
[_tableView setSeparatorColor:[UIColor lightGrayColor]];
[self.view addSubview:_tableView];
//用button控制列表的出现和隐藏,以及显示选择结果
_button=[[UIButton alloc] initWithFrame:CGRectMake(100, 100, 130, 40)];
[_button setTitle:@"" forState:UIControlStateNormal];
[_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_button.layer.borderWidth=1;
_button.layer.borderColor = [UIColor redColor].CGColor;
_button.backgroundColor=[UIColor blueColor];
_button.titleLabel.font=[UIFont systemFontOfSize:20];
[_button addTarget:self action:@selector(dropDown) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_button];
}
//出现按钮点击时调用的方法
-(void)dropDown{
if(showList){//此时列表已经显示,再次点击则让列表收起
showList=NO;
_tableView.hidden=YES;
return;
}else{//显示列表
[self.view bringSubviewToFront:_tableView];
_tableView.hidden=NO;
showList=YES;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 40;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_dataArray count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *string=[[NSString alloc] init];
string=[_dataArray objectAtIndex:[indexPath row]];
[_button setTitle:string forState:UIControlStateNormal];
tableView.hidden=YES;
showList=NO;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"Cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
cell.backgroundColor = [UIColor greenColor];
cell.textLabel.font = [UIFont systemFontOfSize:20];
cell.textLabel.text=[_dataArray objectAtIndex:[indexPath row]];
cell.textLabel.textColor=[UIColor whiteColor];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
//系统的tableview的分割线设置,使其充满
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[tableView setLayoutMargins:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
}
@end
过程中遇到的问题
-
控件的选择
我最开始选择的是UITextField控件,想要用
[_textField addTarget:self action:@selector(dropDown) forControlEvents:UIControlEventAllTouchEvents] 方法来检测它的点击,但是总是出现时有效时无效的情况,但是我没有找到原因,期待大家的大衣解惑,感激不尽; -
button中的文字显示
刚开始,我采用的是对_button.titleLabel.text赋值的方法进行button文本的传递,但是在我讲文字颜色,大小等都设置完成的时候,button中没有任何文字的显示。上网查资料,有说是因为_button.titleLabel的frame默认为(0,0,0,0),以及默认_button.titleLabel.hidden=YES;我对这两者进行设置之后结果也是不能显示。
但是我不确定是不是自己的设置有误,决定之后再进行验证。
以上便是我今天的小进步了,fighting~~~