我有一个 UITableView
,其中填充了一系列问题。当我检查问题时,我想滚动到列表中的第一个不完整的问题。因此,如果我检查问题 1,在检查问题 10 后,我想滚动到问题 2,因为问题 2 不完整。
我怎样才能做到这一点?
这是我目前进度的链接:https://github.com/tygruletz/ChecklistScrollDown
这是我的代码的一部分:
// 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
item.isComplete = 1
scrollDown(row: indexPath.row, section: indexPath.section)
}
else if vehicleFailBtn.isSelected {
item.vehiclePass = item.FAIL
item.isComplete = 1
scrollDown(row: indexPath.row, section: indexPath.section)
}
}
// Only Trailer available
else if item.showTrailer && !item.showVehicle{
if trailerPassBtn.isSelected {
item.trailerPass = item.PASS
item.isComplete = 1
scrollDown(row: indexPath.row, section: indexPath.section)
}
else if trailerFailBtn.isSelected {
item.trailerPass = item.FAIL
item.isComplete = 1
scrollDown(row: indexPath.row, section: indexPath.section)
}
}
// 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
}
}
if (item.vehiclePass == 1 || item.vehiclePass == 2) && (item.trailerPass == 1 || item.trailerPass == 2) {
item.isComplete = 1
scrollDown(row: indexPath.row, section: indexPath.section)
}
print("Completed Questions: \(itemSections.map{$0.checklistItems.map {$0.isComplete}})")
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
itemSections.forEach {
let firstIncompleteQuestion = $0.checklistItems.first(where: {$0.isComplete == 0}) // I can't manage to display only the first questions from list, at the moment I display the first incomplete questions from each section. I need somehow to find the first incomplete question from all sections and to scroll to that question.
print("\n\nFirst incomplete question from list: \(firstIncompleteQuestion?.descript)")
print("Scroll to first incomplete question from list !")
}
// I think all below rows need to be removed and to create a single condition to scroll to first incomplete question every time.
// 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)
}
questionsTableView.reloadData()
}
}
这是屏幕截图:
感谢您尝试帮助我!
最佳答案
你可以使用这样的东西:
var indexPath:[IndexPath] = []
for section in 0..<self.tableView.numberOfSections {
for row in 0..<self.tableView.numberOfRows(inSection: section) {
guard let cell = self.tableView.cellForRow(
at: IndexPath(row: row, section: section)) as? MyCellType else {
return
}
if myCheck { // do your check here
indexPath.append(IndexPath(row: row, section: section))
}
}
}
if let first = indexPath.first {
self.tableView.scrollToRow(at: first, at: .middle, animated: true)
}
关于ios - 如何从对象列表中滚动到第一个不完整的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56595796/