[英]How to access cells of a table view of a view from another view in Swift?
I have two view controllers : ManualViewController, AutomaticViewController ManualViewController has a table view with each cell having a label and a switch. 我有两个视图控制器:ManualViewController,AutomaticViewController ManualViewController有一个表视图,每个单元格都有一个标签和一个开关。
How do I access these switches from AutomaticViewController? 如何从AutomaticViewController访问这些开关?
Heres my ManualViewController code: 这是我的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")
}
}
}
}
` `
You don't want to access the cells of the other UIViewController, you want to access the datasource of the other UIViewController
. 您不想访问另一个UIViewController的单元格,而是想访问另一个
UIViewController
的数据源。
AutomaticViewController will need a reference to the ManualViewController instance you've created, from there you can access the objects
NSMutableArray
but you haven't specified how the two currently have a reference to each other. AutomaticViewController将需要对您创建的ManualViewController实例的引用,从那里您可以访问
objects
NSMutableArray
但尚未指定两者当前如何相互引用。
eg 例如
var array = manualViewController.objects
This effectively gives you access to whatever cells are in the other ViewController. 这有效地使您可以访问另一个ViewController中的任何单元格。
In order to access the switches, you'll want to store their state in the NSMutableArray. 为了访问交换机,您需要将其状态存储在NSMutableArray中。 So rather than an array of strings, have an array of dictionaries.
因此,要有一个字典数组,而不是一个字符串数组。
var objects = [["label" : "iPhone", "switchState": true],["label" : "Apple Watch", "switchState": true],["label" : "Mac", "switchState": true]]
Then in your cellForRowIndexPath method, you can access the state to determine the switch state and update that when the switch is tapped. 然后,在cellForRowIndexPath方法中,您可以访问状态以确定开关状态并在轻按开关时更新状态。 Using what I mentioned earlier, you can then share that data between the two UIViewControllers.
使用前面提到的内容,您可以在两个UIViewController之间共享该数据。
好吧,您的问题还不完整,因为您没有说如何启动“ AutomaticViewController”,但是如果您使用Segue启动此viewController,则只需将单元格数据(而不是单元格对象)转发到下一个视图控制器,更新AutomaticViewController中的数据,然后使用协议更新ManualViewController中的tableview数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.