ios - 从 UICollectionView 选择后使用核心数据填充详细 View

标签 ios iphone swift core-data uicollectionview

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

我有一个 UICollectionView,它本质上是一个从核心数据中提取的联系人列表。当用户选择一个人(或 UICollectionView 中的一个项目)时,我想显示联系人的详细 View 。我已经在 Storyboard 中创建并连接了该 View /segue,但是我无法将所选项目传递给详细 View 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)")
    }
}

这是来自同一 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
}

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

}

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

最佳答案

这是一个完整的例子。

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

关于ios - 从 UICollectionView 选择后使用核心数据填充详细 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35541268/

相关文章:

android - 如何在 Flutter 中创建这个布局?

iphone - 计算 objective-c 中的时间差

iphone - 通过应用内购买将现有的付费应用程序转换为免费版本

ios - 数组包含方法无法与自定义模型和谓词一起正常工作

swift - 用于在返回不正确数字的二维数组中查找岛屿的 DFS 函数

ios - 为什么在延迟调用时循环迭代有时会被打乱? [代码和输出]

ios - 从另一个 View Controller 更新 UIPageControl

ios - 使用 CCSprites 制作 b2Body 动画?

iphone - 当我们在iOS中没有像素失真的小图像时,如何创建大图像?

ios - 尝试使用 Storyboard ID 从 GameScene 移动到 ViewController 时发生 fatal error