代码之家  ›  专栏  ›  技术社区  ›  Sweeper

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

  •  1
  • Sweeper  · 技术社区  · 10 年前

    我正在使用表格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)
    }
    

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

    但是,我希望在中以编程方式选择行 查看DidLoad ,以显示当前设置。

    我找到了一个名为 selectRowAtIndexPath 。我尝试过使用它,但它需要 UITableViewScrollPosition 。医生说:

    表视图中给定行滚动到的位置(上、中、下)。

    似乎每当我想选择一行时,表视图必须滚动到所选行的位置 只有 选择它。

    如何做到这一点?

    3 回复  |  直到 10 年前
        1
  •  5
  •   user4151918 user4151918    10 年前

    the documentation 提到,您可以指定 UITableViewScrollPositionNone 这将导致不滚动。

        2
  •  1
  •   Hitendra Solanki    10 年前

    这完全取决于逻辑。

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

    相反,您只需要维护索引变量,该变量将存储所选行的indexPath。您有3个部分,并且允许进行3次选择,在这种情况下,您只需要维护一个保存selectedIndexPaths值的数组。

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

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

    在里面 viewDidLoad: 根据上次选择的设置添加三个默认值。

    假设用户上次选择

    第0节中的第1行

    第1部分中的第2行

    第2节中的第0行

    那么您的数组将如下所示,

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

    根据我们对您问题的评论中的讨论,我写了上述逻辑。下面是我们的讨论。

    @清扫器您是否试图根据上次设置值在一行中仅显示复选标记?HitendraHckr公司

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

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

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

    @HitendraHckr是的,你说得对,清洁工

        3
  •  1
  •   Crazy Developer    10 年前

    您可以使用下面的代码直接调用表视图的dodselect方法

    let indexpath = NSIndexPath(forRow: yourRow, inSection: yourColumn);
    self.tableView(tblObj, didSelectRowAtIndexPath: indexpath);
    

    实际上,您想调用的是选择执行一些操作,这些操作可以通过此操作进行管理。试试这个,它可能对你有用。

    推荐文章