Im trying to get the VC
that corresponds with the cell of the table view to load when tapped. I am trying to set the indexPath
as the selectedIndexPath
and set up the segue in the collection view, then use that in the tableView but it is throwing an error. Here is the code.
import UIKit
private let reuseIdentifier = "profileCell"
class CompanyCollectionViewController: UICollectionViewController {
//MARK: - View Did Load
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)
}
// MARK: UICollectionViewDataSource
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return stuff.company.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! ProfileCollectionViewCell
cell.profileImageView.image = UIImage(named: stuff.company[indexPath.row]["image"]!)
return cell
}
//MARK: - Prepare For Segue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "profileSegue" {
let profileVC = segue.destinationViewController as!
MyProfileTableViewController
let cell = sender as! UICollectionViewCell
let indexPath = collectionView?.indexPathForCell(cell)
profileVC.selectedIndexPath = indexPath
}
}
}
and then
import UIKit
class MyProfileTableViewController: UITableViewController {
//MARK: - View Did Load
override func viewDidLoad() {
super.viewDidLoad()
}
//MARK: - Table View Data Source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if let indexPath = selectedIndexPath {
let row = stuff.company[indexPath.row]
let cell = tableView.dequeueReusableCellWithIdentifier("myProfileCell", forIndexPath: indexPath) as! MyProfileTableViewCell
cell.heroImageView.image = UIImage(named: row["image"]!)
title = row["name"]
cell.nameLabel.text = row["name"]
cell.statusLabel.text = row["description"]
return cell
} else {
let cell = tableView.dequeueReusableCellWithIdentifier("myProfileCell", forIndexPath: indexPath) as! MyProfileTableViewCell
cell.heroImageView.image = UIImage(named: stuff.profile["profile image"]!)
title = stuff.profile["name"]
cell.nameLabel.text = stuff.profile["name"]
cell.statusLabel.text = stuff.profile["status"]
return cell
}
}
//MARK: - Variables
var selectedIndexPath: NSIndexPath?
}
Ah I see, you are not checking if your collectionView?.indexPathForCell is returning a valid result.
if let cell = sender as? UICollectionViewCell {
// Cell is valid
if let indexPath = collectionView?.indexPathForCell(cell) {
// indexPath is valid
} else {
// indexPath is invalid
}
} else {
// cell is invalid
}
I suspect your indexPath from your table and your indexPath from your selection view are being mixed up, the code posted above should help you debug that.
Beyond that, I believe you can accomplish your intended result by:
1) Saving your selectedIndexPath in the didSelectCellAtIndexPath: method
2) Reading the selectedIndexPath in prepareForSegue instead of deriving the indexPath from the segue sender
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.