简体   繁体   中英

Tap on cell of UITableView and the other view doesn't appear

I have a tableView with some results of a search. I have connected the first and the second view with the "storyboard Segue" from the cell to the second view.

This is the code written in the first ViewController:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    let indexPath = self.tableProva.indexPathForSelectedRow?.row

    let vc = segue.destination as! DettaglioNewViewController

    vc.titleLibro = posts[indexPath!].name
    vc.authorLibro = posts[indexPath!].author
    vc.ISBNLibro = posts[indexPath!].ISBN

}

In other project it works perfectly, but in this project it doesn't work, to show the second viewController I have to tap and swipe to the right or left.

It seems that the problem is graphic. If I try to click on the cell does not become gray. turns gray if I hold down long on it

Has this happened to anyone else?

Can anyone help me?

You should use the 'didSelectRowAtIndexPath' method in order to segue to a different view for a specific cell. Use.....

var storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle(forClass: self.dynamicType))

vc = storyboard.instantiateViewControllerWithIdentifier("LoginVC") as DettaglioNewViewController
vc.loadView()

..... To segue needed information.

* Information Example *

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
    myFavIndex = indexPath.row
    let storyBoard = UIStoryboard(name: "Main", bundle:nil)
    let view = storyBoard.instantiateViewControllerWithIdentifier("LoginVC") as! DettaglioNewViewController
    view.imageURL = self.imageArray[mySelectedIndex]
    view.titleString = self.titleArray[mySelectedIndex]
    self.presentViewController(view, animated: true, completion: nil)
}

In the above example I'm using the new View Controllers image and title as view.imageURL and view.titleString to populate that views elements from my selected Cells Information via [mySelectedIndex] . You should be able to do this with an Int , UIImage , String , eg

You don't need to initialize your controller from the code. Just create a custom segue and call 'performSegue' method in 'didSelect' method of table delegate.

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
      self.performSegueWithIdentifier("yourIdentifierInStoryboardForCustomSegue", sender: nil)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM