UICollectionview单元格中的下拉列表 [英] Dropdown list in UICollectionview cell

查看:178
本文介绍了UICollectionview单元格中的下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was developing a TODOList iOS application but there is a problem i.e. how can I add dropdown list is uicollectionview cell. Mean when view did load collection view loaded there should be a dropdown in every single cell

推荐答案

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

There's no any inbuilt dropdown functionality in iOS but you can do it by using UITableView or by using 3rd party library.

我建议你尝试这个。 DropDown

I suggest you to try this. DropDown

全局定义。

let dropDown = DropDown()

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

If you want to customise dropDown you can use this.

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 中,您需要在下拉按钮上添加操作,如下所示。

In cellForItemAt you need to add action on your dropdown button like this.

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

点击中的任何按钮后UICollectionViewCell 下面的方法将调用你需要传递的地方 anchorView

Once you tapped on any button from UICollectionViewCell below method will call where you need to pass 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()
}

如果您的 UICollectionViewCell 中有 UITextField ,那么您可以在 textFieldShouldBeginEditing中尝试此代码委托。

If you have UITextField in your UICollectionViewCell then you can try this code inside textFieldShouldBeginEditing delegate.

注意:我只是在这里打印掉落选择值,你需要获得 UICollectionViewCell 此处您需要在细胞标签内设置价值,或者您可以在您的模型中设置选择的下降值并重新收集COLLECTIONVIEW

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

这篇关于UICollectionview单元格中的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆