如何在 xcode 中创建 DropDown?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13467029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create DropDown in xcode?
提问by Adnan Khan
I am a very new developer for IOS, i need help, How to create dropdown box in xcode, any one provide me example to create country list in drop down?
我是一位非常新的 IOS 开发人员,我需要帮助,如何在 xcode 中创建下拉框,有人提供我在下拉列表中创建国家/地区列表的示例吗?
回答by Nitin Gohel
Here I found two demos for dropDown list, One is creating custom expandable UITableViewCell
like :-
在这里,我找到了两个下拉列表演示,一个是创建自定义扩展,UITableViewCell
如:-
to
到
source code :- DEMO
源代码:-演示
ANDAnother is custom Drop Down list like:-
和另一种是定制的下拉列表中,如: -
by clicking of test then open drop down list as bellow like image
通过点击测试然后打开下拉列表如下图
source code with Tab Bar:-DEMO
带有标签栏的源代码:- DEMO
updated source code without Tab Bar :-
没有标签栏的更新源代码:-
回答by MANIAK_dobrii
I beleive you shouldn't use dropdown boxes in iOS, as it's a desktop OS UI control element. You should think up something else using existing components (like PickerView), that's the words for UI consistency.
我相信你不应该在 iOS 中使用下拉框,因为它是一个桌面操作系统 UI 控制元素。您应该使用现有组件(如 PickerView)想出一些其他的东西,这就是 UI 一致性的话。
And if you need this anyway, you may create a table view, place it beneath your label and a triangular button(which causes it to appear and disappear) and populate it with values.
如果你无论如何都需要这个,你可以创建一个表格视图,将它放在你的标签和一个三角形按钮下面(这会导致它出现和消失)并用值填充它。
回答by tGilani
Since there are no native DropDown
elements in iOS, you could make use of a TextField
with custom background and a UITableView
to accomplish that. Here is how to go about it.
由于DropDown
iOS中没有本机元素,因此您可以使用TextField
具有自定义背景的 aUITableView
来完成此操作。这是如何去做的。
Pseudocode
伪代码
- Create a
TextField
and set it's delegate to parent controller - Implement
UITextFieldDelegate
and implement thetextFieldShouldBeginEditing
method - Create a new
UIViewController
and implementUITableView
in it programmatically. - Create a custom protocol and create it's object (delegate) it.
- In
textFieldShouldBeginEditing
method, load this controller and present it modally passing the required table's data source and set delegate as parent. - in the new
tableViewController
, implementUITableViewDelegate
and implement the didSelectRowAtIndex path method. - Upon row selection, call the delegate with passing appropriate data.
- dismiss the modally presented controller.
- 创建一个
TextField
并将其委托给父控制器 - 实现
UITextFieldDelegate
和实现textFieldShouldBeginEditing
方法 - 创建一个新的
UIViewController
并UITableView
以编程方式在其中实施。 - 创建一个自定义协议并创建它的对象(委托)它。
- 在
textFieldShouldBeginEditing
方法中,加载此控制器并以模态方式呈现它,传递所需表的数据源并将委托设置为父级。 - 在 new 中
tableViewController
,实现UITableViewDelegate
并实现 didSelectRowAtIndex 路径方法。 - 选择行后,通过传递适当的数据调用委托。
- 关闭模态呈现的控制器。
回答by IronManGill
The easy and simple way to design a drop down list is by representing it like a UITableView
and some animation. This makes it look really like a dropdownlist. Here is a code I used for creating one . For this first import the < QuartzCore/QuartzCore.h > framework.
设计下拉列表的简单方法是将其表示为一个UITableView
动画。这使它看起来很像一个下拉列表。这是我用来创建一个的代码。为此,首先导入 < QuartzCore/QuartzCore.h > 框架。
-(IBAction)DropDownTable:(id)sender
{
TableView.hidden = NO;
if(TableView.frame.origin.y ==203)
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5f];
[TableView setFrame:CGRectMake(224, 204, 27, 160)];
[UIView commitAnimations];
[self.view TableView];
}
else if (TableView.frame.origin.y == 204)
{
[TableView setFrame:CGRectMake(224, 203, 27, 0)];
TableView.hidden = YES;
}
[self.view addSubview:TableActivityLevel];
}
First make a tableview , declare its methods and make the array . Put this function on the click of a UIButton
and youll see it work !!! Happy coding :)
首先创建一个 tableview,声明它的方法并创建数组。把这个功能放在点击 a 上UIButton
,你会看到它起作用了!!!快乐编码:)
回答by djdance
just for whom searching for small simple swift combo boxhere in 2016 year, i've tried a few of old and new (but obj-c) libs, and at last selected this:
只是为了那些在 2016 年在这里寻找小型简单swift 组合框的人,我尝试了一些新旧(但 obj-c)库,最后选择了这个:
https://github.com/sw0906/SWCombox