Peter Pik Peter Pik - 3 months ago 19
Swift Question

Select a row at view load

I'm trying to pre select a table row when the view load, however i have tried several methods, but none seem to work. At first i've set following two methods in order to define the textColor changes when a row is selected and deselected

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


Then i've tried to do following things to preselect a row but it does not seem to change the textColor with alpha to 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)

Answer

According Apple's documentation:

Calling selectRowAtIndexPath does not cause the delegate to receive a tableView(:willSelectRowAt:) or tableView(:didSelectRowAt:) message

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


So in viewDidAppear method you should call manually didSelectRowAtIndexPath like in the example below:

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

Result:

image