获取TableViewCell索引与下拉菜单项选择无关

时间:2019-02-26 06:03:44

标签: ios swift indexing tableview dropdown

我要选择下拉列表项的tableviewcell索引。但是当我单击下拉项时,索引为零。有什么方法可以获取下拉菜单项的索引?如果有人有更好的解决方案,请给我一些想法。

let EditDropDown = DropDown()
lazy var dropDowns: [DropDown] = {
    return [
        self.EditDropDown
    ]
}()

这是我用于下拉列表的功能。

func setupGenderDropDown() {

        let cellHeader = tableview.dequeueReusableCell(withIdentifier: "CellRIDHeader") as! SPOccupationCell

        EditDropDown.anchorView = cellHeader.btnDots
        EditDropDown.bottomOffset = CGPoint(x: 0, y: 40)

        // You can also use localizationKeysDataSource instead. Check the docs.
        EditDropDown.dataSource = [
            "Edit",
            "Make Default",
            "Delete"
        ]

        // Action triggered on selection
        EditDropDown.selectionAction = { [weak self] (index, item) in
            cellHeader.btnDots.setTitle(item, for: .normal)

            if item == "Edit"
            {
                // I am Getting Cell Index but index is nil
                let cell = self!.tableview.dequeueReusableCell(withIdentifier: "CellRIDHeader") as! SPOccupationCell

                let indexPath = self!.tableview.indexPath(for: cell)
                    print(indexPath as Any)

                let occupation_id = self!.arrayOccupation[(indexPath?.row)!].occupation_Main_id
                    print(occupation_id)

                    let next = self!.storyboard?.instantiateViewController(withIdentifier: "EditOccupationVCSID") as! EditOccupationVC
                self!.navigationController?.pushViewController(next, animated: false)
                    next.occupationId = occupation_id



            }
            else if item == "Make Default"
            {
                print("B")
            }
            else if item == "Delete"
            {
                print("c")
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

我假设您正在使用DropDown库来显示下拉列表。有一个问题,当您点击它时会得到单元格,因此我为您创建了一个演示项目(简单的tableView而不是自定义UITableViewCell),并添加了注释以解释更改。考虑下面的代码:

import UIKit
import DropDown

class ViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!

    var items: [String] = ["We", "Heart", "Swift"]

    let editDropDown = DropDown() //Object name should start with small letter

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
    }

    func setupGenderDropDown(cell: UITableViewCell) { //Pass your cell with argument and change type to your custom cell

        //By changing cell argument with your custom cell you will get your button for anchor
        editDropDown.anchorView = cell.textLabel
        editDropDown.bottomOffset = CGPoint(x: 0, y: 40)

        editDropDown.dataSource = [
            "Edit",
            "Make Default",
            "Delete"
        ]

        //Here you need to update selectionAction from their library page
        editDropDown.selectionAction = { [unowned self] (index: Int, item: String) in

            //Here you will get selected item and index
            print("Selected item: \(item) at index: \(index)")
            if item == "Edit"
            {
                print(item)
                print(index)
            }
            else if item == "Make Default"
            {
                print("B")
            }
            else if item == "Delete"
            {
                print("c")
            }
        }

        //This was missing in your code
        editDropDown.show()
    }
}

extension ViewController: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.items.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell") as! UITableViewCell
        cell.textLabel?.text = self.items[indexPath.row]
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        //Get the selected cell this way
        guard let indexPath = tableView.indexPathForSelectedRow else { return }
        guard let currentCell = tableView.cellForRow(at: indexPath) else { return }

        //Pass your selected cell to setupGenderDropDown method
        setupGenderDropDown(cell: currentCell)
    }
}

HERE,您可以检查演示项目。并将其创建到Xcode 10.1