【发布时间】:2019-05-01 11:02:30
【问题描述】:
我有一个将 SQL 中的数据填充到 TableCells 中的表。当我单击一个单元格时,我希望 only 该特定单元格的数据被传递给不同的 UIViewController。数据包括字符串。我该怎么做?
【问题讨论】:
-
你的单元格选择类型是什么?
标签: sql swift uitableview
我有一个将 SQL 中的数据填充到 TableCells 中的表。当我单击一个单元格时,我希望 only 该特定单元格的数据被传递给不同的 UIViewController。数据包括字符串。我该怎么做?
【问题讨论】:
标签: sql swift uitableview
让未来的搜索者更容易找到这个答案-
开始场景-
表格视图控制器(嵌入在 UINavigationController 中) - 这是我们打开应用程序时将看到的第一个 ViewController,我们希望他将数据传输到我们的目标视图控制器
Destination View Controller- 这是我们在选择单元格后将到达的 ViewController。
您可以在下面的 GIF 中看到,我设置了从 TableViewCell 到目标视图控制器的 segue,segue 的标识符是 toDestination(在拖动时看不到 蓝线?在执行此操作时按住 ctrl)
这就是你的故事板场景应该是什么样子-
(不要忘记设置单元格的重用标识符,以便稍后在代码中使用它)
在我们的 TableViewController 中我们编写了这个函数:
// Here we're transfering the data we want to our DestinationViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationVC = segue.destination as? DestinationViewController{
if let pressedIndexPath = tableView.indexPathForSelectedRow{
// passed string is just some variable I've declared on DestinationViewController
destinationVC.passedString = dataHolder[pressedIndexPath.row]
}
}
}
你什么时候调用过这个函数?- 我没有调用它,它是自动调用的,因为我已经使用我们的故事板在我们的单元格和 DestinationViewController 之间设置了一个转场(还记得起始场景吗?...),现在每次用户按下该单元格时,转场都会自动启动并且正在调用此函数
我们的 DestinationViewController 端看起来是这样的-
class DestinationViewController: UIViewController {
@IBOutlet weak var label: UILabel!
var passedString: String!
override func viewDidLoad() {
super.viewDidLoad()
label.text = passedString
}
}
完整代码:
class TableViewController: UITableViewController {
let dataHolder = ["string1", "string2", "string3", "string4"]
override func numberOfSections(in tableView: UITableView) -> Int { return 1 }
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return dataHolder.count }
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// change "cell" to your cell's reuse identifier
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = dataHolder[indexPath.row]
return cell
}
// Here we're transfering the data we want to our DestinationViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationVC = segue.destination as? DestinationViewController{
if let pressedIndexPath = tableView.indexPathForSelectedRow{
destinationVC.passedString = dataHolder[pressedIndexPath.row]
}
}
}
}
另一方面:
class DestinationViewController: UIViewController {
@IBOutlet weak var label: UILabel!
var passedString: String!
override func viewDidLoad() {
super.viewDidLoad()
label.text = passedString
}
}
【讨论】:
您可以像这样创建自定义单元格
class CustomCell: UITableViewCell {
var yourData: CustomData? // You can put your data here as you want, you can also have three different variables for your data
override func awakeFromNib() {
// Initialization code
super.awakeFromNib()
}
}
在函数 cellForRow 之后
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier", for: indexPath) as! CustomCell
// Here you can set data of your cell as you want
cell.yourData = // Put your data
return cell
}
然后你可以使用didSelectRowAt传递你的数据
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedCell: CustomCell = tableView.cellForRow(at: indexPath)!
let yourData = selectedCell.yourData
// Here you can perform a segue or you can present a ViewController and pass your data
....
}
【讨论】: