如何选择表视图中的单元格而不滚动它?

时间:2016-02-08 04:56:01

标签: ios swift uitableview scroll

我正在使用表格VC为我的应用程序构建设置屏幕。在viewDidLoad中,我想通过选择单元格向用户显示他/她的当前设置。然后在所选单元格的左侧将有一个复选标记。这是didSelectrowAtIndexPath方法:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath)
    for row in 0..<tableView.numberOfRowsInSection(indexPath.section) {
        let cellToBeDeselected = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: row, inSection: indexPath.section))
        cellToBeDeselected?.accessoryType = .None
    }
    cell!.accessoryType = .Checkmark
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
}

这很有效。当我选择一行时,会出现一个复选标记。当我选择另一行时,前一个复选标记消失,并出现一个新的复选标记。

但是,我想在viewDidLoad中以编程方式选择行,以显示当前设置。

我找到了一个名为selectRowAtIndexPath的方法。我尝试使用此功能但需要UITableViewScrollPosition。文档说:

  

滚动给定行的表格视图(顶部,中间,底部)中的位置。

似乎无论何时我想选择一行,表视图都必须滚动到所选行的位置。但我只想选择它,选择它。

怎么做?

3 个答案:

答案 0 :(得分:4)

the documentation所述,您可以为滚动位置指定UITableViewScrollPositionNone,这将导致不滚动。

答案 1 :(得分:1)

您可以使用以下代码

直接调用表格视图的选择方法
let indexpath = NSIndexPath(forRow: yourRow, inSection: yourColumn);
self.tableView(tblObj, didSelectRowAtIndexPath: indexpath);

实际上你想调用did select来执行一些可以由此管理的操作。试试这个,它可能适合你。

答案 2 :(得分:1)

这完全取决于逻辑。

当您选择任何行时,您将尝试首先取消选择所有行。 在检查当前代码时,发现您没有保持所选行的状态,因此它也会导致出列问题,即当所选单元格将被重用于其他单元格时,即使未选中,也会显示checkMark。

除此之外,您只需要维护索引变量,该变量将存储所选行的indexPath。你有3个部分,允许3个选项,在这种情况下,你只需要维护一个包含selectedIndexPaths值的数组。

在viewController中声明一个属性,如下所示。

var arraySelectedIndexPaths : Array<NSIndexPath> = Array();

viewDidLoad:中,根据上次选择的设置添加三个默认值。

假设用户上次选择了

第0节中的第1行

第1节中的第2行

第2节中的row0

然后你的数组将如下,

        //in viewDidLoad
        arraySelectedIndexPaths.append(NSIndexPath(forRow: 1, inSection: 0));
        arraySelectedIndexPaths.append(NSIndexPath(forRow: 2, inSection: 1));
        arraySelectedIndexPaths.append(NSIndexPath(forRow: 0, inSection: 2));

所以在tableView:cellForRowAtIndexPath:写下面的逻辑。 这个逻辑将在单元格中显示checkMark,如果它被选中,当它进入屏幕时。

if indexPath == self.arraySelectedIndexPaths[indexPath.section] {
   cell!.accessoryType = .Checkmark
}else{
   cell!.accessoryType = .None
}

并在tableView:didSelectedRowAtIndexPath:写入逻辑如下。 这将根据选择更新数组中的indexPath。 还将取消选择上一行并选择新行。

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath)

//deSelected previously selected Cell
        let cellToBeDeselected = tableView.cellForRowAtIndexPath(self.arraySelectedIndexPaths[indexPath.section])
        cellToBeDeselected?.accessoryType = .None

    cell!.accessoryType = .Checkmark
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
    self.arraySelectedIndexPaths[indexPath.section] = indexPath;//update new selected indexPath in array
}

根据我们在您的问题评论中的讨论,我已经写了上述逻辑。以下是我们的讨论。

@Sweeper你是否试图根据最后的设定值在一行中仅显示复选标记? - HitendraHckr

@HitendraHckr是的我。我试图在三个不同的行中显示复选标记,因为有三个设置 - 清扫器

那么,对于不同的3个设置,你有3个部分吗? - Hitendra Hckr

我认为任何一个都可以在一个部分中选择,对吗? - Hitendra Hckr

@HitendraHckr是的,你是对的 - 清扫车