prepareForSegue collectionView使用swift

时间:2016-04-05 06:55:11

标签: ios swift

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

要从服务器获取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)
}

prepareForSegue:

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,我收到错误

fatal error: unexpectedly found nil while unwrapping an Optional value

但是当我在下面关闭此行时,错误不会显示:

destination.brandLabel.text = self.datas[indexPath.row]["brandName"].stringValue
如果我使用错误的方法,请帮助我。如果您需要更多数据以更好地概述问题,请告诉我。

2 个答案:

答案 0 :(得分:1)

在你的performSegue中,你传递了viewcontroller本身的对象。在prepareForSegue中,您正在尝试获取返回nil的ViewController的indexPath。

相反,在您的prepareForSegue中,您可以执行此操作 let indexPath = self.collectionView.indexPathForSelectedRow?

答案 1 :(得分:1)

问题在于:

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

发件人应该是单元格而不是视图控制器本身,因为您要将发件人解析为prepareForSegue方法中的单元格,因此它应该是:

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