ios 自动滚动到具有特定值的单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28043632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Auto scrolling to a cell with a specific value
提问by Isuru
I have a list of numbers in a table view like this.
我在这样的表格视图中有一个数字列表。
As you can see the numbers are repeated. Let's consider a set of repeated numbers as a group. So there are group of 1s, group of 2s and so on.
如您所见,这些数字是重复的。让我们将一组重复的数字视为一组。所以有1组,2组等等。
What I want to do is when the app starts, I need to scroll to the start position of a specified group automatically. Before explaining further, here is my code so far.
我想要做的是当应用程序启动时,我需要自动滚动到指定组的开始位置。在进一步解释之前,这是我目前的代码。
import UIKit
class TableViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {
private var scrollToTime = true
private var items = [Int]()
private var groupNoToScroll = 12
override func viewDidLoad() {
super.viewDidLoad()
items = [1, 1, 2, 2, 2, 3, 4, 4, 4, 4, 4, 5, 5, 6, 7, 7, 8, 8, 8, 9, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 13, 13, 14, 14, 14, 14, 15, 15, 16, 17, 17, 18, 18, 18, 19, 19, 19, 19, 20, 21, 22, 22, 23, 23, 23]
}
// MARK: - UITableViewDataSource
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = String(items[indexPath.row])
return cell
}
// MARK: - UITableViewDelegate
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
let lastRow = tableView.indexPathsForVisibleRows()?.last as NSIndexPath
if indexPath.row == lastRow.row {
if scrollToTime == true {
let indexPath = NSIndexPath(forRow: groupNoToScroll, inSection: 0)
tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true)
scrollToTime = false
}
}
}
}
I've assigned the value 12 for the variable groupNoToScroll
. This means when the app starts I want the table view to be auto scrolled to the start of the cells of the 12s group.
我已为变量分配了值 12 groupNoToScroll
。这意味着当应用程序启动时,我希望表格视图自动滚动到 12s 组的单元格的开头。
But currently what my code does is scroll to the 12th cell, not the cell that has the value12. My question is how can I check with the cells' values and scroll to the number which I specify?
但目前我的代码所做的是滚动到第 12 个单元格,而不是具有值12的单元格。我的问题是如何检查单元格的值并滚动到我指定的数字?
回答by rakeshbs
You can use find the index of the item(which will be its row), and then scroll to that index. find
function returns the index of a particular element in the array.
您可以使用 find 项目的索引(这将是它的行),然后滚动到该索引。find
函数返回数组中特定元素的索引。
if let index = find(items, groupNoToScroll)
{
let indexPath = NSIndexPath(forRow: index, inSection: 0)
tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true)
}
回答by Gefilte Fish
Swift 4:
斯威夫特 4:
let indexPath = IndexPath(row: row, section: section)
tableView.scrollToRow(at: indexPath, at: .top, animated: true)
(first, of course, you have to assign values to row and section based on which cell you want to scroll to)
(当然,首先,您必须根据要滚动到的单元格为行和节分配值)
回答by Harshil Kotecha
Swift 3.0
斯威夫特 3.0
let indexPath = NSIndexPath(forRow: 5, inSection: 0)
tableView.scrollToRow(at: indexPath, at: .top, animated: true)
Swift 4.0
斯威夫特 4.0
let lastRowIndex = self.tblRequestStatus!.numberOfRows(inSection: 0) - 1
let pathToLastRow = IndexPath.init(row: lastRowIndex, section: 0)
tableView.scrollToRow(at: pathToLastRow, at: .none, animated: false)
回答by Petri Oosthuizen
Do this to find the first element in the array that is equal to groupNoToScroll. Then, go to that row.
执行此操作以查找数组中与 groupNoToScroll 相等的第一个元素。然后,转到该行。
var rowToGoTo:Int = 0 //Rather use the Swift find function.
for x in items{
if x == groupNoToScroll{
break
}
rowToGoTo++
}
let lastRow = tableView.indexPathsForVisibleRows()?.last as NSIndexPath
if indexPath.row == lastRow.row {
if scrollToTime == true {
let indexPath = NSIndexPath(row: rowToGoTo, section: 0)
tblBusList.scrollToRow(at: indexPath as IndexPath, at: .top, animated: true)
}
}
But I would recommend doing this in viewDidAppear.
但我建议在 viewDidAppear 中执行此操作。
As Isuru pointed out rather use the find function.
正如 Isuru 指出的,而是使用 find 函数。