首次出现时显示选定的项目单元格
Show Selected Item Cell At First Appearance
我正在尝试使用不同的按钮图像显示默认选定的单元格。但是当我显示语言页面时无法显示它。我正在尝试检查它是否被选中。如果选中则调用 setupSelected,如果未选中则调用设置函数。
但是当我选择语言时,我总是看到我的 tableview 没有选择。 setSelected 效果很好,选中的单元格的图像正在改变。
语言
enum Language: String, CaseIterable {
case tr = "tr"
case eng = "en"
case az = "az"
var title: String {
switch self {
case .tr: return "settings.language.turkish".localized
case .eng: return "settings.language.english".localized
case .az: return "settings.language.azerbeijan".localized
}
}
var code: String {
switch self {
case .tr: return "tr"
case .eng: return "en"
case .az: return "az-AZ"
}
}
}
我的视图模型
final class LanguageViewModel {
var locales: [Language] = [.tr, .eng, .az] //.ar
var currentLanguage = UserDefaultsUtil.getDeviceLanguage()
var languages: [LanguageModel] = []
func getLanguageTitles() {
languages = [
LanguageModel(language: .tr, isSelected: currentLanguage == locales[0].code ? true : false),
LanguageModel(language: .eng, isSelected: currentLanguage == locales[1].code ? true : false),
LanguageModel(language: .az, isSelected: currentLanguage == locales[2].code ? true : false)
]
}
}
我的模型
struct LanguageModel {
let language: Language
var isSelected: Bool
}
我的ViewController
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return languageCell(indexPath: indexPath)
}
private func languageCell(indexPath: IndexPath) -> LanguageTableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: LanguageTableViewCell.identifier, for: indexPath) as! LanguageTableViewCell
if cell.isSelected {
cell.setupSelected(languageTitles[indexPath.row])
return cell
} else {
cell.setup(languageTitles[indexPath.row])
return cell
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: LanguageTableViewCell.identifier, for: indexPath) as! LanguageTableViewCell
tableView.deselectRow(at: indexPath, animated: true)
let item = languageTitles[indexPath.row]
cell.setupSelected(item)
tableView.reloadData()
}
我的手机
@IBOutlet weak var empty: UIImageView!
@IBOutlet weak var title: UILabel!
var currentLanguage = UserDefaultsUtil.getDeviceLanguage()
static let identifier = String(describing: LanguageTableViewCell.self)
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
func setup(_ languageTitles: LanguageModel) {
title.label(textStr: languageTitles.language.rawValue, textColor: KSColor.neutral700.getColor(), textFont: UIFont.sfProTextMedium(size: 15), fontSize: 15, lineSpacing: -0.16, paragraphStyle: NSMutableParagraphStyle())
}
func setupSelected(_ languageTitles: LanguageModel) {
setSelected(true, animated: true)
title.label(textStr: languageTitles.language.rawValue, textColor: KSColor.neutral700.getColor(), textFont: UIFont.sfProTextMedium(size: 15), fontSize: 15, lineSpacing: -0.16, paragraphStyle: NSMutableParagraphStyle())
self.empty.image = UIImage(named: "icon_done_bold")
self.empty.makeRounded()
self.empty.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
self.empty.setImageColor(color: KSColor.neutral100.getColor())
self.empty.backgroundColor = KSColor.ocean500Base.getColor()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
if selected {
self.empty.image = UIImage(named: "icon_done_bold")
self.empty.makeRounded()
self.empty.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
self.empty.setImageColor(color: KSColor.neutral100.getColor())
self.empty.backgroundColor = KSColor.ocean500Base.getColor()
} else {
self.empty.transform = CGAffineTransform(scaleX: 1, y: 1)
self.empty.image = UIImage(named: "icon_radio_unselected")
self.empty.setImageColor(color: KSColor.neutral100.getColor())
self.empty.backgroundColor = .white
}
}
}extension LanguageTableViewCell: Reusable, NibLoadable { }
这就是我查看表格视图的方式。其实选的是tr。
你需要
1- 首先删除 ? :
它会得到相同的结果
LanguageModel(language: .tr, isSelected: currentLanguage == locales[0].code),
LanguageModel(language: .eng, isSelected: currentLanguage == locales[1].code),
LanguageModel(language: .az, isSelected: currentLanguage == locales[2].code)
2- 不要依赖单元格的 isSelected
属性,因为滚动时单元格会出列,所以使用它会给你错误的状态,而不是
if cell.isSelected {
cell.setupSelected(languageTitles[indexPath.row])
return cell
} else {
cell.setup(languageTitles[indexPath.row])
return cell
}
做
let item = languageTitles[indexPath.row]
if item.isSelected {
cell.setupSelected(item)
return cell
} else {
cell.setup(item)
return cell
}
3- 删除 override func setSelected(
并将其实现移至 table didSelectRowAt
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let item = languageTitles[indexPath.row]
let value = item.isSelected
// Edit isSelected property and refresh table
languageTitles.forEach { [=13=].isSelected = false }
item.isSelected = !value
tableView.reloadData()
}
我正在尝试使用不同的按钮图像显示默认选定的单元格。但是当我显示语言页面时无法显示它。我正在尝试检查它是否被选中。如果选中则调用 setupSelected,如果未选中则调用设置函数。
但是当我选择语言时,我总是看到我的 tableview 没有选择。 setSelected 效果很好,选中的单元格的图像正在改变。
语言
enum Language: String, CaseIterable {
case tr = "tr"
case eng = "en"
case az = "az"
var title: String {
switch self {
case .tr: return "settings.language.turkish".localized
case .eng: return "settings.language.english".localized
case .az: return "settings.language.azerbeijan".localized
}
}
var code: String {
switch self {
case .tr: return "tr"
case .eng: return "en"
case .az: return "az-AZ"
}
}
}
我的视图模型
final class LanguageViewModel {
var locales: [Language] = [.tr, .eng, .az] //.ar
var currentLanguage = UserDefaultsUtil.getDeviceLanguage()
var languages: [LanguageModel] = []
func getLanguageTitles() {
languages = [
LanguageModel(language: .tr, isSelected: currentLanguage == locales[0].code ? true : false),
LanguageModel(language: .eng, isSelected: currentLanguage == locales[1].code ? true : false),
LanguageModel(language: .az, isSelected: currentLanguage == locales[2].code ? true : false)
]
}
}
我的模型
struct LanguageModel {
let language: Language
var isSelected: Bool
}
我的ViewController
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return languageCell(indexPath: indexPath)
}
private func languageCell(indexPath: IndexPath) -> LanguageTableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: LanguageTableViewCell.identifier, for: indexPath) as! LanguageTableViewCell
if cell.isSelected {
cell.setupSelected(languageTitles[indexPath.row])
return cell
} else {
cell.setup(languageTitles[indexPath.row])
return cell
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: LanguageTableViewCell.identifier, for: indexPath) as! LanguageTableViewCell
tableView.deselectRow(at: indexPath, animated: true)
let item = languageTitles[indexPath.row]
cell.setupSelected(item)
tableView.reloadData()
}
我的手机
@IBOutlet weak var empty: UIImageView!
@IBOutlet weak var title: UILabel!
var currentLanguage = UserDefaultsUtil.getDeviceLanguage()
static let identifier = String(describing: LanguageTableViewCell.self)
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
func setup(_ languageTitles: LanguageModel) {
title.label(textStr: languageTitles.language.rawValue, textColor: KSColor.neutral700.getColor(), textFont: UIFont.sfProTextMedium(size: 15), fontSize: 15, lineSpacing: -0.16, paragraphStyle: NSMutableParagraphStyle())
}
func setupSelected(_ languageTitles: LanguageModel) {
setSelected(true, animated: true)
title.label(textStr: languageTitles.language.rawValue, textColor: KSColor.neutral700.getColor(), textFont: UIFont.sfProTextMedium(size: 15), fontSize: 15, lineSpacing: -0.16, paragraphStyle: NSMutableParagraphStyle())
self.empty.image = UIImage(named: "icon_done_bold")
self.empty.makeRounded()
self.empty.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
self.empty.setImageColor(color: KSColor.neutral100.getColor())
self.empty.backgroundColor = KSColor.ocean500Base.getColor()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
if selected {
self.empty.image = UIImage(named: "icon_done_bold")
self.empty.makeRounded()
self.empty.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
self.empty.setImageColor(color: KSColor.neutral100.getColor())
self.empty.backgroundColor = KSColor.ocean500Base.getColor()
} else {
self.empty.transform = CGAffineTransform(scaleX: 1, y: 1)
self.empty.image = UIImage(named: "icon_radio_unselected")
self.empty.setImageColor(color: KSColor.neutral100.getColor())
self.empty.backgroundColor = .white
}
}
}extension LanguageTableViewCell: Reusable, NibLoadable { }
这就是我查看表格视图的方式。其实选的是tr。
你需要
1- 首先删除 ? :
它会得到相同的结果
LanguageModel(language: .tr, isSelected: currentLanguage == locales[0].code),
LanguageModel(language: .eng, isSelected: currentLanguage == locales[1].code),
LanguageModel(language: .az, isSelected: currentLanguage == locales[2].code)
2- 不要依赖单元格的 isSelected
属性,因为滚动时单元格会出列,所以使用它会给你错误的状态,而不是
if cell.isSelected {
cell.setupSelected(languageTitles[indexPath.row])
return cell
} else {
cell.setup(languageTitles[indexPath.row])
return cell
}
做
let item = languageTitles[indexPath.row]
if item.isSelected {
cell.setupSelected(item)
return cell
} else {
cell.setup(item)
return cell
}
3- 删除 override func setSelected(
并将其实现移至 table didSelectRowAt
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let item = languageTitles[indexPath.row]
let value = item.isSelected
// Edit isSelected property and refresh table
languageTitles.forEach { [=13=].isSelected = false }
item.isSelected = !value
tableView.reloadData()
}