【发布时间】: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