在moveRowAt

时间:2018-02-19 03:54:13

标签: ios swift tableview didselectrowatindexpath

我有一个TableView,其中每一行都有一个segue到另一个Storyboard。 我希望用户可以根据需要重新排序行。 但是这会将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的新功能。哪个会使segue崩溃。我现在明白了,问题在于转换。我必须跟踪指数。

修改 在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:...

    }



}

1 个答案:

答案 0 :(得分:0)

我认为你留下了代码的一些重要部分,这些代码负责你所拥有的混乱。例如,为什么你在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:  

编辑:将完整代码添加到地图语言中

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")