2016-09-21 3 views
0

Meine Idee ist, wenn diese Ansicht erscheint. Ich möchte es nach Ort erkennen lassen. Also wenn Benutzer in Singapur. Singapore Cell wird hervorgehoben. Gibt es einen Weg, dies zu ermöglichen? Ich habe diese Funktion sehen, aber wusste nicht, wie esMarkieren, wenn viewDidAppear

didHighlightRowAtIndexPath

Hier zu verwenden ist The View enter image description here

Codierung:

class LanguageViewController: BaseViewController, UITableViewDelegate, UITableViewDataSource, CLLocationManagerDelegate { 

    @IBOutlet weak var optionsTableView: UITableView! 

    let titleArr = ["About", "Terms", "Privacy Policy", "Reset Password", "Change Language", "Logout"] 

    var countryList = [AnyObject]() 
    var languageList = [AnyObject]() 
    var selectRow = Int() 
    var selectLanguage = Int() 
    var isSelect = Bool() 
    var tempCountry = [String]() 
    var tempLanguage = [String]() 
    var countryLbl : String = "Country" 
    var languageLbl : String = "Language" 

    // MARK: - Activity Lifecycle 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     setupWhiteLeftButton() 
     //self.navigationItem.title = "" 

     for country in countryList{ 
      tempCountry.append(country["CountryName"] as! String) 
     } 
     // Do any additional setup after loading the view. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func tableView(tableView: UITableView, didHighlightRowAtIndexPath indexPath: NSIndexPath) { 
     indexPath.row == 0 
    } 

    // MARK: - UITableViewData Source & Delegate 


    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return tempCountry.count 
    } 

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
     return 40 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     optionsTableView.backgroundColor = UIColor.redColor() 
     let cell = optionsTableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCountryTableViewCell 
     cell.backgroundColor = UIColor.redColor() 
     cell.titleLbl.text = tempCountry[indexPath.row] 
     return cell 
    } 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     if indexPath.row == 0 { 
//   let storyboard2 = UIStoryboard(name: "Profile", bundle: nil) 
//   let AboutVC = storyboard2.instantiateViewControllerWithIdentifier("AboutVC") as! AboutViewController 
//   self.navigationController?.pushViewController(AboutVC, animated: true) 
//   optionsTableView.deselectRowAtIndexPath(indexPath, animated: true) 
     } 
    } 
} 
+0

http://stackoverflow.com/questions/2035061/select-tableview-row-programmatically – Azzaknight

Antwort

0

eine Variable nehmen zu speichern Index des aktuellen Standorts

var loctionIndex: Int = -1 

Update loctionIndex in Ortsaktualisierungs In Standort upadate Verfahren einreihige tableview nachladen verwenden, die loctionIndex als

if loctionIndex != -1 { 
    let indexPath = NSIndexPath(forRow: loctionIndex, inSection: 0) 
    tableview.reloadRowsAtIndexPaths([indexPath], withRowAnimation:UITableViewRowAnimation.None) 
} 

hier schreiben einige Logik in Zelle für Zeile am Index-Methode

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    optionsTableView.backgroundColor = UIColor.redColor() 
    let cell = optionsTableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCountryTableViewCell 
    if loctionIndex == indexPath.row { 
    cell.backgroundColor = UIColor.greenColor() 
} else { 
    cell.backgroundColor = UIColor.redColor() 
    } 
    cell.titleLbl.text = tempCountry[indexPath.row] 
    return cell 
} 
+0

ich habe meine neue Codierung Kommentar . Scheint, dass ich es nicht bekomme, wie man ur Code oben für meine Situation benutzt. –

0
folgt

Ermitteln Sie zuerst das Land mit diesem Code:

Lassen Sie currentLocale = NSLocale.currentLocale() lost countryCode = currentLocale.objectForKey (NSLocaleCountryCode) als? String

als das Land Zeichenfolge verglichen, wenn beide gleich sind als hightlight dass perticular Zelle ..

+0

Ja, ich kann die Logik genau dort sehen. aber ich weiß nicht, wie man diese bestimmte Zelle hervorhebt. Welche Funktion sollte ich verwenden? –

+0

- (BOOL) tableView: (UITableView *) tableView sollteHighlightRowAtIndexPath: (NSIndexPath *) indexPath { Rückgabe YES; } - (void) Tabellenansicht: (UITableView *) tableView didHighlightRowAtIndexPath: (NSIndexPath *) indexPath { // etwas hier tun } –

Verwandte Themen