gpt4 book ai didi

ios - prepareForSegue collectionView 使用 swift

转载 作者:搜寻专家 更新时间:2023-11-01 07:25:45 24 4
gpt4 key购买 nike

我正在使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com