滚动到第一个项目后的CollectionView问题

时间:2016-12-15 17:34:29

标签: ios swift uicollectionview

我有一个集合视图,它会自动将单元格逐个滚动到最后。在最后一个单元格中,我有一个重新开始的按钮并滚动到第一个单元格。现在一切正常,直到这一点,但滚动到第一个单元格后,我在第3个,第6个,第9个单元格上看到了我的开始按钮(这是我的最后一个单元格)(每3个单元格基本上是1次)。这是我的代码:

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if (indexPath.item >= alpImageArray.count - 1){
            let cell = myCollectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! AlphabetCollectionViewCell
            cell.startAgain.isHidden = false
            cell.startAgain.addTarget(self, action: #selector(startAgainPressed), for: .touchUpInside)
            cell.alpImage.isHidden = true
            cell.startAgain.setTitle("Start again", for: .normal)
            return cell
        } else {
            let cell = myCollectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! AlphabetCollectionViewCell
            cell.alpImage.image = UIImage(named: alpImageArray[indexPath.row] + ".png")
            return cell
        }
    }

    func scrollToNextCell(){

        let cellSize = CGSize(width: self.view.frame.width, height: self.view.frame.height)
        let contentOffset = myCollectionView.contentOffset
        myCollectionView.scrollRectToVisible(CGRect(x: contentOffset.x + cellSize.width, y: contentOffset.y, width: cellSize.width, height: cellSize.height), animated: true)
        }

    func startTimer() {

        _ = Timer.scheduledTimer(timeInterval: 0.5,
        target: self,
        selector: #selector(scrollToNextCell),
        userInfo: nil,
        repeats: true)
    }
    @IBAction func startAgainPressed(_ sender: UIButton) {
        myCollectionView.resetScrollPositionToTop()
    }

}

extension UIScrollView {
    /// Sets content offset to the top.
    func resetScrollPositionToTop() {
        self.contentOffset = CGPoint(x: -contentInset.left, y: -contentInset.left)
    }
}

1 个答案:

答案 0 :(得分:1)

您看到“重新开始”按钮在最初出列后会被重复使用。

由于您的开始按钮和AlphabetCollectionViewCell使用相同的alpImage cases,因此您也可以将其出列一次。从那里,在if语句中配置单元格的方式。试一试。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

     let cell = myCollectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! AlphabetCollectionViewCell

     if (indexPath.item >= alpImageArray.count - 1) {
         cell.alpImage.isHidden = true
         cell.startAgain.isHidden = false
         cell.startAgain.addTarget(self, action: #selector(startAgainPressed), for: .touchUpInside)
         cell.startAgain.setTitle("Start again", for: .normal)

     } else {
         cell.startAgain.isHidden = true
         cell.alpImage.isHidden = false
         cell.alpImage.image = UIImage(named: alpImageArray[indexPath.row] + ".png")
     }

     return cell
 }