【问题标题】:Passing data between TableViewControllers.using didSelect and navigationController without storyboard in Swift 3在 Swift 3 中使用 didSelect 和 navigationController 在没有情节提要的情况下在 TableViewControllers 之间传递数据
【发布时间】:2016-11-15 04:05:26
【问题描述】:

我知道如何将数据从 UITableViewController 传递到另一个 ViewController。 *两个控制器都是UITableViewController

例如:

 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as UITableViewCell

    let viewcontroller = NextViewController()
    viewcontroller.someText = "Any text"
    self.navigationController?.pushViewController(viewcontroller, animated: true)
}

当我在 TableViewController 中选择一个单元格时,在下一个视图控制器中,例如一个UILabel,比如 myLabel,被设置,并且一个字符串变量被准备为 someText。并设置为self.myLabel.text = someText。选择行时,当在第二个视图控制器中,将显示“任何文本”。

但是,这不是我想要使用的。我的意思是,我想要实现的目标是:

  override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as UITableViewCell

//In this case, the next view controller is UITableView.
let viewcontroller = NextViewController()

// I want a selected cell to lead to contain an array of String, 
//so that next view controller will hold multiple cells.
// which are only unique to each cell in first TableViewController. 

viewcontroller.someArray = array????
self.navigationController?.pushViewController(viewcontroller, animated: true)
}

更新

在我的 SecondTableViewCOntroller 中

有一个 nvaigationBar 和单元格是通过在 UIAlertAction 中添加一个UITextField 来生成的。这些数据使用“table2”保存在 UserDefaults.standfard 中。还有一个变量,一个String集合的数组;

  //Gloabally Declared
  var myArray = [String](); 
  var array = [String]();

  //This function is displayed in didViewLoad()
  func data(){
    if let myArray = savedata.stringArray(forKey: KEY) {
       array = myArray
    }
 }


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as UITableViewCell

    for object in cell.contentView.subviews
    {
        object.removeFromSuperview();
    }
    let getData = UserDefaults.standard.stringArray(forKey:"table2")

    cell.textLabel?.text = getData[indexPath.row] 
    cell.textLabel?.font = UIFont(name: familyFont, size: 19)
    cell.textLabel?.font = UIFont.systemFont(ofSize: 19)
    cell.textLabel?.textColor = UIColor.darkGray
    cell.accessoryType = .disclosureIndicator
    cell.textLabel?.textAlignment = .center

    return cell
}

在我的FirstTableViewController

在此tableViewController 中,通过在UITextField 中添加文本来填充单元格,该navigationBar 位于navigationBar 上。然后我希望选定的单元格通过UITextFieldinsdie UIAlertAction 添加文本来保存在SecondTableViewController 中创建的单元格。这些数据使用 UserDefaults.standfard 与“table1”保存

   override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as UITableViewCell

    for object in cell.contentView.subviews
    {
        object.removeFromSuperview();
    }
    let getData = UserDefaults.standard.stringArray(forKey:"table1")

    cell.textLabel?.myArray = getData[indexPath.row] 

    cell.textLabel?.font = UIFont(name: familyFont, size: 19)
    cell.textLabel?.font = UIFont.systemFont(ofSize: 19)
    cell.textLabel?.textColor = UIColor.darkGray
    cell.accessoryType = .disclosureIndicator
    cell.textLabel?.textAlignment = .center

    return cell
}

我不知道如何实现实现这一目标的代码。我研究了帖子并用谷歌搜索了很多东西,但我能找到的只是在视图控制器和表视图控制器之间传递数据,但就我而言,它是关于在 TableView 和 TableView 之间传递数据。因此,这可能也会帮助其他有同样问题的人。

这个问题困扰了我很久。请帮帮我...

谢谢!

【问题讨论】:

  • 如果那不是一个viewController,你怎么能推动它?如果它是普通的表格视图/自定义视图,则必须将其添加为子视图。对于传递数据的问题,您可以自定义 init 方法。
  • 你能显示cellForRowAt indexPath 方法,这样就可以清除你想要作为数组传递的内容。
  • 已更新,请查收。
  • @Janmenjaya 你如何通过 init 传递数据???你能给我任何代码吗?
  • @Ryo,查看我的答案以了解如何自定义 View 的 init 方法以传递数据。

标签: ios tableview swift3 tableviewcell


【解决方案1】:

你很亲密,概念保持不变。在这种情况下,您将传递要在下一个 tableView 中使用的字符串数组,并将该数组用作第二个表的数据源,这样您就可以:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let vc = NewTableViewController()
    vc.dataSourceArray = array
    self.navigationController.pushViewController(vc, animated: true)
}

然后在您的新 tableView 中根据您的 dataSourceArray 设置您的 numberForSections()、numForRows() 和 cellForIndexPath()。这将使用传递的数据填充新的 tableView。

【讨论】:

    【解决方案2】:

    根据您的问题,我认为您正在尝试将数据发送到自定义视图类而不是 viewController 类。

    所以我建议自定义视图类的init方法。

    注意:我下面显示的代码是用 Swift 2.1 编写的。

    在示例中,我采用了UIView 并在该视图中设计了表格视图。我从视图控制器传递数据并将数据列表显示为下拉列表。

    代码:

    在自定义视图中

    init(frame: CGRect, arrData: NSArray) {
        super.init(frame: frame)
    
        arrListData = NSMutableArray(array: arrData)
    
        //In this method I have designed the table view 
        designListView();
    }
    

    在 ViewController 中:

    func ListButtonClicked()
    {
        let arr = ["Asia", "Europe", "Africa", "North America", "South America", "Australia", "Antarctica"]
    
        dropDownlist = DropdownView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height), arrData: arr);
        dropDownlist.backgroundColor = UIColor.clearColor();
        self.view.addSubview(dropDownlist)
    }
    

    这里我提到了部分代码,但为了您的方便,我添加了输出屏幕,以便您检查这是否满足您的要求。

    我的输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-10
      相关资源
      最近更新 更多