I'm using tableView.scrollToRow
inside of the didSelectRowAt
method so that whenever a row is selected, the view automatically scrolls the next row to the top of the screen. 我让它工作得很好,除了当屏幕上有足够的空间容纳所有东西时,我不确定如何处理最后几行。
在下面的示例中,单击第 5 步之后会显示我正在描述的场景。
这是我的相关代码:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let topInstruction = arrayOne[currentArrayOneIndex!].instructions![topInstructionIndex]
print(topInstructionIndex)
print(arrayOne[currentArrayOneIndex!].instructions!.count)
// Check if it is the last instruction
if topInstructionIndex < arrayOne[currentArrayOneIndex!].instructions!.count {
// Change cell colour to green
let tappedInstruction = tableView.cellForRow(at: indexPath)
tappedInstruction?.contentView.backgroundColor = #colorLiteral(red: 0.3411764801, green: 0.6235294342, blue: 0.1686274558, alpha: 1)
topInstructionIndex += 1
// Scroll to the next instruction
let nextInstruction = IndexPath(row: topInstructionIndex, section: 0)
tableView.scrollToRow(at: nextInstruction, at: UITableView.ScrollPosition.top, animated: true)
return
}
else {
// Finish the instructions
print("This is the last instruction.")
return
}
}
有什么我可以添加到didSelectRowAt
(或其他地方)以将最后一行滚动到顶部,即使所有内容都适合屏幕?