gpt4 book ai didi

swift - Swift 的 TableView 中最多有一个复选标记

转载 作者:行者123 更新时间:2023-11-28 15:29:51 26 4
gpt4 key购买 nike

我希望用户最多可以选择一种声音。复选标记会跳转到您点击的位置并取消选择另一个。

看起来很简单,但我没有看到解决方案。我在互联网上找不到答案。

请问有人能帮帮我吗?

提前致谢!

import UIKit
import AVFoundation

class voicesTableViewController: UITableViewController {

fileprivate let synthesizer = AVSpeechSynthesizer()
fileprivate var speechVoices = AVSpeechSynthesisVoice.speechVoices()


override func viewDidLoad() {
super.viewDidLoad()

}


// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return speechVoices.count
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

//Name
let voice = speechVoices[indexPath.row]
let voiceLang = voice.language as? String
let theVoice = UserDefaults.standard.object(forKey:"voice") as? String

cell.textLabel?.text = voice.name


// Language
if let language = countryName(countryCode: voice.language) {
cell.detailTextLabel?.text = "\(language)"
}
else {
cell.detailTextLabel?.text = ""
}

cell.detailTextLabel?.textColor = UIColor.gray

// Checkmark

if (theVoice != nil) {
if(theVoice == voiceLang) {
cell.accessoryType = UITableViewCellAccessoryType.checkmark
}
}


return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

let voice = speechVoices[indexPath.row]

if tableView.cellForRow(at: indexPath)?.accessoryType == UITableViewCellAccessoryType.checkmark {
tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.none
}
else
{
//if ((tableView.indexPathsForSelectedRows?.count)! > 1) {
tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark
//}
}

UserDefaults.standard.set(voice.language, forKey:"voice")
UserDefaults.standard.synchronize()


tableView.deselectRow(at: indexPath, animated: true)
}

func countryName(countryCode: String) -> String? {
let preferredLanguage = NSLocale.preferredLanguages[0] as String
let current = Locale(identifier: preferredLanguage)
return current.localizedString(forLanguageCode: countryCode) ?? nil

//return current.localizedString(forIdentifier: indentifier) ? nil
}


}

最佳答案

函数 cellForRow:atIndexPath 的简单更改应该有效:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

//Name
let voice = speechVoices[indexPath.row]
let voiceLang = voice.language as? String
let theVoice = UserDefaults.standard.object(forKey:"voice") as? String

cell.textLabel?.text = voice.name


// Language
if let language = countryName(countryCode: voice.language) {
cell.detailTextLabel?.text = "\(language)"
}
else {
cell.detailTextLabel?.text = ""
}

cell.detailTextLabel?.textColor = UIColor.gray

// Checkmark

if(theVoice != nil && theVoice == voiceLang) {
cell.accessoryType = .checkmark
} else {
cell.accessoryType = .none
}

return cell
}

UPD#1

但是你可以使用更好的解决方案:1) 添加属性 fileprivate var selectedIndexPath: IndexPath?

2) 将函数 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) 更改为下一个:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

//Name
let voice = speechVoices[indexPath.row]
let voiceLang = voice.language as? String
let theVoice = UserDefaults.standard.object(forKey:"voice") as? String

cell.textLabel?.text = voice.name


// Language
if let language = countryName(countryCode: voice.language) {
cell.detailTextLabel?.text = "\(language)"
}
else {
cell.detailTextLabel?.text = ""
}

cell.detailTextLabel?.textColor = UIColor.gray

// Checkmark
cell.accessoryType = self.selectedIndexPath == indexPath ? .checkmark : .none
return cell
}

3) 然后在 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 中你可以做下一步:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

let voice = speechVoices[indexPath.row]

self.selectedIndexPath = indexPath
UserDefaults.standard.set(voice.language, forKey:"voice")
UserDefaults.standard.synchronize()


tableView.deselectRow(at: indexPath, animated: true)
tableView.reloadRows(at: [indexPath], with: .automatic)
}

关于swift - Swift 的 TableView 中最多有一个复选标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44834103/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com