传递给另一个 UIViewController 的 TableCell 数据 [英] TableCell Data passing to another UIViewController
问题描述
我有一个表,它将 SQL 中的数据填充到 TableCell 中.当我单击一个单元格时,我希望将仅 特定单元格的数据传递给不同的 UIViewController.数据包括字符串.我该怎么做?
I have a Table which populates data from SQL into TableCells. When i click on a Cell, I want the data of only that particular cell to be passed to a different UIViewController. Data includes Strings. How do I go about this?
推荐答案
让未来的搜索者更容易找到这个答案-
Lets make it easy for future searchers to reach this answer-
开始场景-
Table View Controller(嵌入在 UINavigationController 中)- 这是我们打开应用程序时将看到的第一个 ViewController,我们希望他将数据传输到我们的目标视图控制器
Table View Controller (embedded in UINavigationController) - that's the first ViewController we'll see when we'll open the app, we want him to transfer data to our Destination View Controller
目标视图控制器- 这是我们在选择单元格后将到达的视图控制器.
Destination View Controller- that's the ViewController we'll reach after selecting a cell.
您可以在下面的 GIF 中看到,我已经设置了从 TableViewCell 到目标视图控制器的 segue,segue 的标识符是 toDestination
(拖动时看不到 蓝线? 按住 ctrl 这样做)
You can see in the following GIF that I've set a segue from our TableViewCell to Destination View Controller, the segue's identifier is toDestination
(can't see the blue line when dragging? hold ctrl while doing that)
这就是你的故事板场景应该是什么样子-
That's how your storyboard scene should look like-
(不要忘记设置单元格的重用标识符,以便稍后在代码中使用)
(don't forget to set your cell's reuse identifier so you can use it later in code)
现在让我们深入研究代码:
Now lets dive into code:
在我们的 TableViewController 中,我们编写了这个函数:
In our TableViewController we write this function:
// 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 之间设置了一个 segue(还记得起始场景吗?...),现在每次用户按下该单元格时,segue 都会自动启动这个函数正在被调用
When did you even call this function?- I didn't call it, it's being called automatically because I've set a segue between our cell to our DestinationViewController using our storyboard (remember the starting scene?...), now every time the user presses that cell a segue starts automatically and this function is being called
我们的 DestinationViewController 端看起来像这样-
Our DestinationViewController side looks like that-
class DestinationViewController: UIViewController {
@IBOutlet weak var label: UILabel!
var passedString: String!
override func viewDidLoad() {
super.viewDidLoad()
label.text = passedString
}
}
我们完成了,这是最终结果
We're done, here's the final result
完整代码:
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
}
}
这篇关于传递给另一个 UIViewController 的 TableCell 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!