【问题标题】:Set default selected cell in UITableViewController在 UITableViewController 中设置默认选中的单元格
【发布时间】:2015-11-11 19:31:44
【问题描述】:

我正在尝试设置默认的 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
}

【问题讨论】:

  • 我认为您还需要实际选择单元格。
  • 我也试过选择它没有结果tableView!.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: UITableViewScrollPosition.None)

标签: ios swift uitableview


【解决方案1】:

试试这个:

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

【讨论】:

    【解决方案2】:

    有时您的代码并不总是写在 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】:

      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 替换。

      【讨论】:

        【解决方案4】:

        试试这个(Swift 5):

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-12-01
          相关资源
          最近更新 更多