[英]Move Text from cell to text label in a different view controller
I have created an app that displays 10 random numbers in a table view. 我创建了一个在表格视图中显示10个随机数的应用。 This is the code I used
这是我使用的代码
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ShowNumber", for: indexPath)
// Configure the cell...
cell.textLabel?.text = "\(indexPath.row + 1): \(Int(arc4random_uniform(10001)))"
return cell
}
Now I want to be able to click on one cell and be taken to another view controller and in the center of the page it displays the random number. 现在,我希望能够单击一个单元格并转到另一个视图控制器,并且在页面中央显示随机数。 I have the view controller all set up and linked to the original table view.
我已经设置了所有视图控制器,并将其链接到原始表视图。 I'm just having trouble passing my data through the segue.
我只是无法通过segue传递数据。 This is what I have so far.
到目前为止,这就是我所拥有的。 I know it unfinished, I just don't know what to do.
我知道它还没有完成,我只是不知道该怎么办。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showRandomNumber" {
let controller = segue.destination as! ShowNumberTableViewController
let selectedRow = (tableView.indexPathForSelectedRow as NSIndexPath?)?.row ?? 0
controller.LabelText =
}
The LabelText is the name of the label that I want to pass the data to, but I don't know how to LabelText是我要将数据传递到的标签的名称,但是我不知道如何
Using the indexPathForSelectedRow
, you can get a reference to the cell that triggered the segue. 使用
indexPathForSelectedRow
,您可以获得对触发segue的单元格的引用。 You can then access the textLabel within that cell, get its value, and pass it to the next view controller. 然后,您可以访问该单元格中的textLabel,获取其值,并将其传递给下一个视图控制器。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showRandomNumber" {
let controller = segue.destination as! ShowNumberTableViewController
if let indexPath = tableView.indexPathForSelectedRow {
let cell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell
if let textToPass = cell.textLabel.text {
controller.labelText = textToPass
}
}
}
}
Then in ShowNumberTableViewController
you want a var labelText
and in your viewDidLoad()
you can assign it to the textLabel in that view controller with 然后在
ShowNumberTableViewController
您需要一个var labelText
然后在viewDidLoad()
中,将其分配给该视图控制器中的textLabel
centerLabel.text = labelText
The way you are doing, the numbers will change when you scroll the table view. 滚动表格视图时,数字将更改。 I think you should save the numbers in an array.
我认为您应该将数字保存在数组中。
About how to grab the selected number in the prepareForSegue:
I would do this way: 关于如何在
prepareForSegue:
获取选定的号码prepareForSegue:
我可以这样做:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showRandomNumber" {
if let cell = sender as? UITableViewCell, indexPath = tableView.indexPathForCell(cell) {
let selectedNumber = numbers[indexPath.row]
controller.labelText = String(selectedNumber)
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.