How to pass text from tableViewCell a tableView header on another viewController

huangapple go评论68阅读模式
英文:

How to pass text from tableViewCell a tableView header on another viewController

问题

我无法通过第一个视图控制器中单元格标签的文本传递到第二个视图控制器中表格视图标题的标签。

你可以在附加的截图中看到我得到了什么。

此问题解决后,我觉得我需要处理标题高度(应该根据标签的大小动态调整)。但我认为这应该在SO上提出一个单独的问题。

这是我的FirstVC:

import UIKit

class FirstVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    @IBOutlet weak var firstTableView: UITableView!
    
    var people: [FirstTVCellModel] = []

    override func viewDidLoad() {
        super.viewDidLoad()
    // ... (其余部分未提供)

这是我的Second VC:

import UIKit

class SecondVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    @IBOutlet weak var secondTableView: UITableView!
    
    var stuff = [("Some other stuff 1"),("Some other stuff 2"),("Some other stuff 3")]
    
    var insertionText = FirstTVCellModel(userNameModel: "", dateModel: .none)

    override func viewDidLoad() {
        super.viewDidLoad()
    // ... (其余部分未提供)

How to pass text from tableViewCell a tableView header on another viewController

英文:

I am failing to pass the text from the label in the cell on the FirstVC to a label in the tableView header on the SecondVC.

You can see what I get in the attached screenshot.

Also, once this question is resolved, I feel like I will need to deal with the header height (it should be dynamically adjusted to the size of the label. But I think this should be done in a separate question on SO.

Here's my FirstVC:

import UIKit

class FirstVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    @IBOutlet weak var firstTableView: UITableView!
    
    var people: [FirstTVCellModel] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        firstTableView.delegate = self
        firstTableView.dataSource = self
        
        people = [
            FirstTVCellModel(userNameModel: "This text should appear in the label in the header of the tableView on a SecondVC. Here, let's say it would be a user's name (like Seth Brown). In the real project, the text will be added here from a third static tableView with text fields through the unwindToFirstVC segue. Hope it makes sense", dateModel: Date(timeIntervalSinceNow: -5615416)),
            FirstTVCellModel(userNameModel: "Person 2", dateModel: Date(timeIntervalSinceNow: -5615416)),
            FirstTVCellModel(userNameModel: "Person 3", dateModel: Date(timeIntervalSinceNow: -5615416))
        ]
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        people.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = firstTableView.dequeueReusableCell(withIdentifier: "FirstTVCell", for: indexPath) as! FirstTVCell
        let Person = people[indexPath.row]
        cell.userNameLabel.text = Person.userNameModel
        cell.dateLabel.text = Person.formattedDate
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        firstTableView.deselectRow(at: indexPath, animated: true)
        if let vc = storyboard?.instantiateViewController(withIdentifier: "SecondVC") as? SecondVC {
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
}

And this is my Second VC:

import UIKit

class SecondVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    @IBOutlet weak var secondTableView: UITableView!
    
    var stuff = [("Some other stuff 1"),("Some other stuff 2"),("Some other stuff 3")]
    
    var insertionText = FirstTVCellModel(userNameModel: "", dateModel: .none)

    override func viewDidLoad() {
        super.viewDidLoad()
        secondTableView.delegate = self
        secondTableView.dataSource = self
        
        let tableHeader = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 80))
        secondTableView.tableHeaderView = tableHeader
        tableHeader.backgroundColor = .systemOrange
        
        let labelOne = UILabel(frame: tableHeader.bounds)
        labelOne.text = "USER:"
        tableHeader.addSubview(labelOne)
        
        labelOne.translatesAutoresizingMaskIntoConstraints = false
        labelOne.centerXAnchor.constraint(equalTo: tableHeader.centerXAnchor).isActive = true
        labelOne.topAnchor.constraint(equalTo: tableHeader.safeAreaLayoutGuide.topAnchor, constant: 11).isActive = true
        
        let labelTwo = UILabel(frame: tableHeader.bounds)
        labelTwo.text = "Inherited text here: \(insertionText)"
        tableHeader.addSubview(labelTwo)
        
        labelTwo.numberOfLines = 0
        labelTwo.textAlignment = .center
        
        labelTwo.translatesAutoresizingMaskIntoConstraints = false
        labelTwo.topAnchor.constraint(equalTo: labelOne.bottomAnchor, constant: 0).isActive = true
        labelTwo.leadingAnchor.constraint(equalTo: tableHeader.layoutMarginsGuide.leadingAnchor, constant: 0).isActive = true
        labelTwo.trailingAnchor.constraint(equalTo: tableHeader.layoutMarginsGuide.trailingAnchor, constant: 0).isActive = true


        
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        stuff.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = secondTableView.dequeueReusableCell(withIdentifier: "SecondTVCell", for: indexPath) as! SecondTVCell
        cell.someOtherStuffLabel.text = stuff[indexPath.row]
        return cell
    }
}

How to pass text from tableViewCell a tableView header on another viewController

答案1

得分: 0

你需要将想要的数值注入目标 VC 中。

因此,在第二个 VC 中,你需要有一个地方来保存那个文本,例如:

class SecondVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
  var title: String
  //...
}

然后,在你第一个 VC 的 tableView(didSelectRowAt:) 方法中,从数据源获取文本并将其注入到 VC 2 中。你可以使用 indexPath.row 值来获取适当的数据条目。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    firstTableView.deselectRow(at: indexPath, animated: true)
    if let vc = storyboard?.instantiateViewController(withIdentifier: "SecondVC") as? SecondVC {
       vc.title = people[indexPath.row].userNameModel
       self.navigationController?.pushViewController(vc, animated: true)
    }
}

然后,当设置 VC2 时,你只需使用 title 属性。

英文:

You need to inject the value you want into the destination VC.

So you will need somewhere in the second VC to hold that text, for example

class SecondVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
  var title: String
  //...
}

Then in the your first VC's tableView(didSelectRowAt:) get the text from your data source and inject it into VC 2. You can use the indexPath's .row value to get the appropriate data entry.

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    firstTableView.deselectRow(at: indexPath, animated: true)
    if let vc = storyboard?.instantiateViewController(withIdentifier: "SecondVC") as? SecondVC {
       vc.title = people[indexPath.row].userNameModel
       self.navigationController?.pushViewController(vc, animated: true)
    }
}

then you can just use the title property when setting up VC2.

huangapple
  • 本文由 发表于 2023年5月6日 21:47:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189230.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定