簡體   English   中英

從UICollectionView中選擇后,使用核心數據填充詳細信息視圖

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

我有一個非常類似於這里發現的問題 ,只是我在Swift 2.0中編碼(他們的問題/答案是Objective-C),我的情況略有不同。

我有一個UICollectionView本質上是一個從核心數據中提取的聯系人列表。 當用戶選擇一個人(或UICollectionView中的一個項目)時,我想顯示聯系人的詳細視圖。 我已經在Storyboard中創建並連接了該視圖/ segue,但是我無法將所選項目傳遞到詳細信息視圖ViewController,以便它知道要從核心數據中查詢什么數據。

這是我的代碼片段,每個代碼都有描述:

首先,在我的“ 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)")
    }
}

這是來自同一視圖控制器的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等等,而使用核心數據則使我很困惑。 非常感謝任何支持。

這是一個完整的例子。

在此處輸入圖片說明

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM