无法从TableViewCell检索数据,因为未调用didSelectRowAt

时间:2018-09-09 01:46:33

标签: ios swift uitableview

我有一个自定义UITableView,它包含每个要使用UserDefaults检索并保存的单元格中的数据。

我希望在用户点击某个单元格时调用didSelectRowAt,以便我可以检索该特定单元格中的数据。

问题是didSelectRowAt没有被调用,我尝试了以下方法:

  1. 确保没有手势识别器“吃”单元格上的水龙头(我从未添加手势识别器)。
  2. 将Identity Inspector的“选择”部分设置为“无”和“单选”。

下面是如何设置带有TableView的ViewController的屏幕截图:

Screenshot of Storyboard setup

这是我的代码:

class blueSide: UIViewController, UITableViewDelegate, UITableViewDataSource {
    var items : [SosItem] = []
    @IBOutlet var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.dataSource = self
        tableView.delegate = self

        ref.observe(.value, with: {
            snapshot in
            var newItems : [SosItem] = []
            for child in snapshot.children {
                if let snapshot = child as? DataSnapshot,
                    let sosItem = SosItem(snapshot: snapshot) {
                    newItems.append(sosItem)
                }
            }
            self.items = newItems
            print(self.items)
            self.tableView.reloadData()
        })
    }

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            let removedItem = items.remove(at: indexPath.row)

            let itemsRef = ref.child(removedItem.key.lowercased())
            itemsRef.removeValue()
            tableView.reloadData()
        }
    }

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 150
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let sosItem = items[indexPath.row]
        print(sosItem)
        UserDefaults.standard.set(sosItem.clothingDescription, forKey: "clothingDescription")
        UserDefaults.standard.set(sosItem.placeName, forKey: "placeName")
        UserDefaults.standard.set(sosItem.longitude, forKey: "longitude")
        print("Longitude saved!")
        UserDefaults.standard.set(sosItem.latitude, forKey: "latitude")
        print("Latitude saved!")

        print(UserDefaults.standard.value(forKey: "latitude"))
        // tableView.deleteRows(at: [indexPath], with: .fade)
        //  tableView.reloadData()
        self.performSegue(withIdentifier: "uberSegue", sender: self)
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath) as! CustomTableViewCell

        //get cell data from Firebase
        let sosItem = items[indexPath.row]
cell.descriptionLabel.text = sosItem.clothingDescription
        cell.latitudeLabel.text = String(sosItem.latitude)
        cell.longitudeLabel.text = String(sosItem.longitude)
        cell.locationNameLabel.text = sosItem.placeName
        cell.destinationLabel.text = sosItem.dropoffLocation

        return cell
    }

1 个答案:

答案 0 :(得分:0)

当tableView处于编辑模式,isEditing属性设置为true或调用canEditRowAt时,不会调用didSelectedRowAt方法 尝试在编辑模式结束时选择一行,作为测试!