2016-02-13 76 views
0

我想用表格视图中的原型单元格填充用户输入到先前视图控制器中的文本字段中的信息。我想使用这个数据来设置每个单元格的标题和副标题,而不是预先确定的数组。如何将数据从视图控制器中的文本字段传递到表格单元视图的标题/副标题部分?使用以前视图中的文本字段中的数据填充表格视图单元格

任何帮助表示赞赏。

+0

你的问题很广泛,你想知道如何在控制器之间传递数据吗?或者如何填充UITableViewCell?为了获得答案,你需要更新你的问题,以显示你迄今为止做了什么,使用的代码等。这将使人们能够准确地帮助 –

回答

0

首先在要原因请看你TableViewController点实现这个:

let textFieldString = yourTextField.text ?? "" 
performSegueWithIdentifier("exampleID", sender: textFieldString) 

在你的故事板现在创建正确的SEGUE!在执行之前赛格瑞

1)From the whole ViewController to your destination ViewController

2)And don't forget your unique segue-ID

委托方法prepareForSegue(...)将被调用。在这种方法中,您准备好您的目标ViewController。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) 
{ 
     let targetvc = segue.destinationViewController as? YourTableViewController 
     if let targetvc = targetvc 
     { 
      let yourTextFieldString = sender as? String 
      if let yourTextFieldString = yourTextFieldString 
      { 
       targetvc.yourTextFieldString = yourTextFieldString 
      } 
     } 
} 

当执行SEGUE到目的地的ViewController,你的变量(这里在这种情况下,“yourTextFieldString”)的前一组数据。

class yourTableViewController : UITableViewController 
{ 
    var yourTextFieldString = "" // This variable has the previous set data 
    override func viewDidLoad() 
    { 
     super.viewDidLoad() 
    } 
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    { 
     let cell = tableView.dequeueReusableCellWithIdentifier("yourCellID", forIndexPath: indexPath) 
     cell.textLabel.text = yourTextFieldString 
     return cell 
    } 
} 

现在调整你的原型单元格(不要忘记正确的标识符),你就完成了。

相关问题