【问题标题】:Use core data to populate detail view after selection from UICollectionView从 UICollectionView 选择后使用核心数据填充详细视图
【发布时间】:2016-06-03 03:34:35
【问题描述】:

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

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

这是我的代码的 sn-p,每个都有描述:

首先,在我的“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 等等,使用核心数据,我感到很困惑。非常感谢任何支持。

【问题讨论】:

  • 所以您的问题是将选定的userName 传递给详细视图控制器?你的collectionView(_:didSelectItemAtIndexPath:) 实现在哪里?
  • 嘿@paulvs,我已将其添加到问题中。我不知道它会调用我需要的一切。这两种打印方法都只是简单地看它是否在拉任何东西。他们都没有打印任何东西......
  • 如果你的委托方法没有被调用,你是否将UICollectionView委托设置为self?
  • 我没有,可能是这个问题吗?这有什么影响?
  • 好的,这有点帮助。它现在将从 didSelectItemAtIndexPath 方法打印“selectedName”。从那里,我是否只需将该变量传递给 prepareForSegue 方法并将其传递给新的 viewController?

标签: ios iphone swift core-data uicollectionview


【解决方案1】:

这是一个完整的例子。

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

【讨论】:

  • 你简直太棒了。我仅从您的评论中得到了答案,但这个答案只会让它更清楚。非常感谢@paulvs,你帮了大忙。
  • 不用担心@skind,很高兴它有帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-22
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多