在 moveRowAt 之后找到 didSelectRow 的正确行
Find the correct row for didSelectRow after moveRowAt
我有一个 TableView,其中每一行都有一个到另一个 Storyboard 的 segue。
我希望用户可以根据需要对行重新排序。
但这会使 segues 崩溃到其他故事板。如何将 "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: , index: [=11=]) }
} else {
let languages = ["Español1", "Español2", "English1", "English2", "Deutsch1", "Deutsch1"]
self.languages = languages.enumerated().map { (name: , index: [=11=]) }
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 { [=11=].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: , index: [=12=]) }
cell.textLabel?.text = self.languages[indexPath.row].name
switch(self.languages[indexPath.row].index){
case 0:
编辑:将完整代码添加到地图语言:
func initializelanguageList() {
if let defaults = self.languageStore?.array(forKey: "languages") as? [String] {
self.languages = defaults.enumerated.map { (name: , index: [=13=]) }
} else {
let languages = ["Español", "Español", "English", "English", "Deutsch", "Deutsch"]
self.languages = languages.enumerated.map { (name: , index: [=13=]) }
self.languageStore?.set(languages, forKey: "languages")
}
}
或者,如果在任何情况下您需要将它们转换回来,您可以这样做:
self.languageStore?.set(self.languages.map { [=14=].name }, forKey: "languages")
我有一个 TableView,其中每一行都有一个到另一个 Storyboard 的 segue。 我希望用户可以根据需要对行重新排序。 但这会使 segues 崩溃到其他故事板。如何将 "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: , index: [=11=]) }
} else {
let languages = ["Español1", "Español2", "English1", "English2", "Deutsch1", "Deutsch1"]
self.languages = languages.enumerated().map { (name: , index: [=11=]) }
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 { [=11=].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: , index: [=12=]) }
cell.textLabel?.text = self.languages[indexPath.row].name
switch(self.languages[indexPath.row].index){
case 0:
编辑:将完整代码添加到地图语言:
func initializelanguageList() {
if let defaults = self.languageStore?.array(forKey: "languages") as? [String] {
self.languages = defaults.enumerated.map { (name: , index: [=13=]) }
} else {
let languages = ["Español", "Español", "English", "English", "Deutsch", "Deutsch"]
self.languages = languages.enumerated.map { (name: , index: [=13=]) }
self.languageStore?.set(languages, forKey: "languages")
}
}
或者,如果在任何情况下您需要将它们转换回来,您可以这样做:
self.languageStore?.set(self.languages.map { [=14=].name }, forKey: "languages")