如何在 iOS 中拥有 android 之类的微调器视图?
How to have an android like spinner view in iOS?
enter image description here 我的要求是向用户显示所有 selection 选项,以便他可以一次看到所有选项,然后能够 select 从那些就像它是通过 android 中的微调器视图完成的。目前我正在使用 UI 选择器视图。我附上它的屏幕截图。要么我想要一个微调器,要么希望选择器视图在当前显示三个的高度上同时显示 10 个项目。这是我的代码:
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
var myString = view as! UILabel!
var myStringLength = 0
if( view == nil) {
myString = UILabel()
}
myString?.textAlignment = .center
var rowText:String
if(pickerView==District){
rowText=districts[row]
}
else if(pickerView==block)
{
rowText=blocks[row]
}
else if(pickerView==management)
{
rowText=managements[row]
}
else
{
rowText=categories[row]
}
var attributedRowText = NSMutableAttributedString(string: rowText)
var attributedRowTextLength = attributedRowText.length
attributedRowText.addAttribute(NSForegroundColorAttributeName, value: UIColor.blue, range: NSRange(location: 0, length: attributedRowTextLength))
attributedRowText.addAttribute(NSFontAttributeName, value: UIFont(name: "Helvetica", size: 8.0)!, range: NSRange(location: 0 ,length:attributedRowTextLength))
myString!.attributedText = attributedRowText
return myString!
}
cocoatouch 框架中没有类似微调器的东西。
您最多可以做的是使用 UItableView
进行自定义实现
或者您也可以使用 NIDropdown 之类的东西,这将有助于您的实施。
您可以使用 table 视图作为下拉菜单:
subCategoryTable 是 table 名称。在按下该下拉按钮时创建此 table。这里的 subCatgBtn 是 btn on click 写这段代码。
subCategoryTable.frame = CGRectMake(self.subCatgBtn.frame.origin.x, self.subCatgBtn.frame.origin.y + self.subCatgBtn.frame.size.height, self.subCatgBtn.frame.size.width,CGFloat(numberofrows * 2));
subCategoryTable.delegate = self
subCategoryTable.dataSource = self
subCategoryTable.tag = 2
//subCategoryTable.rowHeight = self.subCatgBtn.frame.size.height
subCategoryTable.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")//this is needed when you use custom cell otherwise no need
subCategoryTable.separatorColor = UIColor.lightGrayColor()
subCategoryTable.allowsSelection = true
self.view.addSubview(subCategoryTable)
CellforrowIndex:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// var cell = tableView.dequeueReusableCellWithIdentifier("cell")!
if tableView.tag == 2
{
let cell = tableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell
//do whatever you want to show.
}
}
numberofrows 方法:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView.tag == 2
{
districts.count
}
}
在 Didselect 方法上,只需在您选择的任何按钮上设置名称即可。
我不知道你的 UI 你是如何设计的,你可以使用这个作为下拉菜单,当你点击那个按钮时它会动态创建。
enter image description here 我的要求是向用户显示所有 selection 选项,以便他可以一次看到所有选项,然后能够 select 从那些就像它是通过 android 中的微调器视图完成的。目前我正在使用 UI 选择器视图。我附上它的屏幕截图。要么我想要一个微调器,要么希望选择器视图在当前显示三个的高度上同时显示 10 个项目。这是我的代码:
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
var myString = view as! UILabel!
var myStringLength = 0
if( view == nil) {
myString = UILabel()
}
myString?.textAlignment = .center
var rowText:String
if(pickerView==District){
rowText=districts[row]
}
else if(pickerView==block)
{
rowText=blocks[row]
}
else if(pickerView==management)
{
rowText=managements[row]
}
else
{
rowText=categories[row]
}
var attributedRowText = NSMutableAttributedString(string: rowText)
var attributedRowTextLength = attributedRowText.length
attributedRowText.addAttribute(NSForegroundColorAttributeName, value: UIColor.blue, range: NSRange(location: 0, length: attributedRowTextLength))
attributedRowText.addAttribute(NSFontAttributeName, value: UIFont(name: "Helvetica", size: 8.0)!, range: NSRange(location: 0 ,length:attributedRowTextLength))
myString!.attributedText = attributedRowText
return myString!
}
cocoatouch 框架中没有类似微调器的东西。 您最多可以做的是使用 UItableView
进行自定义实现或者您也可以使用 NIDropdown 之类的东西,这将有助于您的实施。
您可以使用 table 视图作为下拉菜单:
subCategoryTable 是 table 名称。在按下该下拉按钮时创建此 table。这里的 subCatgBtn 是 btn on click 写这段代码。
subCategoryTable.frame = CGRectMake(self.subCatgBtn.frame.origin.x, self.subCatgBtn.frame.origin.y + self.subCatgBtn.frame.size.height, self.subCatgBtn.frame.size.width,CGFloat(numberofrows * 2));
subCategoryTable.delegate = self
subCategoryTable.dataSource = self
subCategoryTable.tag = 2
//subCategoryTable.rowHeight = self.subCatgBtn.frame.size.height
subCategoryTable.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")//this is needed when you use custom cell otherwise no need
subCategoryTable.separatorColor = UIColor.lightGrayColor()
subCategoryTable.allowsSelection = true
self.view.addSubview(subCategoryTable)
CellforrowIndex:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// var cell = tableView.dequeueReusableCellWithIdentifier("cell")!
if tableView.tag == 2
{
let cell = tableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell
//do whatever you want to show.
}
}
numberofrows 方法:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView.tag == 2
{
districts.count
}
}
在 Didselect 方法上,只需在您选择的任何按钮上设置名称即可。
我不知道你的 UI 你是如何设计的,你可以使用这个作为下拉菜单,当你点击那个按钮时它会动态创建。