在viewDidAppear

时间:2016-09-21 08:28:23

标签: ios iphone swift uitableview

我的想法是这个观点出现的时候。我想让它按位置检测。所以如果用户在新加坡。新加坡小组将亮相。有没有办法让它成为可能?我确实看到了这个功能,但不知道如何使用它

  

didHighlightRowAtIndexPath

这是The View enter image description here

编码:

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

2 个答案:

答案 0 :(得分:0)

取一个变量来存储当前位置的索引

var loctionIndex: Int = -1

更新位置更新程序中的loctionIndex 在location upadate方法中,使用该loctionIndex重新加载单行tableview,如下所示

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

在这里写一些逻辑在单元格中用于索引方法的行

 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
}

答案 1 :(得分:0)

首先使用此代码检测国家/地区:

let currentLocale = NSLocale.currentLocale() 让countryCode = currentLocale.objectForKey(NSLocaleCountryCode)为?串

比较国家字符串,如果两者都比相关单元格的高亮相同。