mercredi 24 juin 2015

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.

How do I access these switches from AutomaticViewController?

Heres my ManualViewController code:

`

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.
    var nib = UINib(nibName: "vwTblCell", bundle: nil)
    tableView.registerNib(nib, forCellReuseIdentifier: "Cell")

    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")
        }
    }
}

} `

See more ..

Aucun commentaire:

Enregistrer un commentaire