Проблема с представлением коллекции. Прокрутите по кнопке.

У меня возникла проблема с программно прокручивающимся видом коллекции после нажатия следующей кнопки. Данные, которые я получаю, получены из API. Есть 15 случайных вопросов, которые я хочу отобразить, которые работают нормально. Но когда я нажимаю на следующую кнопку, чтобы прокрутить представление коллекции для следующего вопроса, возникает несоответствие. Он движется на 2 строки вперед, а не на 1.

Вот код...

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ExamCollectionCell", for: indexPath) as! ExamCollectionCell


    cell.tableView.tag = indexPath.row

    tagSubstitute = cell.tableView.tag

    cell.tableView.delegate = nil
    cell.tableView.dataSource = nil

    cell.tableView.delegate = self
    cell.tableView.dataSource = self


    cell.tableView.reloadData()

    return cell

}

func changeToNextQuestion() {

    if countdownTimer.isValid{
        countdownTimer.invalidate()

    }


    var indexPath = IndexPath(row: tagSubstitute, section: 0)

    if indexPath.row < chosenArray.count - 1 {

        tagSubstitute += 1
        currentQuestion += 1
        questionNumberLbl.text = "\(currentQuestion - 1) / 15"

        nextButton.isHidden = false

        indexPath = IndexPath(row: tagSubstitute, section: 0)

        collectionView.scrollToItem(at: indexPath, at: .right, animated: true)

        print(tagSubstitute)


        startTimer()

    }
    else {

        nextButton.isHidden = true

        startTimer()

        if countdownTimer.isValid{
            countdownTimer.invalidate()
            questionNumberLbl.text = "\(currentQuestion - 1) / 15"
            print("End of Quiz")


        }

    }



}

0 ответов

Другие вопросы по тегам