2016-12-04 59 views
0

彈出時收到錯誤偷看,並從集合視圖

libc++abi.dylib: terminating with uncaught exception of type NSException 
(lldb) 

。我檢查了我的數據結構和索引路徑,但一切似乎都很好。

這裏是我的代碼爲收藏查看

class thisSeaonViewController: UICollectionViewController, UIViewControllerPreviewingDelegate { 

    @IBOutlet weak var activityIndicator: UIActivityIndicatorView! 
    var URLArrayStringThisSeason = [String]() 
    var currentURL = String() 

    override func viewDidLoad() { 
     generateData() 
     if(traitCollection.forceTouchCapability == .available){ 
      registerForPreviewing(with: self as! UIViewControllerPreviewingDelegate, sourceView: view) 
     } 
    } 

    override func viewDidAppear(_ animated: Bool) { 
    } 

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) 

     let imageView = cell.viewWithTag(1) as! UIImageView 

     let url = NSURL(string: URLArrayStringThisSeason[indexPath.row]) 

     let placeholderImage = UIImage(named: "Rectangle")! 

     let filter = AspectScaledToFillSizeWithRoundedCornersFilter(
      size: imageView.frame.size, 
      radius: 0 
     ) 

     imageView.af_setImage(withURL: url as! URL, placeholderImage: placeholderImage, filter: filter, imageTransition: .crossDissolve(0.2) 
     ) 


     cell.backgroundColor = UIColor.init(hexString: "#F3F3F3") 
     cell.layer.cornerRadius = 3.0 

     return cell 
    } 

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

     return URLArrayStringThisSeason.count 
    } 

    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 

     let vc = storyboard?.instantiateViewController(withIdentifier: "gridDetailedView") as! gridDetailedViewController 
     vc.imageURL = URLArrayStringThisSeason[indexPath.row] 
     self.navigationController?.pushViewController(vc, animated: true) 
    } 



    func generateData() { 
     if URLArrayStringThisSeason.count == 0 { 
      self.activityIndicator.isHidden = false 
      self.activityIndicator.startAnimating() 
     } 

     let queryThisSeason = FIRDatabase.database().reference().child("thisSeason") 
     queryThisSeason.keepSynced(true) 
     queryThisSeason.observeSingleEvent(of: .value, with: {(snapshot) in 

      if snapshot.childrenCount != 0 { 
       let urlArray = snapshot.value as! [String] 
       let urlLimitedArray = Array(urlArray.reversed()) 

       self.URLArrayStringThisSeason = urlLimitedArray 
       self.collectionView?.reloadData() 

       self.activityIndicator.stopAnimating() 
       self.activityIndicator.isHidden = true 
      } 

     }) 

    } 

    func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? { 

     guard let indexPath = collectionView?.indexPathForItem(at: location) else { return nil } 

     guard let cell = collectionView?.cellForItem(at: indexPath) else { return nil } 

     guard let detailVC = storyboard?.instantiateViewController(withIdentifier: "gridDetailedView") as? gridDetailedViewController else { return nil } 


     //let photo = UIImage(named: "Rectangle") 
     detailVC.imageURL = URLArrayStringThisSeason[indexPath.row] 

     print(URLArrayStringThisSeason[indexPath.row]) 

     detailVC.preferredContentSize = CGSize(width: 300, height: 300) 

     previewingContext.sourceRect = cell.frame 


     print("peek") 


     return detailVC 
    } 



    func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) { 
     self.navigationController?.show(viewControllerToCommit, sender: Any?.self) 

     print("pop") 

    } 
} 
偷看和彈出時

,功能應該在IMAGEURL發送到詳細視圖控制器和AlamofireImage將處理圖像下載和加載。

但是,由於源矩形將略微高於單元格並防止在單元的某些部分出現窺視和彈出,因此我一直在收集視圖中出現未對齊問題。我認爲這可能也是偷窺和流行崩潰的原因。

編輯:

這裏發生了什麼,當我嘗試做PEEK和流行,你可以看到細胞的焦點稍稍偏離頂部。

enter image description here

回答

1

好,我改變這個registerForPreviewingWithDelegate固定的錯位問題(自我,sourceView:視圖)

這個 registerForPreviewingWithDelegate(個體經營,sourceView:self.collectionView)

然而,每次我嘗試偷看和彈出時,該應用程序仍然崩潰。

編輯:

確定的另一個問題是非常簡單,只是在代碼中的一些錯誤。只需按照上面的說明來解決錯位問題。

+0

我有同樣的問題,這也解決了它。 –

+0

很高興爲你解決這個問題:)猜猜它是有道理的,如果我們沒有首先將它指向正確的sourceview,那麼peek彈出式代表無法正確註冊視圖。 – Wilson