Set selected cell to default in UITableViewController

I am trying to set defaultCell and by default I mean that the cell has a specific one selectedCellBackgroundView. However, despite the fact that I created a method for it, it does not seem to work. Does he not show selectedBackgroundViewin the first cell? It works great 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
}
+4
source share
3 answers

try the following:

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)
  }
}
+6
source

In Swift 4, this is an 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 with IndexPath.

+2

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.

0
source

Source: https://habr.com/ru/post/1615464/


All Articles