ios - Swift 4 UITableViewCell 第一次点击不调用 segue,第二次点击使用第一个单元格数据调用 segue

标签 ios swift uitableview

我在使用 XIB 文件的单元格的 TableView 上遇到问题,当我第一次点击一个单元格时,它什么都不做,当我点击另一个单元格时,会调用 segue,但使用的数据是第一个细胞被窃听。我在“didDeselectRowAt”tableView 函数上使用“self.performSegue”。这是我的代码:

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
     _ = tableView.dequeueReusableCell(withIdentifier: "CardCell") as! CardTableViewCell?
     self.eachCard = cardsArray[indexPath.row]

     self.performSegue(withIdentifier: "showCard", sender: tableView)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let card = segue.destination as? SingleShotViewController
    if segue.identifier == "showCard" {
        card?.name = eachCard["name"] as! String
        card?.type = eachCard["type"] as! String
        if eachCard["imageUrl"] == nil {
            card?.imgURL = "http://gatherer.wizards.com/Handlers/Image.ashx?multiverseid=264373&type=card"
        } else {
        card?.imgURL = eachCard["imageUrl"] as! String
        }
    }
}

最佳答案

使用这个重载代替 didDeselectRowAt

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        _ = tableView.dequeueReusableCell(withIdentifier: "CardCell") as! CardTableViewCell?
     self.eachCard = cardsArray[indexPath.row]

     self.performSegue(withIdentifier: "showCard", sender: tableView)
    }

关于ios - Swift 4 UITableViewCell 第一次点击不调用 segue,第二次点击使用第一个单元格数据调用 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51792132/

相关文章:

ios - iOS 延迟初始化

ios - 为什么 Swift 不支持异常?

ios - 使用 Swift 3.0 将自定义字体添加到 SpriteKit

ios- ScrollView 中的所有页面未加载自定义uiview

ios - 如何在 UIViewController 中设置 tableView 单元格标识符?

ios - Swift 中 ScrollView 的问题

ios - 使用其他iOS App登录(类似于使用Facebook登录)

ios - UILabel向下滑动发送错误信息

iOS 8 自动布局 : when can I get the width of a table view cell?

ios - 有什么方法可以从自定义 UITableViewCell 类更改 UITableView 高度?