自动滚动到具有特定值的单元格 [英] Auto scrolling to a cell with a specific value

查看:137
本文介绍了自动滚动到具有特定值的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这样的表格视图中有一个数字列表。

I have a list of numbers in a table view like this.

正如您所看到的那样,数字会重复出现。我们将一组重复的数字视为。所以有1组,2组等等。

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.

我想要做的是当应用程序启动时,我需要滚动到一个开始位置自动指定组。在进一步解释之前,这是我到目前为止的代码。

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
            }
        }
    }
}

我为变量 groupNoToScroll分配了值12 。这意味着当应用程序启动时,我希望表格视图自动滚动到12s组的单元格的开头。

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个单元格,而不是具有的单元格12.我的问题是如何检查单元格的值并滚动到我指定的数字?

But currently what my code does is scroll to the 12th cell, not the cell that has the value 12. My question is how can I check with the cells' values and scroll to the number which I specify?

推荐答案

您可以使用查找项目的索引(将是它的行),然后滚动到该索引。
find 函数返回数组中特定元素的索引。

You can use find the index of the item(which will be it's row), and then scroll to that index.
find function returns the index of a particular element in the array.

if let index = find(items, groupNoToScroll)
{
    let indexPath = NSIndexPath(forRow: index, inSection: 0)
    tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true)
}

这篇关于自动滚动到具有特定值的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆