Peter Pik
Peter Pik

Reputation: 11193

Set default selected cell in UITableViewController

I'm trying to set a default selectedCell and by default i mean that the cell has a specific selectedCellBackgroundView. However even though i've created a method for it, it does not seem to work. It does not show any selectedBackgroundView on the first cell? It works fine when i select a cell manually. What am i doing wrong?

viewDidLoad and viewWillAppear tried both

let indexPath = NSIndexPath(forRow: 0, inSection: 0)
let cell = tableView!.cellForRowAtIndexPath(indexPath)! as UITableViewCell

applySelectionChangesToCell(cell)

didSelectRowAtIndexPath

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let cell = tableView.cellForRowAtIndexPath(indexPath)! as UITableViewCell
    applySelectionChangesToCell(cell)

    NSUserDefaults.standardUserDefaults().setInteger(menuArray[indexPath.row].id!, forKey: "leagueId")

    self.sideMenuViewController.hideMenuViewController()

}

applySelectionChangesToCell

func applySelectionChangesToCell(cell:UITableViewCell) {

    let backgroundSelectionView = UIView(frame: cell.bounds)
    backgroundSelectionView.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.1)
    cell.selectedBackgroundView = backgroundSelectionView
}

Upvotes: 10

Views: 15451

Answers (4)

Stephen Chen
Stephen Chen

Reputation: 3057

Sometimes your code are not always write inside the UIViewController, Ex: subclass a UITableView

You just need a flag and easy for further reuse

final class CustomTableView: UITableView {

    private var rowDefaultSelected: Int = 0

    //..... 
}

And about UITableViewDelegate

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if indexPath.row == rowDefaultSelected {
        tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
        rowDefaultSelected = -1
    }
}

Upvotes: 4

Hula
Hula

Reputation: 71

try this(Swift 5):

override func viewDidLoad() {
    super.viewDidLoad()
    let indexPath = IndexPath(row: 0, section: 0)
    tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
    }

This will select the first row in the first section of your table view when the view controller loads.

Upvotes: 0

JMGM
JMGM

Reputation: 772

In Swift 4 this is the updated answer :)

class TableViewController: UITableViewController {
  override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 5
  }

  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) {
    let cell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath)

    // setup selectedBackgroundView
    let backgroundSelectionView = UIView()
    backgroundSelectionView.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.1)
    cell.selectedBackgroundView = backgroundSelectionView

    return cell
  }

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

    let indexPath = IndexPath(row: 0, section: 0)
    tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
  }
}

Basically NSIndexPath has been replaced for IndexPath .

Upvotes: 2

André Slotta
André Slotta

Reputation: 14030

try this:

class TableViewController: UITableViewController {
  override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 5
  }

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

    // setup selectedBackgroundView
    let backgroundSelectionView = UIView()
    backgroundSelectionView.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.1)
    cell.selectedBackgroundView = backgroundSelectionView

    return cell
  }

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

    let indexPath = NSIndexPath(forRow: 0, inSection: 0)
    tableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: .None)
  }
}

Upvotes: 10

Related Questions