ios - prepareForSegue collectionView 使用 swift

标签 ios swift

我正在使用 collectionView,我想将选定的单元格数据发送到第二个 Controller

要从服务器获取 json 数据,我使用的是 alamofire:

class TableViewController: UITableViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    var datas: JSON = []
    let brandSegueIdentifier = "showBrandSegue"

func getData() {
    let url = "http://192.168.3.16/dbo/data4.json"
    Alamofire.request(.GET, url).validate().responseJSON { response in
        switch response.result {
        case .Success:
            if let value = response.result.value {
                let json = JSON(value)
                self.datas = json
                self.collectionView.reloadData()
                print("JSON: \(json)")
            }
        case .Failure(let error):
            print(error)
        }
    }
}
}

项目数:

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return datas.count
}

项目单元格:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell =  collectionView.dequeueReusableCellWithReuseIdentifier("brandCell", forIndexPath: indexPath) as! BrandCollectionViewCell

    cell.data = self.datas[indexPath.row]
    return cell
}

项目的大小:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    let itemWidth = 100
    let itemHeight = 100
    return CGSize(width: itemWidth, height: itemHeight)
}

继续:

func collectionView(collectionView: UICollectionView, selectedItemIndex: NSIndexPath) {
    self.performSegueWithIdentifier(brandSegueIdentifier, sender: self)
}

准备转场:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == brandSegueIdentifier {
        if let indexPath = self.collectionView?.indexPathForCell(sender as! BrandCollectionViewCell) {
            let destination = segue.destinationViewController as! BrandViewController
            if (self.datas[indexPath.row]["brandName"].string != nil) {
                destination.brandLabel.text = self.datas[indexPath.row]["brandName"].stringValue
                print(self.datas[indexPath.row]["brandName"].stringValue)
            }
        }
    }
}

当我要使用 prepareForSegue 将数据发送到另一个 View Controller 时,出现错误

fatal error: unexpectedly found nil while unwrapping an Optional value

但是当我关闭下面这一行时,错误不会出现:

destination.brandLabel.text = self.datas[indexPath.row]["brandName"].stringValue

如果我使用了错误的方法,请帮助我。如果您需要更多数据以更好地了解问题,请告诉我。

最佳答案

问题出在这里:

func collectionView(collectionView: UICollectionView, selectedItemIndex: NSIndexPath) {
    self.performSegueWithIdentifier(brandSegueIdentifier, sender: self)
}

发件人应该是单元格而不是 View Controller 本身,因为你想在 prepareForSegue 方法中将发件人解析为单元格,所以它应该是:

func collectionView(collectionView: UICollectionView, selectedItemIndex: NSIndexPath) {
    let cell = collectionView.cellForItemAtIndexPath(selectedItemIndex);
    self.performSegueWithIdentifier(brandSegueIdentifier, sender: cell);
}

关于ios - prepareForSegue collectionView 使用 swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36419179/

相关文章:

swift - Realm 、配置和应用组

ios - 更改 NSLayoutConstraint 常量在layoutSubviews中不起作用

ios - 每台计算机上缺少应用程序标识符和访问钥匙串(keychain)组

ios - 使用 swift 放大到 PDFKit 中的特定坐标

iOS Swift 多维数组 - 编译需要很长时间。我应该改变什么?

swift - 类型 'MessagesController?' 没有成员 'setupNavBarWithUser'

arrays - 如何迭代数组并将每个元素与 Swift 中的其他元素进行比较

ios - UITextField — 观察文本选择和光标移动期间的变化

swift - 如何反转 Swift 扩展中的链表?

ios - 您什么时候会在推送负载中使用 "thread-id" key ?