0

My idea is when this view appear. i want to make it detect by location. So if user in Singapore. Singapore Cell will be highlighted. Is there any way to make it possible? I did see this function, but didnt know how to use it

didHighlightRowAtIndexPath

Here is The View enter image description here

Coding:

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)
        }
    }
}
Ayus Salleh
  • 153
  • 1
  • 1
  • 9

2 Answers2

0

take one variable to store Index of current location

var loctionIndex: Int = -1

update loctionIndex in location updater In location upadate method reload single row of tableview using that loctionIndex as follows

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

write here some logic in cell for row at index method

 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
}
Satyanarayana
  • 1,059
  • 6
  • 16
0

first detect the country using this code:

let currentLocale = NSLocale.currentLocale() let countryCode = currentLocale.objectForKey(NSLocaleCountryCode) as? String

than compared the country string if both are same than hightlight that perticular cell..

  • yeah i can see the logic right there. but i dont know how to highlight that particular cell. Which func i should use? – Ayus Salleh Sep 22 '16 at 02:03
  • - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } - (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath { // do something here } – Dhaval raval Sep 22 '16 at 11:10