Swift 4 UITableViewCell first tap doesn't call segue, and second tap calls segue with the first cell data

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Swift 4 UITableViewCell first tap doesn't call segue, and second tap calls segue with the first cell data



I'm having a problem with a TableView with cells using a XIB file, when I first tap on a cell, it does nothing, and when I tap on another cell, the segue is called, but with the data of the first cell tapped. I'm using "self.performSegue" on the "didDeselectRowAt" tableView Function. Here's my code:


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






3 Answers
3



Use this overload instead of 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)



When you select a cell in a tableview, first the didSelectRow function is called. When you tap it one more time, the didDeselectRow function is called.
Just change the function to didSelectRow and it will work fine.





but the function is the didSelectRow, I don't use the didDeselectRow on this code.
– Douugr
Aug 10 at 18:30





@Douugr The code you posted is didDeselectRow :)
– Pouya Kahfi
Aug 10 at 18:33





@Douugr Can you verify if you used didSelectRowAt or didDeselectRowAt?
– davetw12
Aug 10 at 18:38



didSelectRowAt


didDeselectRowAt



Look carefully you will find yourself using "didDeselectRowAt indexPath" which why in the second touch you preform the segue and delegate is being called so instead of that just change "didDeselectRowAt indexPath" to "didSelectRow indexPath" and it will work fine






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered