从集合视图中偷看和弹出时出错

时间:2016-12-04 13:56:58

标签: swift collections

收到错误

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将处理图像下载和加载。

但是,我一直在收集视图时出现错位问题,因为源矩形会出现在单元格的上方,并阻止在单元格的某些部分进行查看和弹出。我认为这也可能是偷看和流行崩溃的原因。

编辑:

这里当我尝试偷看和弹出时会发生什么,你可以看到单元格的焦点略微向上移动。

enter image description here

1 个答案:

答案 0 :(得分:1)

好的我通过更改此registerForPreviewingWithDelegate(self,sourceView:view)修复了错位问题

对此 registerForPreviewingWithDelegate(self,sourceView:self.collectionView!)

然而,每当我试图偷看和弹出时,应用程序仍然会崩溃。

编辑:

好的另一个问题就是代码中的一些错误。只需按照上述步骤修复错位问题即可。