9

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
}
3
  • 1
    I think you need to actually select the cell as well.
    – j.f.
    Nov 11, 2015 at 19:36
  • stackoverflow.com/a/13808981/312594
    – par
    Nov 11, 2015 at 19:36
  • I've tried selecting it aswell with no result tableView!.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: UITableViewScrollPosition.None)
    – Peter Pik
    Nov 11, 2015 at 19:43

4 Answers 4

9

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

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

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 .

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.