UICollectionview单元格中的下拉列表

我正在开发一个TODOList iOS应用程序但是有一个问题,即如何添加下拉列表是uicollectionview单元格。 意味着当视图加载了加载集合视图时,每个单元格中都应该有一个下拉列表

iOS中没有任何内置的下拉function,但您可以使用UITableView或使用第三方库来实现。

我建议你试试这个。 落下

全局定义。

 let dropDown = DropDown() 

如果要自定义dropDown,可以使用它。

 func customizeDropDown() { DropDown.appearance().cellHeight = 40 DropDown.appearance().backgroundColor = UIColor.white DropDown.appearance().selectionBackgroundColor = Colors.purpleColor DropDown.appearance().cornerRadius = 5 DropDown.appearance().textColor = Colors.NavTitleColor DropDown.appearance().shadowColor = (UIColor.init(hexString: "1AD691")?.withAlphaComponent(0.0))! DropDown.appearance().shadowOpacity = 0.9 DropDown.appearance().shadowRadius = 0 DropDown.appearance().animationduration = 0.25 } 

cellForItemAt你需要像下面这样在你的下拉按钮上添加动作。

 cell.btnDropdown.tag = indexPath.item cell.btnDropdown.addTarget(self, action: #selector(btnDropDownTapped), for: .touchUpInside) 

一旦你从下面的UICollectionViewCell点击任何按钮,方法将调用你需要传递anchorView

 @IBAction func btnDropDownTapped(_ sender: UIButton) { self.dropDown.anchorView = sender // The view to which the drop down will appear on self.dropDown.bottomOffset = CGPoint(x: 0, y: sender.bounds.height) //Top of drop down will be below the anchorView self.dropDown.dataSource = ["First", "Last", "Second", "Third"] // Static array you need to change as per your requirement self.dropDown.selectionAction = { [unowned self] (index, item) in print(item) // **NOTE: I AM JUST PRINTING DROPDOWN SELECTED VALUE HERE, YOU NEED TO GET `UICollectionViewCell` HERE YOU NEED TO SET VALUE INSIDE CELL LABEL OR YOU CAN SET SELECTED DROPDOWN VALUE IN YOUR MODEL AND RELOAD COLLECTIONVIEW** self.collectionView.reloadData() } self.dropDown.show() } 

如果您在UICollectionViewCellUITextField ,那么您可以在textFieldShouldBeginEditing委托中尝试此代码。

注意:我只是在这里打印UICollectionViewCell值,你需要获取UICollectionViewCell你需要在细胞标签内设置值,或者你可以在你的模型和RELOAD COLLECTIONVIEW中设置选择的下降值

iOS没有任何本机控制下拉菜单。您可以使用拣货员代替。 如下所示:

在此处输入图像描述 这是添加pickerView的代码。

 let picker: UIPickerView picker = UIPickerView(frame: CGRectMake(0, 200, view.frame.width, 300)) picker.backgroundColor = .whiteColor() picker.showsSelectionIndicator = true picker.delegate = self picker.dataSource = self let toolBar = UIToolbar() toolBar.barStyle = UIBarStyle.Default toolBar.translucent = true toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1) toolBar.sizeToFit() let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "donePicker") let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil) let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "donePicker") toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false) toolBar.userInteractionEnabled = true textField1.inputView = picker textField1.inputAccessoryView = toolBar 

github上有很多库添加了很酷的选择器控件: Actionsheet PickerView Selectionmenu

请按照步骤执行操作

  1. 在UICollectionViewCell中添加UITableView。

  2. 使UITableView的隐藏属性为true

    collectionViewCell.dropDownTableView.isHidden =真

  3. 当用户选择显示下拉列表的选项时,重新加载UITableView数据并将UITableView的隐藏属性设置为false。

如何将UITableView添加到UICollectionViewCell