简体   繁体   English

从UICollectionView中选择后,使用核心数据填充详细信息视图

[英]Use core data to populate detail view after selection from UICollectionView

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. 我有一个非常类似于这里发现的问题 ,只是我在Swift 2.0中编码(他们的问题/答案是Objective-C),我的情况略有不同。

I have a UICollectionView that is essentially a contact list that pulls from core data. 我有一个UICollectionView本质上是一个从核心数据中提取的联系人列表。 When the user selects a person (or an item within the UICollectionView), I want to present a detail view of the contact. 当用户选择一个人(或UICollectionView中的一个项目)时,我想显示联系人的详细视图。 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. 我已经在Storyboard中创建并连接了该视图/ segue,但是我无法将所选项目传递到详细信息视图ViewController,以便它知道要从核心数据中查询什么数据。

Here is a snippet of my code with descriptions on each: 这是我的代码片段,每个代码都有描述:

First, on my "FamilyCollectionViewController" I have the following viewDidLoad method: 首先,在我的“ FamilyCollectionViewController”上,我具有以下viewDidLoad方法:

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: 这是来自同一视图控制器的cellForItemAtIndexPath方法:

    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): 这是当前的didSelectItemAtIndexPath方法(这可能是我的问题所在,与prepareForSegue方法结合使用):

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. 我之前已经从其他视图传递了数据(使用prepareForSegue方法),但是现在它的细微差别来自UICollectionView等等,而使用核心数据则使我很困惑。 Any support is greatly appreciated. 非常感谢任何支持。

Here's a complete example. 这是一个完整的例子。

在此处输入图片说明

The contents of ViewController.swift 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 CellCollectionViewCell.swift的内容

class CellCollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var label: UILabel!
}

The contents of OtherViewController.swift OtherViewController.swift的内容

class OtherViewController: UIViewController {

    var selectedIndex: Int!

    override func viewDidLoad() {
        super.viewDidLoad()

        title = String(selectedIndex)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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