【问题标题】:Add Information / Details to a TableView row将信息/详细信息添加到 TableView 行
【发布时间】:2019-02-09 17:55:11
【问题描述】:

我有一个 TableView,其中包含第一个 ViewController 上的项目。当我单击一行时,我执行一个 segue 以显示第二个 ViewController,上面有一个 TextField。 现在我想将信息写入 TextField 并将该信息链接到第一个 ViewController 中的选定行。因此,当您单击 row1 时会显示 Information1,当您单击 row2 时会显示 Information2,依此类推。 我不想拥有与 Items 一样多的 ViewController,所以我想知道解决这个问题的最佳方法是什么?

【问题讨论】:

    标签: swift segue viewcontroller


    【解决方案1】:

    在目标视图控制器中声明一个全局 public var content:String?

    在你 parentViewController 的 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 中,在执行 segue 之前写下这个:

    let destVC = YourDestinationVC()
    destVC.content = "content pertaining to cell selected"
    
    // if the segue name is called segueOne
    destinationVC.performSegueWithIdentifier("segueOne", sender: self)
    

    这将使用预加载的数据执行转场

    【讨论】:

      【解决方案2】:

      据我了解,您的问题更多是关于如何设计您的应用以使其易于维护。这是我的建议:

      //1. Create a structure to hold the data
      struct Information{
          let cellTitle : String
          let messageToDisplay : String
      
          init(cellTitle: String, messageToDisplay: String){
              self.cellTitle = cellTitle
              self.messageToDisplay = messageToDisplay
          }
      }
      
      class ViewController: UIViewController {
      
          @IBOutlet weak var tableView: UITableView!
      
          //2. Create a datasource array
          let datasource : [Information] = [Information(cellTitle: "Cell1", messageToDisplay: "Message To Display on for Cell1"),
                                          Information(cellTitle: "Cell2", messageToDisplay: "Message To Display on for Cell2"),
                                          Information(cellTitle: "Cell3", messageToDisplay: "Message To Display on for Cell3")]
      
          override func viewDidLoad() {
              super.viewDidLoad()
          }
      }
      
      extension ViewController : UITableViewDelegate{
          func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
              performSegue(withIdentifier: "ShowSecondViewController", sender: self)
          }
      
          override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
              let indexPath = tableView.indexPathForSelectedRow
      
              if segue.identifier == "ShowSecondViewController" {
                  let vc = segue.destination as! SecondViewController
                  //3.0 Create a variable in your destination Viewcontroller and set it here
                  vc.messageToShow = datasource[(indexPath?.row)!].messageToDisplay
      
              }
          }
      }
      

      【讨论】:

        【解决方案3】:

        您只需要 1 个 vc 作为 segue 的目的地,并使用您从 prepare 方法设置的变量

        let arr = [YourModel(name:"ppp",age:11),YourModel(name:"ppp",age:14)]
        

        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            performSegue(withIdentifier: "YourSegue", sender: arr[indexPath.row])
        }
        
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if segue.identifier == "YourSegue" {
                let des = segue.destination as! DetailVC
                des.item = sender as! YourModel
        
            } 
        }
        

        struct YourModel {
            let name: String
            let age: Int 
        }
        

        class DetailVC:UIViewController { 
           var item:YourModel?
        }
        

        【讨论】:

        • 您能解释一下des.item = sender as! YourModel 行中的“项目”是什么吗?
        • 感谢您的耐心等待。也许我不够清楚,因为我想显示用户输入到 TextView 的信息,而不是数组给出的信息。但是我现在从你们那里得到了基础知识,我现在先自己仔细看看。谢谢。
        猜你喜欢
        • 1970-01-01
        • 2011-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-20
        相关资源
        最近更新 更多