[英]prepareForSegue collectionView using 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)
}
Segue公司:
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
如果我使用錯誤的方法,請幫助我。 讓我知道您是否需要更多數據以更好地了解問題。
在performPergue中,您正在傳遞視圖控制器本身的對象。 在您的prepareForSegue中,您嘗試獲取返回nil的ViewController的indexPath。
而是在prepareForSegue中,您可以執行以下操作:indexPath = self.collectionView.indexPathForSelectedRow?
問題在這里:
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.