gpt4 book ai didi

ios - 在 moveRowAt 之后查找 didSelectRow 的正确行

转载 作者:行者123 更新时间:2023-11-30 11:45:35 25 4
gpt4 key购买 nike

我有一个 TableView,其中每一行都有一个到另一个 Storyboard 的序列。我希望用户可以按照自己的意愿对行进行重新排序。但这会导致其他 Storyboard的转场崩溃。如何将“case 0: .., case 1:...”连接到最初的indexPath.row?

以下是到目前为止我的代码的核心部分:

let languageStore = UserDefaults(suiteName: "LanguageSelect")
var languages = [String]()

func initializelanguageList() {
if let defaults = self.languageStore?.array(forKey: "languages") as? [String] {
self.languages = defaults
} else {
self.languages = ["Español", "Español", "English", "English", "Deutsch", "Deutsch"]
self.languageStore?.set(self.languages, forKey: "languages")
}
}
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}
override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let language = self.languages.remove(at: sourceIndexPath.row)
self.languages.insert(language, at: destinationIndexPath.row)
self.languageStore?.set(self.languages, forKey: "languages")
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return languages.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath)

cell.textLabel?.text = self.languages[indexPath.row]
return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath as IndexPath, animated: true)
print(indexPath.row)
let row = indexPath.row
switch(row){
case 0:
let ViewController = UIStoryboard(name: "firstSBoard", bundle: nil).instantiateViewController(withIdentifier: "firstTabBar")
self.navigationController?.pushViewController(ViewController, animated: false)

case 1:
let ViewController = UIStoryboard(name: "secondSBoard", bundle: nil).instantiateViewController(withIdentifier: "secondTabBar")
self.navigationController?.pushViewController(ViewController, animated: false)

case 2:...

break
default: break
}
}}

起初我有带有大小写开关的 TableView,它可以工作。但现在我尝试了一些新的东西,比如 CanMoveRow 的可能性。这会破坏segues。我现在明白了,问题出在交换机上。我必须跟踪指数。

编辑在 Matic Oblak 的帮助下解决了:

let languageStore = UserDefaults(suiteName: "LanguageSelect")
var languages = [(name: String, index: Int)]()

override func viewDidLoad() {
super.viewDidLoad()
self.initializelanguageList()
}

func initializelanguageList() {
if let defaults = self.languageStore?.array(forKey: "languages1.0") as? [String] {
self.languages = defaults.enumerated().map { (name: $1, index: $0) }
} else {
let languages = ["Español1", "Español2", "English1", "English2", "Deutsch1", "Deutsch1"]
self.languages = languages.enumerated().map { (name: $1, index: $0) }
self.languageStore?.set(languages, forKey: "languages1.0")
}
}

override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}

override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let language = self.languages.remove(at: sourceIndexPath.row)
self.languages.insert(language, at: destinationIndexPath.row)
self.languageStore?.set(self.languages.map { $0.name }, forKey: "languages1.0")
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return languages.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath)
cell.textLabel?.text = self.languages[indexPath.row].name
return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath as IndexPath, animated: true)

switch(self.languages[indexPath.row].index){
case 0:
let ViewController = UIStoryboard(name: "espanol1SBoard", bundle: nil).instantiateViewController(withIdentifier: "espanol1TabBar")
self.navigationController?.pushViewController(ViewController, animated: false)

case 1:
let ViewController = UIStoryboard(name: "espanol2SBoard", bundle: nil).instantiateViewController(withIdentifier: "espanol2TabBar")
self.navigationController?.pushViewController(ViewController, animated: false)

case 2:...

}



}

最佳答案

我认为您留下了代码中的一些重要部分,这些部分导致了您所遇到的困惑。例如,为什么在 didSelectRowAt 中有一个 switch 语句?它应该如下所示:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath as IndexPath, animated: true)
let ViewController = UIStoryboard(name: "firstSBoard", bundle: nil).instantiateViewController(withIdentifier: "firstTabBar")
ViewController.language = languages[indexPath.row]
self.navigationController?.pushViewController(ViewController, animated: false)
}

正如您在本例中所看到的,您的数组是唯一的事实来源,顺序永远不会影响您的结果。

但是,如果您确实需要跟踪这些索引,您只需更改结构即可:

var languages = [(name: String, index: Int)]()

然后

self.languages = defaults.enumerated.map { (name: $1, index: $0) }

cell.textLabel?.text = self.languages[indexPath.row].name

switch(self.languages[indexPath.row].index){
case 0:

编辑:将完整代码添加到 map 语言:

func initializelanguageList() {
if let defaults = self.languageStore?.array(forKey: "languages") as? [String] {
self.languages = defaults.enumerated.map { (name: $1, index: $0) }
} else {
let languages = ["Español", "Español", "English", "English", "Deutsch", "Deutsch"]
self.languages = languages.enumerated.map { (name: $1, index: $0) }
self.languageStore?.set(languages, forKey: "languages")
}
}

或者,如果在任何情况下您需要将它们转换回来,您会这样做:

self.languageStore?.set(self.languages.map { $0.name }, forKey: "languages")

关于ios - 在 moveRowAt 之后查找 didSelectRow 的正确行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48859327/

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