boisterouslobster
boisterouslobster

Reputation: 1293

Calling method when switching to UITableViewController

I managed to programmatically switch to a UITableViewController. However, is there an initial method or something of that nature that I could run as soon as it switches over? I would like the code to run regardless of whether or not the table has data in it.

New to Swift, any help appreciated.

EDIT:

To be more specific, in my main ViewController, I am doing this:

 var next = self.storyboard?.instantiateViewControllerWithIdentifier("MyEvents") as! MyEvents
            self.presentViewController(next, animated: true, completion: nil)

Which as I understand it is pushing the TableViewController

Then the class dedicated to the TableViewController:

    import UIKit

class MyEvents: UITableViewController, SideBarDelegate {

    var eventList = [];
    var sideBar:SideBar = SideBar()



    required init!(coder aDecoder: NSCoder!) {
        self.init()
        sideBar = SideBar(sourceView: self.view, menuItems: ["My Events","Support","Settings"])
        sideBar.delegate = self    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return eventList.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        return basicCellAtIndexPath(indexPath)
    }

    func basicCellAtIndexPath(indexPath:NSIndexPath) -> BasicCell
    {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! BasicCell
        setCellValues(cell, indexPath: indexPath)
        return cell
    }

    func setCellValues(cell:BasicCell, indexPath:NSIndexPath)
    {
         cell.eventLabel.text = "Test Event"
         cell.acceptLabel.text = "100"
         cell.declineLabel.text = "90"
         cell.acceptSubLabel.text = "Accepts"
         cell.declineLabel.text = "Decline"
    }

    func sideBarDidSelectButtonAtIndex(index: Int)
    {
        if index == 0
        {
            println("Here!")
        }

    }

}

Upvotes: 1

Views: 71

Answers (3)

Bennett
Bennett

Reputation: 1107

You could use viewDidAppear or viewWillAppear. Use syntax

override func viewWillAppear() {
super.viewWillAppear()
//Code
}

Hope this helps!

Upvotes: 0

Syed Tariq
Syed Tariq

Reputation: 2918

If you created a subclass of UITableViewController, you can put any initialization code into the (override) method: viewDidLoad. See an example below for other mandatory methods you need:

import UIKit

class TableViewController: UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.title = "Test"
    self.navigationItem.rightBarButtonItem = self.editButtonItem()
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 3
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("cell") as? UITableViewCell
    cell = UITableViewCell(style: .Subtitle, reuseIdentifier: "cell")

    let xrow = indexPath.row
    let xsection = indexPath.section

    println("row = \(xrow), section = \(xsection)")
    println("0: cell.textLabel?.text = \(cell!.textLabel?.text)")
    cell!.textLabel?.text = "Section = \(xsection)"
    println("1: cell.textLabel?.text = \(cell!.textLabel?.text)")
    println("0: cell.detailTextLabel?.text = \(cell!.detailTextLabel?.text)")
    cell!.detailTextLabel?.text = ("section = \(xsection), row = \(xrow)") as String!
    println("1: cell.detailTextLabel?.text = \(cell!.detailTextLabel?.text)")
    return cell!
}

}

Upvotes: 2

amaceing
amaceing

Reputation: 56

I think you should provide a little more detail in your question. When you say 'switch' to a UITableViewController, are you saying that you're pushing this UITableViewController from another one and it's the primary VC on your screen now?

There are some methods that you need to implement in your UITableViewController code; numberOfSections, numberOfRowsInSection, cellForRowAtIndexPath, etc. Basically, you need to comply with the UITableViewDataSource protocol (you can look this up in Apple's documentation).

With a little more detail, we'd be able to help you out some more!

Upvotes: 2

Related Questions