在UITableViewController中设置默认选定的单元格

时间:2015-11-11 19:31:45

标签: ios swift uitableview

我尝试设置默认的selectedCell,默认情况下,我的意思是该单元格具有特定的selectedCellBackgroundView。然而,尽管我已经为它创建了一种方法,但它似乎不起作用。它在第一个单元格上没有显示任何selectedBackgroundView?我手动选择单元格时工作正常。我做错了什么?

viewDidLoad和viewWillAppear同时尝试了

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 个答案:

答案 0 :(得分:6)

试试这个:

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

答案 1 :(得分:2)

Swift 4 中,这是更新的答案:)

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

基本上NSIndexPath已替换为IndexPath。

答案 2 :(得分:1)

有时您的代码并不总是写在UIViewController中,例如:子类化UITableView

您只需要一个标志并且易于进一步重用

final class CustomTableView: UITableView {

    private var rowDefaultSelected: Int = 0

    //..... 
}

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

答案 3 :(得分:0)

尝试一下(快速5):

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

加载视图控制器后,这将选择表视图第一部分的第一行。