gpt4 book ai didi

ios - 从具有多个部分的 TableView 继续

转载 作者:行者123 更新时间:2023-11-28 06:23:16 24 4
gpt4 key购买 nike

我有以下设置 tableView:

enter image description here

我想执行从每个单元格(如披露所示)到另一个 View Controller 的转接。问题是我不想制作六个不同的 View Controller ,特别是因为它们中的许多几乎是相同的,包含一个或两个文本字段和一个标签。有什么方法可以只制作一个 View Controller 并根据单击的单元格进行更改吗?

最佳答案

The problem is I don't want to make six different view controllers, especially since many of them will be almost identical, containing one or two text fields and a label.

这绝对是真的。您可以通过添加 仅一个 segue 来实现此目的 - 无需添加六个不同的 segues - 从设置 ViewController 到下一个(详细信息 ViewController);根据选择的行(在哪个部分),您可以执行 segue 并发送所需的数据。

1- 添加 Segue:

您需要添加一个从设置 ViewController 到详细信息 ViewController 的 segue。 确保 segue 已从设置 ViewController 本身连接,但不是来自任何表格 View 单元格。在 Storyboard上添加 segue 后,您需要为其添加一个标识符,我将在我的代码片段示例中将其称为“toDetails”。

如果您不知道如何为 segue 添加标识符,您可能需要查看 this answer .

2- 发送所需数据:

为了简化,我假设您要发送的数据只是一个字符串变量,在我的代码片段示例中称为 dataToSend

过程如下:

设置 View Controller :

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
}

//...
}

更进一步:

在处理这种情况时,使用枚举而不是检查行号是一种很好的做法,这样可以提高代码的可读性:

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
}
//...
}

加油!希望这对您有所帮助。

关于ios - 从具有多个部分的 TableView 继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42741881/

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