Reputation: 173
I have a question that is very similar to the one found here, only I'm coding in Swift 2.0 (their question/answer is Objective-C), and my case is slightly different.
I have a UICollectionView that is essentially a contact list that pulls from core data. When the user selects a person (or an item within the UICollectionView), I want to present a detail view of the contact. I have that view/segue created and hooked up within the Storyboard, but I'm having trouble passing the selected item to the detail view ViewController so that it knows what data to query from core data.
Here is a snippet of my code with descriptions on each:
First, on my "FamilyCollectionViewController" I have the following viewDidLoad method:
override func viewDidLoad() {
super.viewDidLoad()
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context:NSManagedObjectContext = appDelegate.managedObjectContext
let fetchRequest = NSFetchRequest(entityName: "Family")
fetchRequest.returnsObjectsAsFaults = false
do {
let results = try context.executeFetchRequest(fetchRequest)
userNames = results as! [NSManagedObject]
} catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}
}
Here is the cellForItemAtIndexPath method from the same view controller:
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! FamilyCollectionViewCell
let person = userNames[indexPath.row]
cell.familyName!.text = person.valueForKey("name") as? String
print(userNames)
return cell
}
And here is the current didSelectItemAtIndexPath method (this may be where my problem is at, in combination with the prepareForSegue method):
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let selectedPerson = userNames[indexPath.row]
print(selectedPerson)
let selectedName = selectedPerson.valueForKey("name") as? String
let selectedNumber = selectedPerson.valueForKey("phone") as? String
let selectedEmail = selectedPerson.valueForKey("email") as? String
print(selectedName)
}
I attempted to create something similar to the answer as provided in the aforementioned linked question, but it is so laden with errors its not useful at all (the way I created it that is). I've passed data before from other views (using the prepareForSegue method), but the nuance of it now being from a UICollectionView and more, using core data, I'm left stumped. Any support is greatly appreciated.
Upvotes: 0
Views: 964
Reputation: 12053
Here's a complete example.
The contents of ViewController.swift
class ViewController: UICollectionViewController
{
var selectedIndex: Int!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 100
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("ReuseID", forIndexPath: indexPath) as! CellCollectionViewCell
cell.contentView.backgroundColor = UIColor.whiteColor()
cell.label.text = "\(indexPath.row)"
return cell
}
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
selectedIndex = indexPath.item
performSegueWithIdentifier("OtherSegueID", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "OtherSegueID" {
let otherViewController = segue.destinationViewController as! OtherViewController
otherViewController.selectedIndex = selectedIndex
}
}
}
The contents of CellCollectionViewCell.swift
class CellCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var label: UILabel!
}
The contents of OtherViewController.swift
class OtherViewController: UIViewController {
var selectedIndex: Int!
override func viewDidLoad() {
super.viewDidLoad()
title = String(selectedIndex)
}
}
Upvotes: 2