简体   繁体   English

如何重写ScrollViewDidEndDecelerating并找到当前可见的单元格?

[英]How to Override ScrollViewDidEndDecelerating and find current visible cell ?

I have a view controller which contains a collectionView: 我有一个包含collectionView的视图控制器:

class TagViewController: UIViewController,  UICollectionViewDelegate, UICollectionViewDataSource {

    @IBOutlet weak var collectionView: UICollectionView!


    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return posts.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let post = posts[indexPath.row]
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! SNPostViewCell
        cell.isVideo = post.isVideo
        cell.mediaURL = URL(string: post.mediaURL)
        cell.backgroundColor = UIColor.black
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let post = posts[indexPath.row]
        if post.isVideo == true {
            self.performSegue(withIdentifier: "playVideo", sender: nil)
        } else {
            print("is image. might go full screen one day here")
        }
        let bin = self.bins[post.timeStamp]
        print(bin)
    }
    override func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {                //Error: method cannot override from its superclass
        <#code#>
    }

}

Although I can override those collectionView functions, it seems that to override scrollViewDidEndDecelerating ?I specifically need to have an instance of collectionView (and not do it in a view controller) 尽管我可以覆盖这些collectionView函数,但似乎要覆盖scrollViewDidEndDecelerating吗?我特别需要具有collectionView的实例(而不是在视图控制器中执行此操作)

OK so I made a custom collectionView which inherits from collectionViewController. OK,所以我做了一个自定义collectionView,它继承自collectionViewController。 This overrides scrollViewDidEndDecelerating correctly (the method now DOES override from its superclass), and I am using code inside of it to find the index of the current cell, 这会正确覆盖scrollViewDidEndDecelerating(该方法现在可以从其超类覆盖),并且我正在其中使用代码来查找当前单元格的索引,

class PagingCollectionViewController: UICollectionViewController {
...   
 override func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        if (self.collectionView.contentOffset.y < 0) {
            self.collectionView.contentOffset = CGPointMake(self.collectionView.contentOffset.x, 0.0);
        }
        for cell in yourCollectionViewname.visibleCells()  as [UICollectionViewCell]    {
            let indexPath = yourCollectionViewname.indexPathForCell(cell as UICollectionViewCell)

        }
    }

...
}

However, as you can see this needs to reference the current index of the collectionView which I wouldn't have defined in the class right? 但是,如您所见,这需要引用collectionView的当前索引,而我在类中没有定义该索引呢? Only in the view Controller where the instance is created and populated... 仅在创建和填充实例的视图控制器中...

So I don't really understand where to put this override scrollViewDidEndDecelerating function and how to access the data within it successfully - to find the index. 因此,我不太了解此重写scrollViewDidEndDecelerating函数的位置以及如何成功访问其中的数据-查找索引。

This is wrong: 这是错误的:

override func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    <#code#>
}

... because scrollViewDidEndDecelerating is not an override method. ...因为scrollViewDidEndDecelerating 不是 override方法。 It is a delegate method. 这是一个委托方法。

https://developer.apple.com/documentation/uikit/uiscrollviewdelegate/1619417-scrollviewdidenddecelerating https://developer.apple.com/documentation/uikit/uiscrollviewdelegate/1619417-scrollviewdidenddecelerating

So delete the word override , and set self as the delegate of the collectionView , and you're all set. 因此,删除单词override ,并将self设置为collectionViewdelegate ,就一切就绪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM