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

时间:2016-02-21 19:59:09

标签: ios iphone swift core-data uicollectionview

我的问题与here非常相似,只有我在Swift 2.0中编码(他们的问题/答案是Objective-C),我的情况略有不同。< / p>

我有一个UICollectionView,它本质上是一个从核心数据中提取的联系人列表。当用户选择一个人(或UICollectionView中的项目)时,我想呈现联系人的详细视图。我在Storyboard中创建并连接了该视图/ segue,但是我无法将所选项目传递给详细视图ViewController,以便它知道从核心数据中查询哪些数据。

以下是我的代码片段,其中包含每个代码的说明:

首先,在我的&#34; FamilyCollectionViewController&#34;我有以下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)")
    }
}

以下是来自同一视图控制器的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
}

这是当前的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)

}

我尝试创建与上述链接问题中提供的答案类似的内容,但它充满了错误,根本没有用(我创建它的方式)。我之前从其他视图传递数据(使用prepareForSegue方法),但现在来自UICollectionView的细微差别以及更多,使用核心数据,我很难过。非常感谢任何支持。

1 个答案:

答案 0 :(得分:2)

这是一个完整的例子。

enter image description here

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
        }
    }
}

CellCollectionViewCell.swift的内容

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

OtherViewController.swift的内容

class OtherViewController: UIViewController {

    var selectedIndex: Int!

    override func viewDidLoad() {
        super.viewDidLoad()

        title = String(selectedIndex)
    }
}