Как прокрутить UITableView на незавершенный вопрос - PullRequest
0 голосов
/ 11 июня 2019

У меня есть UITableView с несколькими вопросами, и мне не удается прокрутить вниз до следующего незавершенного вопроса.Поэтому, если я закончу вопрос 1, тогда я уйду и завершу последний вопрос, свиток должен привести меня к вопросу 2, потому что это следующий Неполный вопрос из моего списка.Кто-нибудь есть какие-либо идеи, как я могу добиться этого?

Вот моя маленькая демонстрационная программа проекта: https://github.com/tygruletz/ChecklistScrollDown

Вот пример того, как должно работать: http://g.recordit.co/sy0CqwHN9a.gif

Вот часть моего исходного кода:

// Get all the values from the ChecklistCell using the delegates
extension ChecklistVC: ChecklistCellDelegate{  

    // Check if user pressed Pass or Fail btn for Vehicle/Trailer and scroll to next question
    func tappedOnVehicleOrTrailerButtons(vehiclePassBtn: UIButton, vehicleFailBtn: UIButton, trailerPassBtn: UIButton, trailerFailBtn: UIButton, selectedCell: ChecklistCell) {

        let indexPath = questionsTableView.indexPath(for: selectedCell)!
        let item = itemSections[indexPath.section].checklistItems[indexPath.row]

        // Only Vehicle available
        if item.showVehicle && !item.showTrailer {

            if vehiclePassBtn.isSelected {
                item.vehiclePass = item.PASS

                scrollDown(row: indexPath.row, section: indexPath.section)
            }
            else if vehicleFailBtn.isSelected {
                item.vehiclePass = item.FAIL
                scrollDown(row: indexPath.row, section: indexPath.section)
            }
            else{
                item.vehiclePass = item.INCOMPLETE
            }
        }

            // Only Trailer available
        else if item.showTrailer && !item.showVehicle{

            if trailerPassBtn.isSelected {
                item.trailerPass = item.PASS
                scrollDown(row: indexPath.row, section: indexPath.section)
            }
            else if trailerFailBtn.isSelected {
                item.trailerPass = item.FAIL
                scrollDown(row: indexPath.row, section: indexPath.section)
            }
            else{
                item.trailerPass = item.INCOMPLETE
            }
        }

            // Both available: Vehicle and Trailer
        else if item.showVehicle && item.showTrailer {

            if vehiclePassBtn.isSelected {
                item.vehiclePass = item.PASS
            }
            else if vehicleFailBtn.isSelected {
                item.vehiclePass = item.FAIL
            }
            if trailerPassBtn.isSelected {
                item.trailerPass = item.PASS
            }
            if trailerFailBtn.isSelected {
                item.trailerPass = item.FAIL
            }
        }

        print("For section \(indexPath.section) row: \(indexPath.row) - vehicle status = \(item.vehiclePass)")
        print("For section \(indexPath.section) row: \(indexPath.row) - trailer status = \(item.trailerPass)")

        if (item.vehiclePass == 1 || item.vehiclePass == 2) && (item.trailerPass == 1 || item.trailerPass == 2) {

            scrollDown(row: indexPath.row, section: indexPath.section)
        }

        questionsTableView.reloadData()
    }

    // Function to scroll down to next row when the user complete a question
    func scrollDown(row: Int, section: Int){

        let lastRowFromSection = questionsTableView.numberOfRows(inSection: section) - 1
        let lastSection = questionsTableView.numberOfSections - 1
        let lastRowFromLastSection = questionsTableView.numberOfRows(inSection: lastSection) - 1

        // Not last Row and Not Last Section -> scroll to next row from the same section
        if row != lastRowFromSection && section != lastSection {
            questionsTableView.scrollToRow(at: NSIndexPath(row: row + 1, section: section) as IndexPath, at: .top, animated: true)
        }

        // Last Row but Not Last Section -> scroll to first Row from next Section
        if row == lastRowFromSection && section != lastSection {
            questionsTableView.scrollToRow(at: NSIndexPath(row: 0, section: section + 1) as IndexPath, at: .top, animated: true)
        }

        // Not Last Row but Last Section -> scroll to next row from last section
        if row != lastRowFromLastSection && section == lastSection {
            questionsTableView.scrollToRow(at: NSIndexPath(row: row + 1, section: section) as IndexPath, at: .top, animated: true)
        }

        // Last Row from Last Section -> scroll to first row from first section
        if row == lastRowFromLastSection && section == lastSection {
            questionsTableView.scrollToRow(at: NSIndexPath(row: 0, section: 0) as IndexPath, at: .top, animated: true)
        }
    }
}

Вот снимок с моим прогрессом для ScrollDown:

enter image description here

1 Ответ

0 голосов
/ 11 июня 2019

Не могли бы вы попробовать это;

extension ChecklistVC: ChecklistCellDelegate{  

        // Check if user pressed Pass or Fail btn for Vehicle/Trailer and scroll to next question
        func tappedOnVehicleOrTrailerButtons(vehiclePassBtn: UIButton, vehicleFailBtn: UIButton, trailerPassBtn: UIButton, trailerFailBtn: UIButton, selectedCell: ChecklistCell) {

            let indexPath = questionsTableView.indexPath(for: selectedCell)!
            let item = itemSections[indexPath.section].checklistItems[indexPath.row]

            // Only Vehicle available
            if item.showVehicle && !item.showTrailer {

                if vehiclePassBtn.isSelected {
                    item.vehiclePass = item.PASS

        UIView.transition(with: tableView, duration: 0.2, options: .curveEaseIn, animations: {self.tableView.reloadData()}, completion:{ (success) in
        if success {
                    scrollDown(row: indexPath.row, section: indexPath.section)
                   }
                  })
                }
                else if vehicleFailBtn.isSelected {
                    item.vehiclePass = item.FAIL
     UIView.transition(with: tableView, duration: 0.2, options: .curveEaseIn, animations: {self.tableView.reloadData()}, completion:{ (success) in
        if success {
                    scrollDown(row: indexPath.row, section: indexPath.section)
                    }
                  })
                }
                else{
                    item.vehiclePass = item.INCOMPLETE
                }
            }

                // Only Trailer available
            else if item.showTrailer && !item.showVehicle{

                if trailerPassBtn.isSelected {
                    item.trailerPass = item.PASS
                    scrollDown(row: indexPath.row, section: indexPath.section)
                }
                else if trailerFailBtn.isSelected {
                    item.trailerPass = item.FAIL
                    scrollDown(row: indexPath.row, section: indexPath.section)
                }
                else{
                    item.trailerPass = item.INCOMPLETE
                }
            }

                // Both available: Vehicle and Trailer
            else if item.showVehicle && item.showTrailer {

                if vehiclePassBtn.isSelected {
                    item.vehiclePass = item.PASS
                }
                else if vehicleFailBtn.isSelected {
                    item.vehiclePass = item.FAIL
                }
                if trailerPassBtn.isSelected {
                    item.trailerPass = item.PASS
                }
                if trailerFailBtn.isSelected {
                    item.trailerPass = item.FAIL
                }
            }

            print("For section \(indexPath.section) row: \(indexPath.row) - vehicle status = \(item.vehiclePass)")
            print("For section \(indexPath.section) row: \(indexPath.row) - trailer status = \(item.trailerPass)")

            if (item.vehiclePass == 1 || item.vehiclePass == 2) && (item.trailerPass == 1 || item.trailerPass == 2) {

                scrollDown(row: indexPath.row, section: indexPath.section)
            }

            questionsTableView.reloadData()
        }

        // Function to scroll down to next row when the user complete a question
        func scrollDown(row: Int, section: Int){

            let lastRowFromSection = questionsTableView.numberOfRows(inSection: section) - 1
            let lastSection = questionsTableView.numberOfSections - 1
            let lastRowFromLastSection = questionsTableView.numberOfRows(inSection: lastSection) - 1

            // Not last Row and Not Last Section -> scroll to next row from the same section
            if row != lastRowFromSection && section != lastSection {
                questionsTableView.scrollToRow(at: NSIndexPath(row: row + 1, section: section) as IndexPath, at: .top, animated: true)
            }

            // Last Row but Not Last Section -> scroll to first Row from next Section
            if row == lastRowFromSection && section != lastSection {
                questionsTableView.scrollToRow(at: NSIndexPath(row: 0, section: section + 1) as IndexPath, at: .top, animated: true)
            }

            // Not Last Row but Last Section -> scroll to next row from last section
            if row != lastRowFromLastSection && section == lastSection {
                questionsTableView.scrollToRow(at: NSIndexPath(row: row + 1, section: section) as IndexPath, at: .top, animated: true)
            }

            // Last Row from Last Section -> scroll to first row from first section
            if row == lastRowFromLastSection && section == lastSection {
                questionsTableView.scrollToRow(at: NSIndexPath(row: 0, section: 0) as IndexPath, at: .top, animated: true)
            }
        }
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...