[英]Table view Help…..selected cells from one table view passed to another table view swift
[英]How to access cells of a table view of a view from another view in Swift?
我有两个视图控制器:ManualViewController,AutomaticViewController ManualViewController有一个表视图,每个单元格都有一个标签和一个开关。
如何从AutomaticViewController访问这些开关?
这是我的ManualViewController代码:
import UIKit
class ManualViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var objects: NSMutableArray! = NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBarHidden = false
self.navigationController?.navigationBar.barTintColor = UIColor.blackColor()
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
self.tabBarController?.tabBar.barTintColor = UIColor.blackColor()
// Do any additional setup after loading the view.
self.objects.addObject("iPhone")
self.objects.addObject("Apple Watch")
self.objects.addObject("Mac")
self.tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return self.objects.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewCell
cell.deviceName.text = self.objects.objectAtIndex(indexPath.row) as? String
cell.deviceState.tag = indexPath.row;
return cell
}
@IBAction func whatToDo(sender: UISwitch) {
var c = sender.tag
if c == 2
{
if sender.on
{
println("on")
}
else
{
println("off")
}
}
}
}
`
您不想访问另一个UIViewController的单元格,而是想访问另一个UIViewController
的数据源。
AutomaticViewController将需要对您创建的ManualViewController实例的引用,从那里您可以访问objects
NSMutableArray
但尚未指定两者当前如何相互引用。
例如
var array = manualViewController.objects
这有效地使您可以访问另一个ViewController中的任何单元格。
为了访问交换机,您需要将其状态存储在NSMutableArray中。 因此,要有一个字典数组,而不是一个字符串数组。
var objects = [["label" : "iPhone", "switchState": true],["label" : "Apple Watch", "switchState": true],["label" : "Mac", "switchState": true]]
然后,在cellForRowIndexPath方法中,您可以访问状态以确定开关状态并在轻按开关时更新状态。 使用前面提到的内容,您可以在两个UIViewController之间共享该数据。
好吧,您的问题还不完整,因为您没有说如何启动“ AutomaticViewController”,但是如果您使用Segue启动此viewController,则只需将单元格数据(而不是单元格对象)转发到下一个视图控制器,更新AutomaticViewController中的数据,然后使用协议更新ManualViewController中的tableview数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.