// // SBTLanguageVCtr.swift // SolarBT // // Created by weclouds on 2019/4/22. // Copyright © 2019 weclouds. All rights reserved. // import UIKit import Localize_Swift class SBTlangueCell: UITableViewCell { var isCheck:Bool? = true{ didSet{ checkImageView.isHidden = !self.isCheck! } } lazy var checkImageView: UIImageView = { let checkImageView = UIImageView(image: UIImage(named: "勾选")) return checkImageView }() override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) addSubview(checkImageView) checkImageView.snp.makeConstraints { (make) in make.centerY.equalToSuperview() make.right.equalToSuperview().offset(-16) make.width.height.equalTo(16) } } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } } let NOTIFICTIONRELOADLANGUAGE = "NOTIFICTIONRELOADLANGUAGE" class SBTLanguageVCtr: UIViewController { let selectedDefaultKey = "selectedDefaultKey" var selectedIndexPath : IndexPath? = IndexPath(row: 0, section: 0) var dataSource = ["Follow_system".da_localizedStr(),"Simplified_Chinese".da_localizedStr(),"English"] lazy var tableView: UITableView = { let tableView = UITableView(frame: CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: KSCREENHEIGHT - KNavBarHeight), style: .plain) tableView.delegate = self tableView.dataSource = self tableView.register(SBTlangueCell.self, forCellReuseIdentifier: "cell") return tableView }() override func viewDidLoad() { super.viewDidLoad() checkTheBlueToothWasActivity() title = "Client_language".da_localizedStr() LocalizationManager.shareInstance().callback = { self.title = "Client_language".da_localizedStr() } view.addSubview(tableView) let defaults = UserDefaults.standard let row = defaults.integer(forKey: selectedDefaultKey) log.debug("userdefaults - \(row)") if row == 0 { selectedIndexPath = IndexPath(row: 0, section: 0) }else if row == 1 { selectedIndexPath = IndexPath(row: 1, section: 0) }else if row == 2 { selectedIndexPath = IndexPath(row: 2, section: 0) }else { selectedIndexPath = IndexPath(row: 0, section: 0) } NotificationCenter.default.addObserver(self, selector: #selector(reloadLanguage), name: NSNotification.Name(NOTIFICTIONRELOADLANGUAGE), object: nil) } @objc func reloadLanguage(){ dataSource = ["Follow_system".da_localizedStr(),"Simplified_Chinese".da_localizedStr(),"English"] title = "Client_language".da_localizedStr() self.tableView.reloadData() } } extension SBTLanguageVCtr:UITableViewDelegate,UITableViewDataSource{ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return dataSource.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { var cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? SBTlangueCell if cell == nil { cell = SBTlangueCell(style: .default, reuseIdentifier: "cell") } cell?.textLabel?.text = dataSource[indexPath.row].da_localizedStr() LocalizationManager.shareInstance().callback = { cell?.textLabel?.text = self.dataSource[indexPath.row].da_localizedStr() } if selectedIndexPath == indexPath { cell?.isCheck = true }else{ cell?.isCheck = false } return cell! } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { // tableView.deselectRow(at: indexPath, animated: true) //取消之前的选择 let celled = tableView.cellForRow(at: selectedIndexPath!) as! SBTlangueCell celled.isCheck = false //记录当前选择位置 selectedIndexPath = indexPath let defaults = UserDefaults.standard defaults.set(indexPath.row, forKey: selectedDefaultKey) //选择当前的打勾 let cell = tableView.cellForRow(at: indexPath) as! SBTlangueCell cell.isCheck = true let userDef = UserDefaults.standard if indexPath.row == 0 { DAConfig.userLanguage = nil userDef.set("0", forKey: "SOLARLANGUAGE") }else if indexPath.row == 1 { userDef.set("1", forKey: "SOLARLANGUAGE") DAConfig.userLanguage = "zh-Hans" }else if indexPath.row == 2 { userDef.set("2", forKey: "SOLARLANGUAGE") DAConfig.userLanguage = "en" } userDef.synchronize() //通知改变文字 NotificationCenter.default.post(name: NSNotification.Name("updateEmptyViewLanguage"), object: nil) NotificationCenter.default.post(name: NSNotification.Name(NOTIFICTIONRELOADLANGUAGE), object: nil) NotificationCenter.default.post(name: NSNotification.Name(NOTIFICTIONCHANGEFEEDBACKURL), object: nil) LocalizationManager.shareInstance().notificationToChangeLanguage() g_showloading() } }