简体   繁体   English

如何在Swift中从另一个视图访问视图的表格视图的单元格?

[英]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.

相关问题 表格视图帮助.....从一个表格视图中选择的单元格迅速传递到另一个表格视图 - Table view Help…..selected cells from one table view passed to another table view swift 如何使用来自另一个视图控制器的用户输入来使用表视图单元格创建列表 - How to make a list with Table View Cells with user input from another view controller swift Swift:如何访问选定表视图单元格的值 - Swift: How to access value of selected table view cells swift 将带有段的表视图单元格中的数据传递到另一个视图 controller - swift passing data from Table View cells with segment to another view controller Swift 3 Table View-从某些单元格中删除堆栈视图 - Swift 3 Table View - Remove Stack View from some cells 表格视图未显示单元格快速 - Table View not showing the Cells Swift 如何在Swift中将数据从一个表视图传递到另一个表视图? - How to pass data from one table view to another table view in Swift? 如何在表视图单元格中获取此表视图在Swift 4中另一个表视图控制器中的文本字段文本? - how to get textfields texts in tableview cells that this tableview is in another table view controller in swift 4? 在Swift中,当定向发生时,如何在表格视图中看到相同的单元格? - In Swift, how to see the same cells for a table view when an orientation happens? 如何在表格视图中删除空单元格? iOS Swift - How do I remove empty cells in table view? iOS Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM