UITableView Not Using Cell as Sender - ios

I'm trying to segue and pass data (from UITableView to a UIViewController). My code is
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "IndividualAchievementsSegue" {
let destination = segue.destinationViewController as? IndividualAchievementsViewController
let cell = sender as! UITableViewCell
let selectedRow = AchievementsTable.indexPathForCell(cell)!.row
destination!.viaSegue = achievements[selectedRow]
print(selectedRow)
}
}
but the line let cell = sender as! UITableViewCell is throwing
Could not cast value of type 'ProgramName.AchievementsViewController' to 'UITableViewCell'
I have also tried
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "IndividualAchievementsSegue" {
let destination = segue.destinationViewController as? IndividualAchievementsViewController
let selectedRow = AchievementsTable.indexPathForSelectedRow!.row
destination!.viaSegue = achievements[selectedRow]
print(selectedRow)
}
}
That fails because it finds nil unwrapping the optional during achievements[selectedRow] since that apparently gives me a nil from let selectedRow = AchievementsTable.indexPathForSelectedRow!.row

I think the problem is that your segue is connected with your AchievementsViewController to IndividualAchievementsViewController instead of UITableViewCell to IndividualAchievementsViewController. Check in the storyboard for that and correct the segue with TableCell to IndividualAchievementsViewController.

So the solution was to go into DidSelectrowAtIndexPath and remove DeselectRowAtIndexPath, which was in there twice by accident. Then use the following
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "IndividualAchievementsSegue" {
let destination = segue.destinationViewController as? IndividualAchievementsViewController
let selectedRow = AchievementsTable.indexPathForSelectedRow?.row
destination!.viaSegue = achievements[selectedRow!]
print(selectedRow)
}
}

Related

Push segue from Cell doesn't work after click

I have been reading around on various ways to perform segues. I want a push segue from a cell click. I have the segue in the storyboard, so I am not using the didSelectForRow function:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let selectedRow = self.tableView.indexPathForSelectedRow as? Int else { return }
if segue.identifier == "detail", let vc = segue.destination as? DetailViewController {
vc.playerImage = UIImageView(image: UIImage(named: "userIcon"))
vc.currentRankingLabel.text = String(players[selectedRow].ranking)
vc.scoreLabel.text = String("\(players[selectedRow].wins) - \(players[selectedRow].losses)")
}
}
This is what I have got at the moment. It compiles, but the cell will not react to the click!
indexPathForSelectedRow is never Int, it's IndexPath?. Delete the conditional downcast and get the row from its row property
guard let selectedPath = self.tableView.indexPathForSelectedRow else { return }
let selectedRow = selectedPath.row
if segue.identifier == "detail", let vc = segue.destination as? DetailViewController {
vc.playerImage = UIImageView(image: UIImage(named: "userIcon"))
vc.currentRankingLabel.text = String(players[selectedRow].ranking)
vc.scoreLabel.text = String("\(players[selectedRow].wins) - \(players[selectedRow].losses)")
}
However if the segue is connected to the cell I recommend to use the sender parameter which represents the cell
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "detail", let vc = segue.destination as? DetailViewController {
let selectedPath = self.tableView.indexPath(for: sender as! UITableViewCell)!
let selectedRow = selectedPath.row
vc.playerImage = UIImageView(image: UIImage(named: "userIcon"))
vc.currentRankingLabel.text = String(players[selectedRow].ranking)
vc.scoreLabel.text = String("\(players[selectedRow].wins) - \(players[selectedRow].losses)")
}
}
Important note:
Be aware that the outlets in the destination controller are not connected in prepare(for so nothing will be displayed or the code even crashes.
I think you need call performSegue on cell selection, like below
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "detail", sender: indexPath)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let selectedPath = sender as? IndexPath, let selectedRow = selectedPath.row, let player = players[selectedRow] else {
return
}
if segue.identifier == "detail", let vc = segue.destination as? DetailViewController {
vc.playerImage = UIImageView(image: UIImage(named: "userIcon"))
vc.currentRankingLabel.text = String(player.ranking)
vc.scoreLabel.text = String("\(player.wins) - \(player.losses)")
}
}

apple swift slideoutMenu indexPath

I am creating a slide menu for ios but I'm having problems
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let DestVC = segue.destinationViewController as ViewController
let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow()!
DestVC.varView = indexPath.row
}
why doesn't this code work?
You need to make optional cast cause segue.destinationViewController is UIViewController that can't be always casted to its subclass
Try this
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
guard let DestVC = segue.destinationViewController as? ViewController else { return }
let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow()!
DestVC.varView = indexPath.row
}

prepareForSegue UICollectionView not working (swift)

I’ve tried to push segue by using UICollectionView (show image form MasterView to DeatilView) but is not working. Xcode also doesn’t show any error message. What’s wrong with my code. I need some advice.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
if let indexPath = self.collectionView?.indexPathForCell(sender as! UICollectionViewCell) {
let detailVC = segue.destinationViewController as! DetailMenuViewController
detailVC.picFood = self.collection[indexPath.row]
collection is empty array get data form json
[title = self.nameFood.title], it is working in DetailMenuViewController.swift but not image on code above.
You need to add the UICollecitonViewDelegate
func collectionView(collection: UICollectionView, selectedItemIndex: NSIndexPath)
{
self.performSegueWithIdentifier("showDetail", sender: self)
}
After that add
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
if let indexPath = self.collectionView?.indexPathForCell(sender as! UICollectionViewCell) {
let detailVC = segue.destinationViewController as! DetailMenuViewController
detailVC.picFood = self.collection[indexPath.row]
}
}
}

Swift Segue not working

I have a school project to make a swift app. I'm very new to swift and trying to pass data through the segue from a tableview to another view.
Here is the project file Download Here.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "PopularDetails" {
let detailsViewController = segue.destinationViewController as DetailsViewController
let selectedIndexPath = popularTableView.indexPathForSelectedRow()
detailsViewController.shots = shots[selectedIndexPath.row]
}
}
}
.indexPathForSelectedRow() returns an optional. You have to unwrap it, before you can access its properties. Try:
let selectedIndexPath = popularTableView.indexPathForSelectedRow()
detailsViewController.shots = shots[selectedIndexPath!.row]
(see the exclamationmark)
Although #gutenmorgenuhu is correct, it's probably better practice to use:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "PopularDetails" {
if let detailsViewController = segue.destinationViewController as? DetailsViewController {
if let selectedIndexPath = popularTableView.indexPathForSelectedRow() {
detailsViewController.shots = shots[selectedIndexPath.row]
}
}
}
}

Swift pass data between VCs

i'm new to Swift and i would like to know why this piece of code i translated from Obj-C doesn't work here.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "scoreDetail" {
let indexPath = self.tableView.indexPathForSelectedRow()
let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject
let itemVC: ScoreVC = segue.destinationViewController as ScoreVC
itemVC.detailItem = object
}
}
Here is the storyboard. I have a Show Replace segue from the cell to the UINavigatorController
I get this error as soon as i click on a UITableViewCell
The problem has nothing to do with Swift. The destinationViewController is the navigation controller, not your ScoreVC, so the code you translated that from shouldn't have worked either (if it had the same setup). It should look like this,
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "scoreDetail" {
let indexPath = self.tableView.indexPathForSelectedRow()
let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject
let nav : UINavigationController = segue.destinationViewController
let itemVC: ScoreVC = nav.topViewController as ScoreVC
itemVC.detailItem = object
}
}

Resources