在视图加载时选择一行

时间:2016-09-03 09:15:12

标签: swift uitableview

我正在尝试在视图加载时预先选择一个表行,但是我尝试了几种方法,但似乎都没有。首先,我设置了以下两种方法,以便在选择和取消选择行时定义textColor更改

open func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    let cell = menuCells[indexPath.row]

    cell.textLabel?.textColor = UIColor.white.withAlphaComponent(0.4)
}

open func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = menuCells[indexPath.row]

    cell.textLabel?.textColor = UIColor.white.withAlphaComponent(1.0)

    UIApplication.shared.sendAction(cell.menuAction, to: cell.menuTarget, from: cell, for: nil)
}

然后我尝试做一些事情来预先选择一行,但它似乎没有将带有alpha的textColor更改为1.0

open func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    if indexPath.row == 0 {
        cell.setSelected(true, animated: false)
    }
}



menuView.selectRow(at: IndexPath(row: 0, section: 0), animated: false, scrollPosition: UITableViewScrollPosition.none)

4 个答案:

答案 0 :(得分:2)

根据Apple的文档:

调用 selectRowAtIndexPath 不会导致委托接收tableView(:willSelectRowAt :)或tableView(:didSelectRowAt :)消息

https://developer.apple.com/reference/uikit/uitableview/1614875-selectrowatindexpath

因此,在viewDidAppear方法中,您应该手动调用didSelectRowAtIndexPath,如下例所示:

class ViewController: UIViewController {

  @IBOutlet weak var myTableView: UITableView!


  override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)


    let indexPath = NSIndexPath(forRow: 2, inSection: 0)

    myTableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: .None)

    // Calling manually to the delegate method
    tableView(myTableView, didSelectRowAtIndexPath: indexPath)
  }

}


// UITableView Delegate Implementation
extension ViewController: UITableViewDataSource, UITableViewDelegate {

  func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath)
    cell?.textLabel?.textColor = UIColor.blackColor()
  }

  func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath)
    cell?.textLabel?.textColor = UIColor.redColor()
    cell?.textLabel?.backgroundColor = UIColor.clearColor()
  }

  func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
  }

  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

    cell.textLabel?.text = String(indexPath.row)

    return cell
  }
}

结果:

image

答案 1 :(得分:1)

这是你之后的事吗?

override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        let iPath = NSIndexPath(forRow: 0, inSection: 0)
        tableView.selectRowAtIndexPath(iPath, animated: true, scrollPosition: .Top )
    }

答案 2 :(得分:0)

请记住,tableView(_:didSelectRowAt:)是委托协议的一部分,旨在处理用户与UITableView实例的交互。因此,它响应用户交互而不是程序化变化而被调用。

要解决此问题,请将外观更改移动到名为applyAppearanceSelected之类的单独函数中,并在进行编程更改时调用此函数,并从tableView(_:didSelectRowAt:)调用它以处理用户启动的更改。我不建议您自己直接调用tableView(_:didSelectRowAt:),因为您可能希望添加其他代码,仅在用户选择单元格时才适用。

答案 3 :(得分:0)

我认为选择<script src="..."></script>方法中的行是最合适的,因为视图必须已完全初始化。

以下示例选择第一部分的第一个单元格:

viewWillAppear