试图找出一种使用单个 UITableview 的不那么笨拙的方法
Trying to figure out a less clunky way of utilising a single UITableview
我有一个 UITableViewController 连接到一个 UIViewController。
我做了一个全局 counter
来跟踪 UITableView 中的哪一行被选中。因为根据选择的行,呈现的 UIViewController 中的一些信息会发生变化。
我认为可以对其进行整理,以便 dayx
segue 只需调用一次,而 counter
可以根据选择的行进行相应更改?但是我很难弄明白。
这是我目前拥有的,它可以工作但看起来很乱?:
//what happens when row is selected
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
counter = 0
self.performSegue(withIdentifier: "dayx", sender: self)
} else if indexPath.row == 1 {
counter = 1
self.performSegue(withIdentifier: "dayx", sender: self)
}
else if indexPath.row == 2 {
counter = 2
self.performSegue(withIdentifier: "dayx", sender: self)
}
else if indexPath.row == 3 {
counter = 3
self.performSegue(withIdentifier: "dayx", sender: self)
}
else if indexPath.row == 4 {
counter = 4
self.performSegue(withIdentifier: "dayx", sender: self)
}
else if indexPath.row == 5 {
counter = 5
self.performSegue(withIdentifier: "dayx", sender: self)
}
else if indexPath.row == 6 {
counter = 6
self.performSegue(withIdentifier: "dayx", sender: self)
}
}
使用flatMap
:
Swift 4.0
indexPath.flatMap {
print([=10=])
counter = [=10=]
self.performSegue(withIdentifier: "dayx", sender: self)
}
之后:
let _ = indexPath.compactMap {
counter = [=11=]
self.performSegue(withIdentifier: "dayx", sender: self)
}
了解 flatMap
的基础知识。阅读这篇文章Replacing flatMap With compactMap
更新
如果你所有的行都打算做一个performSegue
。简单:
//what happens when row is selected
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
counter = indexPath.row
self.performSegue(withIdentifier: "dayx", sender: self)
}
我有一个 UITableViewController 连接到一个 UIViewController。
我做了一个全局 counter
来跟踪 UITableView 中的哪一行被选中。因为根据选择的行,呈现的 UIViewController 中的一些信息会发生变化。
我认为可以对其进行整理,以便 dayx
segue 只需调用一次,而 counter
可以根据选择的行进行相应更改?但是我很难弄明白。
这是我目前拥有的,它可以工作但看起来很乱?:
//what happens when row is selected
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
counter = 0
self.performSegue(withIdentifier: "dayx", sender: self)
} else if indexPath.row == 1 {
counter = 1
self.performSegue(withIdentifier: "dayx", sender: self)
}
else if indexPath.row == 2 {
counter = 2
self.performSegue(withIdentifier: "dayx", sender: self)
}
else if indexPath.row == 3 {
counter = 3
self.performSegue(withIdentifier: "dayx", sender: self)
}
else if indexPath.row == 4 {
counter = 4
self.performSegue(withIdentifier: "dayx", sender: self)
}
else if indexPath.row == 5 {
counter = 5
self.performSegue(withIdentifier: "dayx", sender: self)
}
else if indexPath.row == 6 {
counter = 6
self.performSegue(withIdentifier: "dayx", sender: self)
}
}
使用flatMap
:
Swift 4.0
indexPath.flatMap {
print([=10=])
counter = [=10=]
self.performSegue(withIdentifier: "dayx", sender: self)
}
之后:
let _ = indexPath.compactMap {
counter = [=11=]
self.performSegue(withIdentifier: "dayx", sender: self)
}
了解 flatMap
的基础知识。阅读这篇文章Replacing flatMap With compactMap
更新
如果你所有的行都打算做一个performSegue
。简单:
//what happens when row is selected
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
counter = indexPath.row
self.performSegue(withIdentifier: "dayx", sender: self)
}