2016-09-03 5 views
0

Ich versuche, eine Tabellenzeile vorzuwählen, wenn die Ansicht laden, aber ich habe mehrere Methoden ausprobiert, aber keine scheint zu arbeiten. Zuerst habe ich folgenden zwei Verfahren festgelegt, um die Textfarbe Änderungen zu definieren, wenn eine Zeile ausgewählt und abgewähltWählen Sie eine Zeile bei Ansicht laden

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

Dann habe ich versucht, folgende Dinge zu tun, um eine Reihe vorgewählt werden, aber es scheint nicht zu ändern die textColor mit alpha zu 1.0

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) 

Antwort

2

Nach Apples Dokumentation:

Aufruf selectRowAtIndexPath hat den Delegaten nicht dazu führen, eine Tableview (zu erhalten: willSelectRowAt :) oder Tableview (: didSelectRowAt :) Nachricht

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


Also in ViewDid Appear Methode sollten Sie manuell aufrufen DidSelectRowAtIndexPath wie im Beispiel unten:

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

Ergebnis:

image

1

Ist das, wonach Sie suchen?

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

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

Denken Sie daran, dass tableView(_:didSelectRowAt:) Teil eines Delegierten-Protokoll ist entworfen Benutzer-Interaktionen mit dem UITableView Instanz zu behandeln. Es wird daher als Reaktion auf Benutzerinteraktion aufgerufen, nicht als programmatische Änderung.

Um dieses Problem zu lösen, verschieben Sie Ihre Änderungen in eine separate Funktion wie etwa applyAppearanceSelected und rufen Sie diese bei programmgesteuerten Änderungen auf, und rufen Sie sie auch von tableView(_:didSelectRowAt:), um benutzerinitiierte Änderungen zu behandeln. Ich würde nicht empfehlen, tableView(_:didSelectRowAt:) direkt selbst aufzurufen, weil Sie möglicherweise anderen Code dort hinzufügen möchten, der nur gilt, wenn der Benutzer eine Zelle auswählt.

0

Ich denke, es ist am besten, die Zeile in der viewWillAppear Methode auszuwählen, da die Ansicht vollständig initialisiert worden sein muss.

Das folgende Beispiel die erste Zelle des ersten Abschnitts auswählt:

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