2016-09-03 9 views
0

私はいくつかの方法を試しましたが、ビューの読み込み時にテーブル行を事前に選択しようとしていますが、動作しないようです。最初に私は、行が選択された場合に変更し、ビューの行を選択します。load

open func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { 
    let cell = menuCells[indexPath.row] 

    cell.textLabel?.textColor = UIColor.white.withAlphaComponent(0.4) 
} 

open func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    let cell = menuCells[indexPath.row] 

    cell.textLabel?.textColor = UIColor.white.withAlphaComponent(1.0) 

    UIApplication.shared.sendAction(cell.menuAction, to: cell.menuTarget, from: cell, for: nil) 
} 

を選択解除し、私は行を事前に選択するには、次のことを行うことを試みましたが、変更していないようですの、textColorを定義するために、2つの方法で次のように設定しましたtextColor 1.0〜alpha

open func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 

    if indexPath.row == 0 { 
     cell.setSelected(true, animated: false) 
    } 
} 



menuView.selectRow(at: IndexPath(row: 0, section: 0), animated: false, scrollPosition: UITableViewScrollPosition.none) 

答えて

2

didSelectRowAt :)メッセージ

https://developer.apple.com/reference/uikit/uitableview/1614875-selectrowatindexpath


したがって、viewDid

class ViewController: UIViewController { 

    @IBOutlet weak var myTableView: UITableView! 


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


    let indexPath = NSIndexPath(forRow: 2, inSection: 0) 

    myTableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: .None) 

    // Calling manually to the delegate method 
    tableView(myTableView, didSelectRowAtIndexPath: indexPath) 
    } 

} 


// UITableView Delegate Implementation 
extension ViewController: UITableViewDataSource, UITableViewDelegate { 

    func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { 
    let cell = tableView.cellForRowAtIndexPath(indexPath) 
    cell?.textLabel?.textColor = UIColor.blackColor() 
    } 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    let cell = tableView.cellForRowAtIndexPath(indexPath) 
    cell?.textLabel?.textColor = UIColor.redColor() 
    cell?.textLabel?.backgroundColor = UIColor.clearColor() 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 10 
    } 

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

    cell.textLabel?.text = String(indexPath.row) 

    return cell 
    } 
} 

結果:

image

1

これはあなたの後ですか?

はのtableView(を受け取るためにデリゲートを起こさないselectRowAtIndexPathを呼び出す:willSelectRowAt :)かのtableView(:Appleのドキュメントよると

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

     let iPath = NSIndexPath(forRow: 0, inSection: 0) 
     tableView.selectRowAtIndexPath(iPath, animated: true, scrollPosition: .Top) 
    } 
0

tableView(_:didSelectRowAt:)UITableViewインスタンスを持つユーザーとの対話を処理するために設計されたデリゲートプロトコルの一部であることを忘れないでください、あなたが手動で呼び出す必要があります方法は、以下の例のようにdidSelectRowAtIndexPath表示されます。したがって、ユーザーのやりとりに応じて呼び出され、プログラム的な変更は呼び出されません。

これを解決するには、外観の変更をapplyAppearanceSelectedという別の関数に移動し、プログラムによる変更を行うときにこれを呼び出し、tableView(_:didSelectRowAt:)から呼び出すと、ユーザーの変更が処理されます。 ユーザがセルを選択した場合にのみ適用される他のコードを追加したい場合があるので、tableView(_:didSelectRowAt:)を直接呼び出すことはお勧めしません。

0

viewWillAppearメソッドの行を選択すると、ビューが完全に初期化されている必要があります。

以下の例では、最初のセクションの最初のセルを選択:

override func viewWillAppear(_ animated: Bool) { 
    let selectedIndexPath = IndexPath(row: 0, section: 0) 
    tableView.selectRow(at: selectedIndexPath, animated: true, scrollPosition: UITableViewScrollPosition.none) 
    tableView(tableView, didSelectRowAt: selectedIndexPath) 
} 
関連する問題