简体   繁体   English

将文本从单元格移动到另一个视图控制器中的文本标签

[英]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.

相关问题 如何根据表视图单元格的详细文本标签,将segue设置为与表视图控制器不同的视图控制器? - How can I set a segue to a different view controller from a table view controller, based on the detail text label of the table view cell? 在其他视图控制器中更新标签文本 - Update label text in different view controller 如何使用表格视图单元格中的文本并将其显示为父视图控制器中的标签? - How can I use text from a table view cell and display it as a label in a parent view controller? 在最小化的视图控制器中显示自定义单元格标签文本 - Displaying custom cell label text in a minimized view controller 将信息从tableview传递给视图控制器而不是基于indexPathForSelectedRow,而是传递给选定单元格的标签文本? - Pass information from a tableview to a view controller based not on indexPathForSelectedRow, but on label text of cell selected? 如何使用CoreData从选定的集合视图单元格中获取标签文本? - How to Get Label Text from Selected Collection View Cell with CoreData? 使用来自单独的表视图控制器的选定单元格填充文本字段 - Populating a Text Field with a Selected Cell from a Separate Table View Controller 按下视图控制器后标签文本未更改 - Label text not changing after view controller is pushed 从表格单元格中的文本从一个View控制器获取到另一个View控制器 - Getting text from table cell from one View controller to another View controller 表格视图单元格标签文本(列文本)用作链接 - Table View Cell Label text (Column text) use as a link
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM