在下拉项目选择中获取TableViewCell索引为nil

问题描述 投票:0回答:1

我想在下拉项目选择上使用tableviewcell索引。但是当我点击下拉项时,索引是零。有没有办法获得下拉项目选择的索引?如果有人有任何更好的解决方案给我一些想法。

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

这是我用于DropDown列表的函数。

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")
            }
        }
    }
ios swift indexing tableview dropdown
1个回答
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中

© www.soinside.com 2019 - 2024. All rights reserved.