我有以下设置tableView:
我想从每个单元格(如公开所示)执行到另一个视图控制器的segue。问题是我不想制作六个不同的视图控制器,特别是因为它们中的许多几乎完全相同,包含一个或两个文本字段和一个标签。我有什么方法可以制作一个视图控制器并在单击哪个单元格时更改它?
答案 0 :(得分:1)
问题是我不想制作六个不同的视图控制器, 特别是因为它们中的许多几乎相同,包含一个 或两个文本字段和一个标签。
这绝对是真的。您可以通过添加只有一个segue 来实现这一点 - 无需添加六个不同的segue - 从设置ViewController到下一个(详情ViewController);根据选择了哪个部分的行 - 您可以执行segue并发送所需的数据。
1-添加Segue:
您需要将设置ViewController中的segue添加到ViewController的详细信息中。 确保已从设置ViewController本身连接segue,但不来自任何表格视图单元格。在故事板上添加segue后,您需要为它添加一个标识符,我将在我的代码片段示例中调用它 - " toDetails"。
如果您不知道如何为segue添加标识符,则可能需要检查this answer。
2-发送所需数据:
出于简化的目的,我假设您要发送的数据只是一个字符串变量,在我的代码片段示例中称为-dataToSend
。
如下:
设置ViewController:
class SettingsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
//...
private var dataToSend = ""
//...
//...
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "toDetails", sender: self)
}
//...
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// tableView should be connected as an IBOutlet
guard let selectedIndexPath = tableView.indexPathForSelectedRow else {
print("Something went wrong when selected a row!")
return
}
if selectedIndexPath.section == 0 { // Account Section
if selectedIndexPath.row == 0 { // Name Row
dataToSend = "name"
} else if selectedIndexPath.row == 1 { // School Row
dataToSend = "school"
} else if selectedIndexPath.row == 2 { // Grade Row
dataToSend = "grade"
}
} else if selectedIndexPath.section == 1 { // Private Section
if selectedIndexPath.row == 0 { // Email Row
dataToSend = "email"
} else if selectedIndexPath.row == 1 { // Password Row
dataToSend = "password"
} else if selectedIndexPath.row == 2 { // Phone Number Row
dataToSend = "phone"
}
}
let detailsViewController = segue.destination as! DetailsViewController
detailsViewController.receivedData = dataToSend
}
//...
}
进一步:
使用 enums 而不是检查行号时,这是一个很好的做法,这会导致代码更易读:
enum Sections:Int {
case
account = 0,
privacy = 1
}
enum AccountRows:Int {
case
name = 0,
school = 1,
grade = 2
}
enum PrivacyRows:Int {
case
email = 0,
password = 1,
phone = 2
}
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
//...
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// tableView should be connected as an IBOutlet
guard let selectedIndexPath = tableView.indexPathForSelectedRow else {
print("Something went wrong when selected a row!")
return
}
// tuple of (section, row)
switch (selectedIndexPath.section, selectedIndexPath.row) {
// Accounts Section
case (Sections.account.rawValue, AccountRows.name.rawValue):
dataToSend = "name"
case (Sections.account.rawValue, AccountRows.school.rawValue):
dataToSend = "school"
case (Sections.account.rawValue, AccountRows.grade.rawValue):
dataToSend = "grade"
// Privacy Section
case (Sections.privacy.rawValue, PrivacyRows.email.rawValue):
dataToSend = "email"
case (Sections.privacy.rawValue, PrivacyRows.password.rawValue):
dataToSend = "password"
case (Sections.privacy.rawValue, PrivacyRows.phone.rawValue):
dataToSend = "phone"
default:
print("Something went wrong when checking Section and Rows!")
}
let detailsViewController = segue.destination as! DetailsViewController
detailsViewController.receivedData = dataToSend
}
//...
}
振作起来!希望这有帮助。
答案 1 :(得分:0)
Create a new View Controller i am naming it as "VController"
Create enum in "VController" above the view didload :
public enum VCType:Int {
case name = 1
case school = 2
case grade = 3
}
var selectedVc:VCType!
In viewdidload of "VController":
if selectedVc == .name {
// do whatever you want
}
else if selectedVc == .school {
// do whatever you want
}
else if
..........
..........
Code For First ViewController :
above the view didload :
var selectedType:VController.VCType!
// in didSelectRowAtIndexPath
selectedType = .school // if selecting school
perform segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "mySegue"{
let nextScene = segue.destination as? VController
nextScene?.selectedVc = selectedType
}
}